Merge pull request #5227 from epage/flat

fix(help): Use right about when flattening
This commit is contained in:
Ed Page 2023-11-27 10:21:02 -06:00 committed by GitHub
commit 5c8f8d5f65
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 98 additions and 34 deletions

View file

@ -909,9 +909,9 @@ impl<'cmd, 'writer> HelpTemplate<'cmd, 'writer> {
*first = false;
let heading = subcommand.get_usage_name_fallback();
let about = cmd
let about = subcommand
.get_about()
.or_else(|| cmd.get_long_about())
.or_else(|| subcommand.get_long_about())
.unwrap_or_default();
let _ = write!(

View file

@ -95,6 +95,7 @@ git-derive[EXE] stash apply:
[STASH]
git-derive[EXE] stash help:
Print this message or the help of the given subcommand(s)
[COMMAND]... Print help for the subcommand(s)
$ git-derive stash push -h

View file

@ -93,6 +93,7 @@ git[EXE] stash apply:
[STASH]
git[EXE] stash help:
Print this message or the help of the given subcommand(s)
[COMMAND]... Print help for the subcommand(s)
$ git stash push -h

View file

@ -2952,6 +2952,8 @@ fn display_name_subcommand_explicit() {
#[test]
fn flatten_basic() {
static EXPECTED: &str = "\
parent command
Usage: parent [OPTIONS]
parent test [OPTIONS]
parent help [COMMAND]...
@ -2961,18 +2963,21 @@ Options:
-h, --help Print help
parent test:
test command
--child <child>
-h, --help Print help
parent help:
Print this message or the help of the given subcommand(s)
[COMMAND]... Print help for the subcommand(s)
";
let cmd = Command::new("parent")
.flatten_help(true)
.about("parent command")
.arg(Arg::new("parent").long("parent"))
.subcommand(
Command::new("test")
.about("some")
.about("test command")
.arg(Arg::new("child").long("child")),
);
utils::assert_output(cmd, "parent -h", EXPECTED, false);
@ -2981,6 +2986,8 @@ parent help:
#[test]
fn flatten_short_help() {
static EXPECTED: &str = "\
parent command
Usage: parent [OPTIONS]
parent test [OPTIONS]
parent help [COMMAND]...
@ -2990,14 +2997,17 @@ Options:
-h, --help Print help (see more with '--help')
parent test:
test command
--child <child> foo
-h, --help Print help (see more with '--help')
parent help:
Print this message or the help of the given subcommand(s)
[COMMAND]... Print help for the subcommand(s)
";
let cmd = Command::new("parent")
.flatten_help(true)
.about("parent command")
.arg(
Arg::new("parent")
.long("parent")
@ -3006,7 +3016,7 @@ parent help:
)
.subcommand(
Command::new("test")
.about("some")
.about("test command")
.long_about("long some")
.arg(Arg::new("child").long("child").help("foo").long_help("bar")),
);
@ -3016,6 +3026,8 @@ parent help:
#[test]
fn flatten_long_help() {
static EXPECTED: &str = "\
parent command
Usage: parent [OPTIONS]
parent test [OPTIONS]
parent help [COMMAND]...
@ -3028,6 +3040,7 @@ Options:
Print help (see a summary with '-h')
parent test:
test command
--child <child>
bar
@ -3035,11 +3048,13 @@ parent test:
Print help (see a summary with '-h')
parent help:
Print this message or the help of the given subcommand(s)
[COMMAND]...
Print help for the subcommand(s)
";
let cmd = Command::new("parent")
.flatten_help(true)
.about("parent command")
.arg(
Arg::new("parent")
.long("parent")
@ -3048,7 +3063,7 @@ parent help:
)
.subcommand(
Command::new("test")
.about("some")
.about("test command")
.long_about("long some")
.arg(Arg::new("child").long("child").help("foo").long_help("bar")),
);
@ -3058,6 +3073,8 @@ parent help:
#[test]
fn flatten_help_cmd() {
static EXPECTED: &str = "\
parent command
Usage: parent [OPTIONS]
parent test [OPTIONS]
parent help [COMMAND]...
@ -3070,6 +3087,7 @@ Options:
Print help (see a summary with '-h')
parent test:
test command
--child <child>
bar
@ -3077,11 +3095,13 @@ parent test:
Print help (see a summary with '-h')
parent help:
Print this message or the help of the given subcommand(s)
[COMMAND]...
Print help for the subcommand(s)
";
let cmd = Command::new("parent")
.flatten_help(true)
.about("parent command")
.arg(
Arg::new("parent")
.long("parent")
@ -3090,7 +3110,7 @@ parent help:
)
.subcommand(
Command::new("test")
.about("some")
.about("test command")
.long_about("long some")
.arg(Arg::new("child").long("child").help("foo").long_help("bar")),
);
@ -3100,6 +3120,8 @@ parent help:
#[test]
fn flatten_with_global() {
static EXPECTED: &str = "\
parent command
Usage: parent [OPTIONS]
parent test [OPTIONS]
parent help [COMMAND]...
@ -3109,18 +3131,21 @@ Options:
-h, --help Print help
parent test:
test command
--child <child>
-h, --help Print help
parent help:
Print this message or the help of the given subcommand(s)
[COMMAND]... Print help for the subcommand(s)
";
let cmd = Command::new("parent")
.flatten_help(true)
.about("parent command")
.arg(Arg::new("parent").long("parent").global(true))
.subcommand(
Command::new("test")
.about("some")
.about("test command")
.arg(Arg::new("child").long("child")),
);
utils::assert_output(cmd, "parent -h", EXPECTED, false);
@ -3129,6 +3154,8 @@ parent help:
#[test]
fn flatten_arg_required() {
static EXPECTED: &str = "\
parent command
Usage: parent --parent <parent>
parent --parent <parent> test --child <child>
parent --parent <parent> help [COMMAND]...
@ -3138,18 +3165,21 @@ Options:
-h, --help Print help
parent --parent <parent> test:
test command
--child <child>
-h, --help Print help
parent --parent <parent> help:
Print this message or the help of the given subcommand(s)
[COMMAND]... Print help for the subcommand(s)
";
let cmd = Command::new("parent")
.flatten_help(true)
.about("parent command")
.arg(Arg::new("parent").long("parent").required(true))
.subcommand(
Command::new("test")
.about("some")
.about("test command")
.arg(Arg::new("child").long("child").required(true)),
);
utils::assert_output(cmd, "parent -h", EXPECTED, false);
@ -3158,6 +3188,8 @@ parent --parent <parent> help:
#[test]
fn flatten_with_external_subcommand() {
static EXPECTED: &str = "\
parent command
Usage: parent [OPTIONS]
parent test [OPTIONS]
parent help [COMMAND]...
@ -3167,19 +3199,22 @@ Options:
-h, --help Print help
parent test:
test command
--child <child>
-h, --help Print help
parent help:
Print this message or the help of the given subcommand(s)
[COMMAND]... Print help for the subcommand(s)
";
let cmd = Command::new("parent")
.flatten_help(true)
.about("parent command")
.allow_external_subcommands(true)
.arg(Arg::new("parent").long("parent"))
.subcommand(
Command::new("test")
.about("some")
.about("test command")
.arg(Arg::new("child").long("child")),
);
utils::assert_output(cmd, "parent -h", EXPECTED, false);
@ -3188,6 +3223,8 @@ parent help:
#[test]
fn flatten_without_subcommands() {
static EXPECTED: &str = "\
parent command
Usage: parent [OPTIONS]
Options:
@ -3196,6 +3233,7 @@ Options:
";
let cmd = Command::new("parent")
.flatten_help(true)
.about("parent command")
.arg(Arg::new("parent").long("parent"));
utils::assert_output(cmd, "parent -h", EXPECTED, false);
}
@ -3203,6 +3241,8 @@ Options:
#[test]
fn flatten_with_subcommand_required() {
static EXPECTED: &str = "\
parent command
Usage: parent test [OPTIONS]
parent help [COMMAND]...
@ -3211,19 +3251,22 @@ Options:
-h, --help Print help
parent test:
test command
--child <child>
-h, --help Print help
parent help:
Print this message or the help of the given subcommand(s)
[COMMAND]... Print help for the subcommand(s)
";
let cmd = Command::new("parent")
.flatten_help(true)
.about("parent command")
.subcommand_required(true)
.arg(Arg::new("parent").long("parent"))
.subcommand(
Command::new("test")
.about("some")
.about("test command")
.arg(Arg::new("child").long("child")),
);
utils::assert_output(cmd, "parent -h", EXPECTED, false);
@ -3232,6 +3275,8 @@ parent help:
#[test]
fn flatten_with_args_conflicts_with_subcommands() {
static EXPECTED: &str = "\
parent command
Usage: parent [OPTIONS]
parent test [OPTIONS]
parent help [COMMAND]...
@ -3241,20 +3286,23 @@ Options:
-h, --help Print help
parent test:
test command
--child <child>
-h, --help Print help
parent help:
Print this message or the help of the given subcommand(s)
[COMMAND]... Print help for the subcommand(s)
";
let cmd = Command::new("parent")
.flatten_help(true)
.about("parent command")
.subcommand_required(true)
.args_conflicts_with_subcommands(true)
.arg(Arg::new("parent").long("parent"))
.subcommand(
Command::new("test")
.about("some")
.about("test command")
.arg(Arg::new("child").long("child")),
);
utils::assert_output(cmd, "parent -h", EXPECTED, false);
@ -3263,6 +3311,8 @@ parent help:
#[test]
fn flatten_recursive() {
static EXPECTED: &str = "\
parent command
Usage: parent [OPTIONS]
parent child1 [OPTIONS]
parent child1 grandchild1 [OPTIONS]
@ -3282,106 +3332,111 @@ Options:
-h, --help Print help
parent child1:
child1 command
--child1 <child>
-h, --help Print help
parent child1 grandchild1:
some 1
grandchild1 command
--grandchild1 <grandchild>
-h, --help Print help
parent child1 grandchild1 greatgrandchild1:
some 1
greatgrandchild1 command
--greatgrandchild1 <greatgrandchild>
-h, --help Print help
parent child1 grandchild1 greatgrandchild2:
some 1
greatgrandchild2 command
--greatgrandchild2 <greatgrandchild>
-h, --help Print help
parent child1 grandchild1 greatgrandchild3:
some 1
greatgrandchild3 command
--greatgrandchild3 <greatgrandchild>
-h, --help Print help
parent child1 grandchild1 help:
some 1
Print this message or the help of the given subcommand(s)
parent child1 grandchild2:
some 1
grandchild2 command
--grandchild2 <grandchild>
-h, --help Print help
parent child1 grandchild3:
some 1
grandchild3 command
--grandchild3 <grandchild>
-h, --help Print help
parent child1 help:
some 1
Print this message or the help of the given subcommand(s)
parent child2:
child2 command
--child2 <child>
-h, --help Print help
parent child3:
child3 command
--child3 <child>
-h, --help Print help
parent help:
Print this message or the help of the given subcommand(s)
[COMMAND]... Print help for the subcommand(s)
";
let cmd = Command::new("parent")
.flatten_help(true)
.about("parent command")
.arg(Arg::new("parent").long("parent"))
.subcommand(
Command::new("child1")
.flatten_help(true)
.about("some 1")
.about("child1 command")
.arg(Arg::new("child").long("child1"))
.subcommand(
Command::new("grandchild1")
.flatten_help(true)
.about("some 1")
.about("grandchild1 command")
.arg(Arg::new("grandchild").long("grandchild1"))
.subcommand(
Command::new("greatgrandchild1")
.about("some 1")
.about("greatgrandchild1 command")
.arg(Arg::new("greatgrandchild").long("greatgrandchild1")),
)
.subcommand(
Command::new("greatgrandchild2")
.about("some 2")
.about("greatgrandchild2 command")
.arg(Arg::new("greatgrandchild").long("greatgrandchild2")),
)
.subcommand(
Command::new("greatgrandchild3")
.about("some 3")
.about("greatgrandchild3 command")
.arg(Arg::new("greatgrandchild").long("greatgrandchild3")),
),
)
.subcommand(
Command::new("grandchild2")
.about("some 2")
.about("grandchild2 command")
.arg(Arg::new("grandchild").long("grandchild2")),
)
.subcommand(
Command::new("grandchild3")
.about("some 3")
.about("grandchild3 command")
.arg(Arg::new("grandchild").long("grandchild3")),
),
)
.subcommand(
Command::new("child2")
.about("some 2")
.about("child2 command")
.arg(Arg::new("child").long("child2")),
)
.subcommand(
Command::new("child3")
.about("some 3")
.about("child3 command")
.arg(Arg::new("child").long("child3")),
);
utils::assert_output(cmd, "parent -h", EXPECTED, false);
@ -3390,6 +3445,8 @@ parent help:
#[test]
fn flatten_not_recursive() {
static EXPECTED: &str = "\
parent command
Usage: parent [OPTIONS]
parent child1 [OPTIONS] [COMMAND]
parent child2 [OPTIONS]
@ -3401,51 +3458,56 @@ Options:
-h, --help Print help
parent child1:
child1 command
--child1 <child>
-h, --help Print help
parent child2:
child2 command
--child2 <child>
-h, --help Print help
parent child3:
child3 command
--child3 <child>
-h, --help Print help
parent help:
Print this message or the help of the given subcommand(s)
[COMMAND]... Print help for the subcommand(s)
";
let cmd = Command::new("parent")
.flatten_help(true)
.about("parent command")
.arg(Arg::new("parent").long("parent"))
.subcommand(
Command::new("child1")
.about("some 1")
.about("child1 command")
.arg(Arg::new("child").long("child1"))
.subcommand(
Command::new("grandchild1")
.about("some 1")
.about("grandchild1 command")
.arg(Arg::new("grandchild").long("grandchild1")),
)
.subcommand(
Command::new("grandchild2")
.about("some 2")
.about("grandchild2 command")
.arg(Arg::new("grandchild").long("grandchild2")),
)
.subcommand(
Command::new("grandchild3")
.about("some 3")
.about("grandchild3 command")
.arg(Arg::new("grandchild").long("grandchild3")),
),
)
.subcommand(
Command::new("child2")
.about("some 2")
.about("child2 command")
.arg(Arg::new("child").long("child2")),
)
.subcommand(
Command::new("child3")
.about("some 3")
.about("child3 command")
.arg(Arg::new("child").long("child3")),
);
utils::assert_output(cmd, "parent -h", EXPECTED, false);