From 6c774f960afcb992fa8d7560c91b4440a00d80a8 Mon Sep 17 00:00:00 2001 From: Araozu Date: Wed, 13 Dec 2023 06:12:13 -0500 Subject: [PATCH] CLI help & empty commands --- src/cli/empty.rs | 46 ++++++++++++++++++++++++++++++++++++++++++++++ src/cli/help.rs | 6 +++++- src/cli/mod.rs | 14 +++++++++++--- src/cli/types.rs | 1 + 4 files changed, 63 insertions(+), 4 deletions(-) diff --git a/src/cli/empty.rs b/src/cli/empty.rs index e69de29..955d165 100644 --- a/src/cli/empty.rs +++ b/src/cli/empty.rs @@ -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) { + // 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 { + match option.as_str() { + "-h" | "--help" => Ok(EmptyOptions::Help), + "-v" | "--version" => Ok(EmptyOptions::Version), + _ => Err(option.clone()), + } +} diff --git a/src/cli/help.rs b/src/cli/help.rs index 24b0591..75c6b97 100644 --- a/src/cli/help.rs +++ b/src/cli/help.rs @@ -5,6 +5,10 @@ pub fn help_command(options: &Vec) { 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." + ); } } diff --git a/src/cli/mod.rs b/src/cli/mod.rs index 4b6fa8c..4ac9ef9 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -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 { 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 + )); } } diff --git a/src/cli/types.rs b/src/cli/types.rs index fcc742b..a4fdc2c 100644 --- a/src/cli/types.rs +++ b/src/cli/types.rs @@ -27,6 +27,7 @@ impl CommandType { pub fn run(&self, options: &Vec) { match self { CommandType::Help => super::help::help_command(options), + CommandType::None => super::empty::empty_command(options), _ => { println!("Not implemented yet! {:?} {:?}", self, options); }