diff --git a/src/parse/parser.rs b/src/parse/parser.rs index aa5b26554ed..83325feee15 100644 --- a/src/parse/parser.rs +++ b/src/parse/parser.rs @@ -892,7 +892,6 @@ impl<'help, 'app> Parser<'help, 'app> { fn parse_help_subcommand(&self, cmds: &[OsString]) -> ClapResult { debug!("Parser::parse_help_subcommand"); - let mut help_help = false; let mut bin_name = self.app.bin_name.as_ref().unwrap_or(&self.app.name).clone(); let mut sc = { @@ -901,12 +900,6 @@ impl<'help, 'app> Parser<'help, 'app> { let mut sc = self.app.clone(); for cmd in cmds.iter() { - if cmd == OsStr::new("help") { - // cmd help help - help_help = true; - break; // Maybe? - } - sc = if let Some(c) = sc.find_subcommand(cmd) { c } else if let Some(c) = sc.find_subcommand(&cmd.to_string_lossy()) { @@ -924,6 +917,16 @@ impl<'help, 'app> Parser<'help, 'app> { } .clone(); + if cmd == OsStr::new("help") { + let pb = Arg::new("subcommand") + .index(1) + .setting(ArgSettings::TakesValue) + .setting(ArgSettings::MultipleValues) + .value_name("SUBCOMMAND") + .about("The subcommand whose help message to display"); + sc = sc.arg(pb); + } + sc._build(); bin_name.push(' '); bin_name.push_str(&sc.name); @@ -931,24 +934,10 @@ impl<'help, 'app> Parser<'help, 'app> { sc }; + sc = sc.bin_name(bin_name); let parser = Parser::new(&mut sc); - if help_help { - let mut pb = Arg::new("subcommand") - .index(1) - .setting(ArgSettings::TakesValue) - .setting(ArgSettings::MultipleValues) - .about("The subcommand whose help message to display"); - - pb._build(); - //parser.positionals.insert(1, pb.name); - parser.app.settings = parser.app.settings | self.app.g_settings; - parser.app.g_settings = self.app.g_settings; - } - - parser.app.bin_name = Some(bin_name); - Err(parser.help_err(self.app.is_set(AS::UseLongFormatForHelpSubcommand))) } diff --git a/tests/help.rs b/tests/help.rs index 7f098bb07f0..fee7af246c7 100644 --- a/tests/help.rs +++ b/tests/help.rs @@ -2137,6 +2137,62 @@ fn after_help_no_args() { assert_eq!(help, AFTER_HELP_NO_ARGS); } +static HELP_SUBCMD_HELP: &str = "myapp-help + +Print this message or the help of the given subcommand(s) + +USAGE: + myapp help [SUBCOMMAND]... + +ARGS: + ... The subcommand whose help message to display + +OPTIONS: + -h, --help Print custom help about text +"; + +#[test] +fn help_subcmd_help() { + let app = App::new("myapp") + .mut_arg("help", |h| h.about("Print custom help about text")) + .subcommand(App::new("subcmd").subcommand(App::new("multi").version("1.0"))); + + assert!(utils::compare_output( + app.clone(), + "myapp help help", + HELP_SUBCMD_HELP, + false + )); +} + +static SUBCMD_HELP_SUBCMD_HELP: &str = "myapp-subcmd-help + +Print this message or the help of the given subcommand(s) + +USAGE: + myapp subcmd help [SUBCOMMAND]... + +ARGS: + ... The subcommand whose help message to display + +OPTIONS: + -h, --help Print custom help about text +"; + +#[test] +fn subcmd_help_subcmd_help() { + let app = App::new("myapp") + .mut_arg("help", |h| h.about("Print custom help about text")) + .subcommand(App::new("subcmd").subcommand(App::new("multi").version("1.0"))); + + assert!(utils::compare_output( + app.clone(), + "myapp subcmd help help", + SUBCMD_HELP_SUBCMD_HELP, + false + )); +} + static HELP_ABOUT_MULTI_SC: &str = "myapp-subcmd-multi 1.0 USAGE: