From 3d5c7769e6ab8c6ab3d0b4110c37c204abb66c95 Mon Sep 17 00:00:00 2001 From: Araozu Date: Wed, 13 Dec 2023 20:03:23 -0500 Subject: [PATCH] Now every cli command handles its own arguments --- src/cli/compile.rs | 7 +++++++ src/cli/empty.rs | 6 +++--- src/cli/help.rs | 6 +++--- src/cli/mod.rs | 46 +++++++++++++++------------------------------- src/cli/types.rs | 16 ++-------------- 5 files changed, 30 insertions(+), 51 deletions(-) create mode 100644 src/cli/compile.rs diff --git a/src/cli/compile.rs b/src/cli/compile.rs new file mode 100644 index 0000000..28cdb33 --- /dev/null +++ b/src/cli/compile.rs @@ -0,0 +1,7 @@ +use crate::cli::{get_help_text, get_version}; +use colored::*; + +#[derive(Eq, PartialEq, Hash)] +enum CompileOptions { + Help, +} diff --git a/src/cli/empty.rs b/src/cli/empty.rs index 955d165..9fb1410 100644 --- a/src/cli/empty.rs +++ b/src/cli/empty.rs @@ -7,11 +7,11 @@ enum EmptyOptions { Version, } -pub fn empty_command(options: &Vec) { +pub fn empty_command(arguments: Vec) { // Add all options to a set let mut options_set = std::collections::HashSet::new(); - for option in options { - match expand_option(option) { + for option in arguments { + match expand_option(&option) { Ok(o) => { options_set.insert(o); } diff --git a/src/cli/help.rs b/src/cli/help.rs index 75c6b97..c259430 100644 --- a/src/cli/help.rs +++ b/src/cli/help.rs @@ -1,14 +1,14 @@ use crate::cli::get_help_text; use colored::*; -pub fn help_command(options: &Vec) { +pub fn help_command(arguments: Vec) { println!("{}", get_help_text()); - if options.len() > 0 { + if arguments.len() > 0 { println!( "{}: {}", "warning".yellow(), - "The help command doesn't take any options." + "The help command doesn't take any argument." ); } } diff --git a/src/cli/mod.rs b/src/cli/mod.rs index 4ac9ef9..c585249 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -1,8 +1,9 @@ +mod compile; mod empty; mod help; mod types; -use types::{Command, CommandType}; +use types::CommandType; use colored::*; @@ -39,7 +40,7 @@ fn get_version() -> String { } pub fn run_cli() { - let command = match parse_args() { + let (command, args) = match parse_args() { Ok(c) => c, Err(reason) => { println!("{}", get_help_text()); @@ -48,38 +49,17 @@ pub fn run_cli() { } }; - command.run(); + command.run(args); } -fn parse_args() -> Result { +fn parse_args() -> Result<(CommandType, Vec), String> { let mut args = std::env::args().collect::>(); + + // Remove the first argument, which is the path to the executable 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, - }; - - for arg in args { - if arg.starts_with('-') { - options.push(arg); - } else { - return Err(format!( - "Unexpected command `{}`. There can only be one command", - arg - )); - } - } - - let command = match command { - Some(command) => match command.as_str() { + let command = match args.get(0) { + Some(command) if !command.starts_with('-') => match command.as_str() { "c" | "compile" => CommandType::Compile, "f" | "format" => CommandType::Format, "r" | "repl" => CommandType::Repl, @@ -90,8 +70,12 @@ fn parse_args() -> Result { "help" | "h" => CommandType::Help, _ => return Err(format!("Unknown command `{}`", command)), }, - None => CommandType::None, + _ => CommandType::None, }; - Ok(Command { command, options }) + if command != CommandType::None { + args.remove(0); + } + + Ok((command, args)) } diff --git a/src/cli/types.rs b/src/cli/types.rs index a4fdc2c..05e80bb 100644 --- a/src/cli/types.rs +++ b/src/cli/types.rs @@ -1,10 +1,4 @@ -#[derive(Debug)] -pub struct Command { - pub command: CommandType, - pub options: Vec, -} - -#[derive(Debug)] +#[derive(Debug, PartialEq)] pub enum CommandType { Compile, Format, @@ -17,14 +11,8 @@ pub enum CommandType { None, } -impl Command { - pub fn run(&self) { - self.command.run(&self.options); - } -} - impl CommandType { - pub fn run(&self, options: &Vec) { + pub fn run(&self, options: Vec) { match self { CommandType::Help => super::help::help_command(options), CommandType::None => super::empty::empty_command(options),