Skip to content

Commit

Permalink
Merge #2887
Browse files Browse the repository at this point in the history
2887: fix(help): Partial fix for 'help help' r=pksunkara a=epage



Co-authored-by: Ed Page <eopage@gmail.com>
  • Loading branch information
bors[bot] and epage committed Oct 15, 2021
2 parents 2795ccd + 81e877c commit afd9efc
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 22 deletions.
33 changes: 11 additions & 22 deletions src/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -892,7 +892,6 @@ impl<'help, 'app> Parser<'help, 'app> {
fn parse_help_subcommand(&self, cmds: &[OsString]) -> ClapResult<ParseResult> {
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 = {
Expand All @@ -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()) {
Expand All @@ -924,31 +917,27 @@ 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);
}

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)))
}

Expand Down
56 changes: 56 additions & 0 deletions tests/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:
<SUBCOMMAND>... 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:
<SUBCOMMAND>... 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:
Expand Down

0 comments on commit afd9efc

Please sign in to comment.