Skip to content

Commit

Permalink
fix(Help): prevents invoking <cmd> help help and displaying incorrect…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
kbknapp committed Jun 24, 2016
1 parent 08ad1cf commit e3d2893
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/app/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ impl<'a, 'b> Parser<'a, 'b>

pub fn add_subcommand(&mut self, mut subcmd: App<'a, 'b>) {
debugln!("fn=Parser::add_subcommand;");
debugln!("Term widnth...{:?}", self.p.meta.term_w);
debugln!("Term widnth...{:?}", self.meta.term_w);
subcmd.p.meta.term_w = self.meta.term_w;
debug!("Is help...");
if subcmd.p.meta.name == "help" {
Expand Down Expand Up @@ -520,9 +520,13 @@ impl<'a, 'b> Parser<'a, 'b>
if &*arg_os == "help" &&
self.settings.is_set(AppSettings::NeedsSubcommandHelp) {
let cmds: Vec<OsString> = it.map(|c| c.into()).collect();
let mut help_help = false;
let mut sc = {
let mut sc: &Parser = self;
for (i, cmd) in cmds.iter().enumerate() {
if &*cmd.to_string_lossy() == "help" { // cmd help help
help_help = true;
}
if let Some(c) = sc.subcommands
.iter()
.filter(|s| &*s.p.meta.name == cmd)
Expand Down Expand Up @@ -563,7 +567,17 @@ impl<'a, 'b> Parser<'a, 'b>
}
sc.clone()
};
sc.create_help_and_version();
if help_help {
let mut pb = PosBuilder::new("subcommand", 1);
pb.help = Some("The subcommand whose help message to display");
pb.set(ArgSettings::Multiple);
sc.positionals.insert(1, pb);
for s in self.g_settings.clone() {
sc.set(s);
}
} else {
sc.create_help_and_version();
}
if sc.meta.bin_name != self.meta.bin_name {
sc.meta.bin_name = Some(format!("{}{}{}",
self.meta
Expand Down

0 comments on commit e3d2893

Please sign in to comment.