Now every cli command handles its own arguments

master
Araozu 2023-12-13 20:03:23 -05:00
parent 6c774f960a
commit 3d5c7769e6
5 changed files with 30 additions and 51 deletions

7
src/cli/compile.rs Normal file
View File

@ -0,0 +1,7 @@
use crate::cli::{get_help_text, get_version};
use colored::*;
#[derive(Eq, PartialEq, Hash)]
enum CompileOptions {
Help,
}

View File

@ -7,11 +7,11 @@ enum EmptyOptions {
Version, Version,
} }
pub fn empty_command(options: &Vec<String>) { pub fn empty_command(arguments: Vec<String>) {
// Add all options to a set // Add all options to a set
let mut options_set = std::collections::HashSet::new(); let mut options_set = std::collections::HashSet::new();
for option in options { for option in arguments {
match expand_option(option) { match expand_option(&option) {
Ok(o) => { Ok(o) => {
options_set.insert(o); options_set.insert(o);
} }

View File

@ -1,14 +1,14 @@
use crate::cli::get_help_text; use crate::cli::get_help_text;
use colored::*; use colored::*;
pub fn help_command(options: &Vec<String>) { pub fn help_command(arguments: Vec<String>) {
println!("{}", get_help_text()); println!("{}", get_help_text());
if options.len() > 0 { if arguments.len() > 0 {
println!( println!(
"{}: {}", "{}: {}",
"warning".yellow(), "warning".yellow(),
"The help command doesn't take any options." "The help command doesn't take any argument."
); );
} }
} }

View File

@ -1,8 +1,9 @@
mod compile;
mod empty; mod empty;
mod help; mod help;
mod types; mod types;
use types::{Command, CommandType}; use types::CommandType;
use colored::*; use colored::*;
@ -39,7 +40,7 @@ fn get_version() -> String {
} }
pub fn run_cli() { pub fn run_cli() {
let command = match parse_args() { let (command, args) = match parse_args() {
Ok(c) => c, Ok(c) => c,
Err(reason) => { Err(reason) => {
println!("{}", get_help_text()); println!("{}", get_help_text());
@ -48,38 +49,17 @@ pub fn run_cli() {
} }
}; };
command.run(); command.run(args);
} }
fn parse_args() -> Result<Command, String> { fn parse_args() -> Result<(CommandType, Vec<String>), String> {
let mut args = std::env::args().collect::<Vec<String>>(); let mut args = std::env::args().collect::<Vec<String>>();
// Remove the first argument, which is the path to the executable
args.remove(0); args.remove(0);
let mut args = args.into_iter(); let command = match args.get(0) {
let mut options = Vec::new(); Some(command) if !command.starts_with('-') => match command.as_str() {
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() {
"c" | "compile" => CommandType::Compile, "c" | "compile" => CommandType::Compile,
"f" | "format" => CommandType::Format, "f" | "format" => CommandType::Format,
"r" | "repl" => CommandType::Repl, "r" | "repl" => CommandType::Repl,
@ -90,8 +70,12 @@ fn parse_args() -> Result<Command, String> {
"help" | "h" => CommandType::Help, "help" | "h" => CommandType::Help,
_ => return Err(format!("Unknown command `{}`", command)), _ => 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))
} }

View File

@ -1,10 +1,4 @@
#[derive(Debug)] #[derive(Debug, PartialEq)]
pub struct Command {
pub command: CommandType,
pub options: Vec<String>,
}
#[derive(Debug)]
pub enum CommandType { pub enum CommandType {
Compile, Compile,
Format, Format,
@ -17,14 +11,8 @@ pub enum CommandType {
None, None,
} }
impl Command {
pub fn run(&self) {
self.command.run(&self.options);
}
}
impl CommandType { impl CommandType {
pub fn run(&self, options: &Vec<String>) { pub fn run(&self, options: Vec<String>) {
match self { match self {
CommandType::Help => super::help::help_command(options), CommandType::Help => super::help::help_command(options),
CommandType::None => super::empty::empty_command(options), CommandType::None => super::empty::empty_command(options),