Skip to content

Commit 473cf17

Browse files
fix(help): Fix --help help text in edge case (#4710)
We were accidentally showing long help just because a hidden argument had a description for a possible value. This is most likely to be hit with the derive API as the value descriptions are automatically applied from documentation.
1 parent 62da8f9 commit 473cf17

File tree

2 files changed

+32
-7
lines changed

2 files changed

+32
-7
lines changed

src/builder/command.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -4593,12 +4593,13 @@ impl Command {
45934593
// specified by the user is sent through. If hide_short_help is not included,
45944594
// then items specified with hidden_short_help will also be hidden.
45954595
let should_long = |v: &Arg| {
4596-
v.get_long_help().is_some()
4597-
|| v.is_hide_long_help_set()
4598-
|| v.is_hide_short_help_set()
4599-
|| v.get_possible_values()
4600-
.iter()
4601-
.any(PossibleValue::should_show_help)
4596+
!v.is_hide_set()
4597+
&& (v.get_long_help().is_some()
4598+
|| v.is_hide_long_help_set()
4599+
|| v.is_hide_short_help_set()
4600+
|| v.get_possible_values()
4601+
.iter()
4602+
.any(PossibleValue::should_show_help))
46024603
};
46034604

46044605
// Subcommands aren't checked because we prefer short help for them, deferring to

tests/builder/hidden_args.rs

+25-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::utils;
22

3-
use clap::{arg, Arg, ArgAction, Command};
3+
use clap::{arg, builder::PossibleValue, Arg, ArgAction, Command};
44

55
static HIDDEN_ARGS: &str = "\
66
tests stuff
@@ -278,3 +278,27 @@ fn hide_subcmds_only() {
278278

279279
utils::assert_output(cmd, "test --help", HIDDEN_SUBCMDS_ONLY, false);
280280
}
281+
282+
#[test]
283+
fn hidden_arg_with_possible_value_with_help() {
284+
// Normally the presence of a possible value with a help text triggers a
285+
// change of the --help help text by appending `(see more with '--help')`
286+
// or `(see a summary with '-h')`. When the argument is completely hidden
287+
// we however do not want it to trigger that change.
288+
static POS_VALS_HELP: &str = "\
289+
Usage: ctest
290+
291+
Options:
292+
-h, --help Print help
293+
";
294+
let app = Command::new("ctest").arg(
295+
Arg::new("pos")
296+
.hide(true)
297+
.value_parser([
298+
PossibleValue::new("fast"),
299+
PossibleValue::new("slow").help("not as fast"),
300+
])
301+
.action(ArgAction::Set),
302+
);
303+
utils::assert_output(app, "ctest --help", POS_VALS_HELP, false);
304+
}

0 commit comments

Comments
 (0)