2887: fix(help): Partial fix for 'help help' r=pksunkara a=epage



Co-authored-by: Ed Page <eopage@gmail.com>
This commit is contained in:
bors[bot] 2021-10-15 23:38:17 +00:00 committed by GitHub
commit afd9efc108
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 67 additions and 22 deletions

View file

@ -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 = {
@ -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)))
}

View file

@ -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: