Now every cli command handles its own arguments
This commit is contained in:
parent
6c774f960a
commit
3d5c7769e6
7
src/cli/compile.rs
Normal file
7
src/cli/compile.rs
Normal file
@ -0,0 +1,7 @@
|
||||
use crate::cli::{get_help_text, get_version};
|
||||
use colored::*;
|
||||
|
||||
#[derive(Eq, PartialEq, Hash)]
|
||||
enum CompileOptions {
|
||||
Help,
|
||||
}
|
@ -7,11 +7,11 @@ enum EmptyOptions {
|
||||
Version,
|
||||
}
|
||||
|
||||
pub fn empty_command(options: &Vec<String>) {
|
||||
pub fn empty_command(arguments: Vec<String>) {
|
||||
// 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);
|
||||
}
|
||||
|
@ -1,14 +1,14 @@
|
||||
use crate::cli::get_help_text;
|
||||
use colored::*;
|
||||
|
||||
pub fn help_command(options: &Vec<String>) {
|
||||
pub fn help_command(arguments: Vec<String>) {
|
||||
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."
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -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<Command, String> {
|
||||
fn parse_args() -> Result<(CommandType, Vec<String>), String> {
|
||||
let mut args = std::env::args().collect::<Vec<String>>();
|
||||
|
||||
// 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<Command, String> {
|
||||
"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))
|
||||
}
|
||||
|
@ -1,10 +1,4 @@
|
||||
#[derive(Debug)]
|
||||
pub struct Command {
|
||||
pub command: CommandType,
|
||||
pub options: Vec<String>,
|
||||
}
|
||||
|
||||
#[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<String>) {
|
||||
pub fn run(&self, options: Vec<String>) {
|
||||
match self {
|
||||
CommandType::Help => super::help::help_command(options),
|
||||
CommandType::None => super::empty::empty_command(options),
|
||||
|
Loading…
Reference in New Issue
Block a user