From b8a82922c74b881bd059d59cf883086af7243634 Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Sun, 1 Dec 2019 09:42:51 +0100 Subject: [PATCH] src/cli/command.rs: handle multilevel sub commands in help --- src/cli.rs | 6 +++--- src/cli/command.rs | 21 +++++++++++---------- src/cli/format.rs | 14 +++++++++----- 3 files changed, 23 insertions(+), 18 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index d927d456..0d7a322b 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -78,10 +78,10 @@ impl CliCommandMap { self } - fn find_command(&self, name: &str) -> Option<&CommandLineInterface> { + fn find_command(&self, name: &str) -> Option<(String, &CommandLineInterface)> { if let Some(sub_cmd) = self.commands.get(name) { - return Some(sub_cmd); + return Some((name.to_string(), sub_cmd)); }; let mut matches: Vec<&str> = vec![]; @@ -94,7 +94,7 @@ impl CliCommandMap { if matches.len() != 1 { return None; } if let Some(sub_cmd) = self.commands.get(matches[0]) { - return Some(sub_cmd); + return Some((matches[0].to_string(), sub_cmd)); }; None diff --git a/src/cli/command.rs b/src/cli/command.rs index 91fc45e5..554c8696 100644 --- a/src/cli/command.rs +++ b/src/cli/command.rs @@ -94,7 +94,7 @@ pub fn handle_nested_command( let command = args.remove(0); - let sub_cmd = match def.find_command(&command) { + let (_, sub_cmd) = match def.find_command(&command) { Some(cmd) => cmd, None => { let err_msg = format!("no such command '{}'", command); @@ -331,7 +331,10 @@ const API_METHOD_COMMAND_HELP: ApiMethod = ApiMethod::new( &[ ( "command", true, - &StringSchema::new("Command name.").schema() + &ArraySchema::new( + "Command", + &StringSchema::new("Name.").schema() + ).schema() ), ( "verbose", true, @@ -351,20 +354,18 @@ fn help_command( _rpcenv: &mut dyn RpcEnvironment, ) -> Result { + let command: Vec = param["command"].as_array().unwrap_or(&Vec::new()) + .iter() + .map(|v| v.as_str().unwrap().to_string()) + .collect(); + - let command = param["command"].as_str(); let verbose = param["verbose"].as_bool(); HELP_CONTEXT.with(|ctx| { match &*ctx.borrow() { Some(def) => { - let mut args = Vec::new(); - // TODO: Handle multilevel sub commands - if let Some(command) = command { - args.push(command.to_string()); - } - - print_help(def, String::from(""), &args, verbose); + print_help(def, String::from(""), &command, verbose); } None => { eprintln!("Sorry, help context not set - internal error."); diff --git a/src/cli/format.rs b/src/cli/format.rs index 8434bff4..d4e0145b 100644 --- a/src/cli/format.rs +++ b/src/cli/format.rs @@ -173,15 +173,19 @@ pub fn print_help( for cmd in args { if let CommandLineInterface::Nested(map) = iface { - if let Some(subcmd) = map.find_command(cmd) { + if let Some((full_name, subcmd)) = map.find_command(cmd) { iface = subcmd; - prefix.push(' '); - prefix.push_str(cmd); + if !prefix.is_empty() { prefix.push(' '); } + prefix.push_str(&full_name); continue; } } - eprintln!("no such command '{}'", cmd); - std::process::exit(-1); + if prefix.is_empty() { + eprintln!("no such command '{}'", cmd); + } else { + eprintln!("no such command '{} {}'", prefix, cmd); + } + return; } let format = match verbose.unwrap_or(false) {