src/cli.rs: add insert_help() method

This commit is contained in:
Dietmar Maurer 2019-11-30 14:56:31 +01:00
parent 1201abcffa
commit 2b691daf6f
2 changed files with 8 additions and 5 deletions

View File

@ -32,7 +32,7 @@ fn command_map() -> CliCommandMap {
let cmd_def = CliCommandMap::new() let cmd_def = CliCommandMap::new()
.insert("ls", CliCommand::new(&API_METHOD_TEST_COMMAND).into()) .insert("ls", CliCommand::new(&API_METHOD_TEST_COMMAND).into())
.insert("test", CliCommand::new(&API_METHOD_TEST_COMMAND).into()) .insert("test", CliCommand::new(&API_METHOD_TEST_COMMAND).into())
.insert("help", help_command_def().into()); .insert_help();
cmd_def cmd_def
} }
@ -53,12 +53,10 @@ fn main() -> Result<(), Error> {
while let Ok(line) = rl.readline("# prompt: ") { while let Ok(line) = rl.readline("# prompt: ") {
let helper = rl.helper().unwrap(); let helper = rl.helper().unwrap();
// readline already handles tabs, so here we only split on spaces
let args = shellword_split(&line)?; let args = shellword_split(&line)?;
let def = helper.cmd_def(); handle_command(helper.cmd_def(), "", args);
handle_command(def, "", args);
} }
Ok(()) Ok(())

View File

@ -73,6 +73,11 @@ impl CliCommandMap {
self self
} }
pub fn insert_help(mut self) -> Self {
self.commands.insert(String::from("help"), help_command_def().into());
self
}
fn find_command(&self, name: &str) -> Option<&CommandLineInterface> { fn find_command(&self, name: &str) -> Option<&CommandLineInterface> {
if let Some(sub_cmd) = self.commands.get(name) { if let Some(sub_cmd) = self.commands.get(name) {