Skip to content

Commit e3d2893

Browse files
committed
fix(Help): prevents invoking <cmd> help help and displaying incorrect help message
Previously, if one ran `<cmd> help help` an additional `help` subcommand was created and a help message displayed for it. We *could* have just thrown an error `<cmd> help help` but I worry that the message would be confusing, because something like, "Invalid Subcommand" isn't 100% correct as the form `<cmd> help <subcmd>` is allowed, and there *is* a `help` subcmd. This fix correct dispatches `<cmd> help help` to the `<cmd>` help message. Closes #538
1 parent 08ad1cf commit e3d2893

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

src/app/parser.rs

+16-2
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ impl<'a, 'b> Parser<'a, 'b>
195195

196196
pub fn add_subcommand(&mut self, mut subcmd: App<'a, 'b>) {
197197
debugln!("fn=Parser::add_subcommand;");
198-
debugln!("Term widnth...{:?}", self.p.meta.term_w);
198+
debugln!("Term widnth...{:?}", self.meta.term_w);
199199
subcmd.p.meta.term_w = self.meta.term_w;
200200
debug!("Is help...");
201201
if subcmd.p.meta.name == "help" {
@@ -520,9 +520,13 @@ impl<'a, 'b> Parser<'a, 'b>
520520
if &*arg_os == "help" &&
521521
self.settings.is_set(AppSettings::NeedsSubcommandHelp) {
522522
let cmds: Vec<OsString> = it.map(|c| c.into()).collect();
523+
let mut help_help = false;
523524
let mut sc = {
524525
let mut sc: &Parser = self;
525526
for (i, cmd) in cmds.iter().enumerate() {
527+
if &*cmd.to_string_lossy() == "help" { // cmd help help
528+
help_help = true;
529+
}
526530
if let Some(c) = sc.subcommands
527531
.iter()
528532
.filter(|s| &*s.p.meta.name == cmd)
@@ -563,7 +567,17 @@ impl<'a, 'b> Parser<'a, 'b>
563567
}
564568
sc.clone()
565569
};
566-
sc.create_help_and_version();
570+
if help_help {
571+
let mut pb = PosBuilder::new("subcommand", 1);
572+
pb.help = Some("The subcommand whose help message to display");
573+
pb.set(ArgSettings::Multiple);
574+
sc.positionals.insert(1, pb);
575+
for s in self.g_settings.clone() {
576+
sc.set(s);
577+
}
578+
} else {
579+
sc.create_help_and_version();
580+
}
567581
if sc.meta.bin_name != self.meta.bin_name {
568582
sc.meta.bin_name = Some(format!("{}{}{}",
569583
self.meta

0 commit comments

Comments
 (0)