More help cleanup

This commit is contained in:
greg 2019-06-06 22:36:44 -07:00
parent 7097775a4a
commit f88f2e8550

View File

@ -1,5 +1,6 @@
use std::fmt::Write as FmtWrite; use std::fmt::Write as FmtWrite;
use super::command_tree::CommandTree;
use super::{Repl, InterpreterDirectiveOutput}; use super::{Repl, InterpreterDirectiveOutput};
pub fn help(repl: &mut Repl, arguments: &[&str]) -> InterpreterDirectiveOutput { pub fn help(repl: &mut Repl, arguments: &[&str]) -> InterpreterDirectiveOutput {
@ -7,6 +8,15 @@ pub fn help(repl: &mut Repl, arguments: &[&str]) -> InterpreterDirectiveOutput {
[] => return global_help(repl), [] => return global_help(repl),
commands => { commands => {
let dirs = repl.get_directives(); let dirs = repl.get_directives();
Some(match get_directive_from_commands(commands, &dirs) {
None => format!("Directive `{}` not found", commands.last().unwrap()),
Some(dir) => format!("`{}` - {}", dir.get_cmd(), dir.get_help())
})
}
}
}
fn get_directive_from_commands<'a>(commands: &[&str], dirs: &'a CommandTree) -> Option<&'a CommandTree> {
let mut directive_list = dirs.get_children(); let mut directive_list = dirs.get_children();
let mut matched_directive = None; let mut matched_directive = None;
for cmd in commands { for cmd in commands {
@ -17,14 +27,7 @@ pub fn help(repl: &mut Repl, arguments: &[&str]) -> InterpreterDirectiveOutput {
matched_directive = found; matched_directive = found;
} }
matched_directive
Some(if let Some(dir) = matched_directive {
format!("{}", dir.get_help())
} else {
format!("Last command not found")
})
}
}
} }
fn global_help(repl: &mut Repl) -> InterpreterDirectiveOutput { fn global_help(repl: &mut Repl) -> InterpreterDirectiveOutput {