CLI help & empty commands
This commit is contained in:
parent
09c267139d
commit
6c774f960a
@ -0,0 +1,46 @@
|
||||
use crate::cli::{get_help_text, get_version};
|
||||
use colored::*;
|
||||
|
||||
#[derive(Eq, PartialEq, Hash)]
|
||||
enum EmptyOptions {
|
||||
Help,
|
||||
Version,
|
||||
}
|
||||
|
||||
pub fn empty_command(options: &Vec<String>) {
|
||||
// Add all options to a set
|
||||
let mut options_set = std::collections::HashSet::new();
|
||||
for option in options {
|
||||
match expand_option(option) {
|
||||
Ok(o) => {
|
||||
options_set.insert(o);
|
||||
}
|
||||
Err(invalid_option) => {
|
||||
println!("{}", get_help_text());
|
||||
println!("{}: invalid option: `{}`", "error".on_red(), invalid_option);
|
||||
return;
|
||||
}
|
||||
};
|
||||
}
|
||||
let options = options_set;
|
||||
|
||||
if options.is_empty() {
|
||||
println!("{}", get_help_text());
|
||||
} else {
|
||||
if options.contains(&EmptyOptions::Version) {
|
||||
println!("{}\n", get_version());
|
||||
}
|
||||
|
||||
if options.contains(&EmptyOptions::Help) {
|
||||
println!("{}", get_help_text());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn expand_option(option: &String) -> Result<EmptyOptions, String> {
|
||||
match option.as_str() {
|
||||
"-h" | "--help" => Ok(EmptyOptions::Help),
|
||||
"-v" | "--version" => Ok(EmptyOptions::Version),
|
||||
_ => Err(option.clone()),
|
||||
}
|
||||
}
|
@ -5,6 +5,10 @@ pub fn help_command(options: &Vec<String>) {
|
||||
println!("{}", get_help_text());
|
||||
|
||||
if options.len() > 0 {
|
||||
println!("{}: {}", "warning".yellow(), "The help command doesn't take any options.");
|
||||
println!(
|
||||
"{}: {}",
|
||||
"warning".yellow(),
|
||||
"The help command doesn't take any options."
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
mod empty;
|
||||
mod help;
|
||||
mod types;
|
||||
mod empty;
|
||||
|
||||
use types::{Command, CommandType};
|
||||
|
||||
@ -27,6 +27,7 @@ Commands
|
||||
General options
|
||||
|
||||
-h, --help Print command-specific usage
|
||||
-v, --version Print version & exit
|
||||
"#,
|
||||
"_file_".green()
|
||||
)
|
||||
@ -55,18 +56,25 @@ fn parse_args() -> Result<Command, String> {
|
||||
args.remove(0);
|
||||
|
||||
let mut args = args.into_iter();
|
||||
let mut options = Vec::new();
|
||||
|
||||
let command = match args.next() {
|
||||
Some(command) if !command.starts_with('-') => Some(command),
|
||||
Some(option) => {
|
||||
options.push(option);
|
||||
None
|
||||
}
|
||||
_ => None,
|
||||
};
|
||||
|
||||
let mut options = Vec::new();
|
||||
for arg in args {
|
||||
if arg.starts_with('-') {
|
||||
options.push(arg);
|
||||
} else {
|
||||
return Err(format!("Unexpected command `{}` after the options", arg));
|
||||
return Err(format!(
|
||||
"Unexpected command `{}`. There can only be one command",
|
||||
arg
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -27,6 +27,7 @@ impl CommandType {
|
||||
pub fn run(&self, options: &Vec<String>) {
|
||||
match self {
|
||||
CommandType::Help => super::help::help_command(options),
|
||||
CommandType::None => super::empty::empty_command(options),
|
||||
_ => {
|
||||
println!("Not implemented yet! {:?} {:?}", self, options);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user