From ac0050e5d1ffd8df14388c16be26164acd2e0bd3 Mon Sep 17 00:00:00 2001 From: greg Date: Sat, 16 Mar 2019 18:33:31 -0700 Subject: [PATCH] Truncate command list passed to command function Only pass it the arguments after its own path, if any exist --- schala-repl/src/repl/command_tree.rs | 2 +- schala-repl/src/repl/mod.rs | 22 ++++++++++++++-------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/schala-repl/src/repl/command_tree.rs b/schala-repl/src/repl/command_tree.rs index f4508b6..279a338 100644 --- a/schala-repl/src/repl/command_tree.rs +++ b/schala-repl/src/repl/command_tree.rs @@ -1,6 +1,6 @@ use super::Repl; -pub type BoxedCommandFunction = Box<(fn(&mut Repl, Vec<&str>) -> Option)>; +pub type BoxedCommandFunction = Box<(fn(&mut Repl, &[&str]) -> Option)>; #[derive(Clone)] pub enum CommandTree { diff --git a/schala-repl/src/repl/mod.rs b/schala-repl/src/repl/mod.rs index 3cf691b..69456b6 100644 --- a/schala-repl/src/repl/mod.rs +++ b/schala-repl/src/repl/mod.rs @@ -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 idx = 0; @@ -112,7 +112,10 @@ impl Repl { }; }, 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 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 { - Ok(f) => f(self, commands), + Ok((f, idx)) => f(self, &commands[idx..]), Err(err) => Some(err.red().to_string()) } } @@ -208,16 +211,19 @@ impl Repl { */ 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(); ::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(); ::std::process::exit(0) })), - CommandTree::term_with_function("help", Some("Print this help message"), Box::new(|repl: &mut Repl, _cmds: Vec<&str>| { - Some(repl.print_help_message()) + CommandTree::term_with_function("help", Some("Print this help message"), Box::new(|repl: &mut Repl, cmds: &[&str]| { + Some(match cmds { + [] => repl.print_help_message(), + _ => format!("ARGS: {:?}", cmds) + }) })), 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"),