From e9194b55e2e6febdfd1d1b3ed8a27ada974f5466 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Mon, 27 Nov 2023 09:23:38 -0600 Subject: [PATCH 1/2] test(help): Better show about bug --- tests/builder/help.rs | 126 +++++++++++++++++++++++++++++++----------- 1 file changed, 94 insertions(+), 32 deletions(-) diff --git a/tests/builder/help.rs b/tests/builder/help.rs index e8fd22cc696..20e92c3da62 100644 --- a/tests/builder/help.rs +++ b/tests/builder/help.rs @@ -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: +parent command --child -h, --help Print help parent help: +parent command [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: +parent command --child foo -h, --help Print help (see more with '--help') parent help: +parent command [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: +parent command --child bar @@ -3035,11 +3048,13 @@ parent test: Print help (see a summary with '-h') parent help: +parent command [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: +parent command --child bar @@ -3077,11 +3095,13 @@ parent test: Print help (see a summary with '-h') parent help: +parent command [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: +parent command --child -h, --help Print help parent help: +parent command [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 test --child parent --parent help [COMMAND]... @@ -3138,18 +3165,21 @@ Options: -h, --help Print help parent --parent test: +parent command --child -h, --help Print help parent --parent help: +parent command [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 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: +parent command --child -h, --help Print help parent help: +parent command [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: +parent command --child -h, --help Print help parent help: +parent command [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: +parent command --child -h, --help Print help parent help: +parent command [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: +parent command --child1 -h, --help Print help parent child1 grandchild1: -some 1 +child1 command --grandchild1 -h, --help Print help parent child1 grandchild1 greatgrandchild1: -some 1 +grandchild1 command --greatgrandchild1 -h, --help Print help parent child1 grandchild1 greatgrandchild2: -some 1 +grandchild1 command --greatgrandchild2 -h, --help Print help parent child1 grandchild1 greatgrandchild3: -some 1 +grandchild1 command --greatgrandchild3 -h, --help Print help parent child1 grandchild1 help: -some 1 +grandchild1 command parent child1 grandchild2: -some 1 +child1 command --grandchild2 -h, --help Print help parent child1 grandchild3: -some 1 +child1 command --grandchild3 -h, --help Print help parent child1 help: -some 1 +child1 command parent child2: +parent command --child2 -h, --help Print help parent child3: +parent command --child3 -h, --help Print help parent help: +parent command [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: +parent command --child1 -h, --help Print help parent child2: +parent command --child2 -h, --help Print help parent child3: +parent command --child3 -h, --help Print help parent help: +parent command [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); From 22130e30577ba29a97f21a88a178982ef24c4cd0 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Mon, 27 Nov 2023 09:28:30 -0600 Subject: [PATCH 2/2] fix(help): Use right `about` when flattening Fixes #5226 --- clap_builder/src/output/help_template.rs | 4 +- examples/git-derive.md | 1 + examples/git.md | 1 + tests/builder/help.rs | 68 ++++++++++++------------ 4 files changed, 38 insertions(+), 36 deletions(-) diff --git a/clap_builder/src/output/help_template.rs b/clap_builder/src/output/help_template.rs index 92a039dfe1c..8c719043500 100644 --- a/clap_builder/src/output/help_template.rs +++ b/clap_builder/src/output/help_template.rs @@ -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!( diff --git a/examples/git-derive.md b/examples/git-derive.md index 442498dc603..119a1978273 100644 --- a/examples/git-derive.md +++ b/examples/git-derive.md @@ -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 diff --git a/examples/git.md b/examples/git.md index 55acc69c246..61a9403f7de 100644 --- a/examples/git.md +++ b/examples/git.md @@ -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 diff --git a/tests/builder/help.rs b/tests/builder/help.rs index 20e92c3da62..7ed6a2959fa 100644 --- a/tests/builder/help.rs +++ b/tests/builder/help.rs @@ -2963,12 +2963,12 @@ Options: -h, --help Print help parent test: -parent command +test command --child -h, --help Print help parent help: -parent command +Print this message or the help of the given subcommand(s) [COMMAND]... Print help for the subcommand(s) "; let cmd = Command::new("parent") @@ -2997,12 +2997,12 @@ Options: -h, --help Print help (see more with '--help') parent test: -parent command +test command --child foo -h, --help Print help (see more with '--help') parent help: -parent command +Print this message or the help of the given subcommand(s) [COMMAND]... Print help for the subcommand(s) "; let cmd = Command::new("parent") @@ -3040,7 +3040,7 @@ Options: Print help (see a summary with '-h') parent test: -parent command +test command --child bar @@ -3048,7 +3048,7 @@ parent command Print help (see a summary with '-h') parent help: -parent command +Print this message or the help of the given subcommand(s) [COMMAND]... Print help for the subcommand(s) "; @@ -3087,7 +3087,7 @@ Options: Print help (see a summary with '-h') parent test: -parent command +test command --child bar @@ -3095,7 +3095,7 @@ parent command Print help (see a summary with '-h') parent help: -parent command +Print this message or the help of the given subcommand(s) [COMMAND]... Print help for the subcommand(s) "; @@ -3131,12 +3131,12 @@ Options: -h, --help Print help parent test: -parent command +test command --child -h, --help Print help parent help: -parent command +Print this message or the help of the given subcommand(s) [COMMAND]... Print help for the subcommand(s) "; let cmd = Command::new("parent") @@ -3165,12 +3165,12 @@ Options: -h, --help Print help parent --parent test: -parent command +test command --child -h, --help Print help parent --parent help: -parent command +Print this message or the help of the given subcommand(s) [COMMAND]... Print help for the subcommand(s) "; let cmd = Command::new("parent") @@ -3199,12 +3199,12 @@ Options: -h, --help Print help parent test: -parent command +test command --child -h, --help Print help parent help: -parent command +Print this message or the help of the given subcommand(s) [COMMAND]... Print help for the subcommand(s) "; let cmd = Command::new("parent") @@ -3251,12 +3251,12 @@ Options: -h, --help Print help parent test: -parent command +test command --child -h, --help Print help parent help: -parent command +Print this message or the help of the given subcommand(s) [COMMAND]... Print help for the subcommand(s) "; let cmd = Command::new("parent") @@ -3286,12 +3286,12 @@ Options: -h, --help Print help parent test: -parent command +test command --child -h, --help Print help parent help: -parent command +Print this message or the help of the given subcommand(s) [COMMAND]... Print help for the subcommand(s) "; let cmd = Command::new("parent") @@ -3332,60 +3332,60 @@ Options: -h, --help Print help parent child1: -parent command +child1 command --child1 -h, --help Print help parent child1 grandchild1: -child1 command +grandchild1 command --grandchild1 -h, --help Print help parent child1 grandchild1 greatgrandchild1: -grandchild1 command +greatgrandchild1 command --greatgrandchild1 -h, --help Print help parent child1 grandchild1 greatgrandchild2: -grandchild1 command +greatgrandchild2 command --greatgrandchild2 -h, --help Print help parent child1 grandchild1 greatgrandchild3: -grandchild1 command +greatgrandchild3 command --greatgrandchild3 -h, --help Print help parent child1 grandchild1 help: -grandchild1 command +Print this message or the help of the given subcommand(s) parent child1 grandchild2: -child1 command +grandchild2 command --grandchild2 -h, --help Print help parent child1 grandchild3: -child1 command +grandchild3 command --grandchild3 -h, --help Print help parent child1 help: -child1 command +Print this message or the help of the given subcommand(s) parent child2: -parent command +child2 command --child2 -h, --help Print help parent child3: -parent command +child3 command --child3 -h, --help Print help parent help: -parent command +Print this message or the help of the given subcommand(s) [COMMAND]... Print help for the subcommand(s) "; let cmd = Command::new("parent") @@ -3458,22 +3458,22 @@ Options: -h, --help Print help parent child1: -parent command +child1 command --child1 -h, --help Print help parent child2: -parent command +child2 command --child2 -h, --help Print help parent child3: -parent command +child3 command --child3 -h, --help Print help parent help: -parent command +Print this message or the help of the given subcommand(s) [COMMAND]... Print help for the subcommand(s) "; let cmd = Command::new("parent")