Truncate command list passed to command function

Only pass it the arguments after its own path, if any exist
This commit is contained in:
greg 2019-03-16 18:33:31 -07:00
parent d06cf90fce
commit ac0050e5d1
2 changed files with 15 additions and 9 deletions

View File

@ -1,6 +1,6 @@
use super::Repl; use super::Repl;
pub type BoxedCommandFunction = Box<(fn(&mut Repl, Vec<&str>) -> Option<String>)>; pub type BoxedCommandFunction = Box<(fn(&mut Repl, &[&str]) -> Option<String>)>;
#[derive(Clone)] #[derive(Clone)]
pub enum CommandTree { pub enum CommandTree {

View File

@ -92,7 +92,7 @@ impl Repl {
*/ */
} }
fn get_function_from_directives<'a>(directives: &'a CommandTree, commands: &Vec<&str>) -> Result<&'a BoxedCommandFunction, String> { fn get_function_from_directives<'a>(directives: &'a CommandTree, commands: &Vec<&str>) -> Result<(&'a BoxedCommandFunction, usize), String> {
let mut dir_pointer: &CommandTree = &directives; let mut dir_pointer: &CommandTree = &directives;
let mut idx = 0; let mut idx = 0;
@ -112,7 +112,10 @@ impl Repl {
}; };
}, },
CommandTree::Terminal { name, function, .. } => { CommandTree::Terminal { name, function, .. } => {
break function.as_ref().ok_or(format!("No action specified for: {:?}", commands)); break match function.as_ref() {
Some(f) => Ok((f, idx)),
None => Err(format!("No action specified for: {:?}", commands)),
};
} }
} }
} }
@ -131,9 +134,9 @@ impl Repl {
} }
let directives = self.get_directives(); let directives = self.get_directives();
let result: Result<&BoxedCommandFunction, String> = Repl::get_function_from_directives(&directives, &commands); let result: Result<(&BoxedCommandFunction, _), String> = Repl::get_function_from_directives(&directives, &commands);
match result { match result {
Ok(f) => f(self, commands), Ok((f, idx)) => f(self, &commands[idx..]),
Err(err) => Some(err.red().to_string()) Err(err) => Some(err.red().to_string())
} }
} }
@ -208,16 +211,19 @@ impl Repl {
*/ */
CommandTree::Top(vec![ CommandTree::Top(vec![
CommandTree::term_with_function("exit", Some("exit the REPL"), Box::new(|repl: &mut Repl, _cmds: Vec<&str>| { CommandTree::term_with_function("exit", Some("exit the REPL"), Box::new(|repl: &mut Repl, _cmds: &[&str]| {
repl.save_before_exit(); repl.save_before_exit();
::std::process::exit(0) ::std::process::exit(0)
})), })),
CommandTree::term_with_function("quit", Some("exit the REPL"), Box::new(|repl: &mut Repl, _cmds: Vec<&str>| { CommandTree::term_with_function("quit", Some("exit the REPL"), Box::new(|repl: &mut Repl, _cmds: &[&str]| {
repl.save_before_exit(); repl.save_before_exit();
::std::process::exit(0) ::std::process::exit(0)
})), })),
CommandTree::term_with_function("help", Some("Print this help message"), Box::new(|repl: &mut Repl, _cmds: Vec<&str>| { CommandTree::term_with_function("help", Some("Print this help message"), Box::new(|repl: &mut Repl, cmds: &[&str]| {
Some(repl.print_help_message()) Some(match cmds {
[] => repl.print_help_message(),
_ => format!("ARGS: {:?}", cmds)
})
})), })),
CommandTree::nonterm("debug", CommandTree::nonterm("debug",
Some("show or hide pass debug info for a given pass, or display the names of all passes, or turn timing on/off"), Some("show or hide pass debug info for a given pass, or display the names of all passes, or turn timing on/off"),