Skip to content

Commit

Permalink
Fix --help help text in edge case
Browse files Browse the repository at this point in the history
  • Loading branch information
not-my-profile committed Feb 15, 2023
1 parent ad5d676 commit de8a410
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
13 changes: 7 additions & 6 deletions src/builder/command.rs
Expand Up @@ -4593,12 +4593,13 @@ impl Command {
// specified by the user is sent through. If hide_short_help is not included,
// then items specified with hidden_short_help will also be hidden.
let should_long = |v: &Arg| {
v.get_long_help().is_some()
|| v.is_hide_long_help_set()
|| v.is_hide_short_help_set()
|| v.get_possible_values()
.iter()
.any(PossibleValue::should_show_help)
!v.is_hide_set()
&& (v.get_long_help().is_some()
|| v.is_hide_long_help_set()
|| v.is_hide_short_help_set()
|| v.get_possible_values()
.iter()
.any(PossibleValue::should_show_help))
};

// Subcommands aren't checked because we prefer short help for them, deferring to
Expand Down
26 changes: 25 additions & 1 deletion tests/builder/hidden_args.rs
@@ -1,6 +1,6 @@
use super::utils;

use clap::{arg, Arg, ArgAction, Command};
use clap::{arg, builder::PossibleValue, Arg, ArgAction, Command};

static HIDDEN_ARGS: &str = "\
tests stuff
Expand Down Expand Up @@ -278,3 +278,27 @@ fn hide_subcmds_only() {

utils::assert_output(cmd, "test --help", HIDDEN_SUBCMDS_ONLY, false);
}

#[test]
fn hidden_arg_with_possible_value_with_help() {
// The help text of possible values is displayed for --help but not for -h.
// This is indicated by appending `(see more with '--help')` or `(see a summary with '-h')`
// to the help text of the help option if any possible value has a help (that isn't hidden).
// This test asserts that this indicator isn't shown when the whole argument is hidden.
static POS_VALS_HELP: &str = "\
Usage: ctest
Options:
-h, --help Print help
";
let app = Command::new("ctest").arg(
Arg::new("pos")
.hide(true)
.value_parser([
PossibleValue::new("fast"),
PossibleValue::new("slow").help("not as fast"),
])
.action(ArgAction::Set),
);
utils::assert_output(app, "ctest --help", POS_VALS_HELP, false);
}

0 comments on commit de8a410

Please sign in to comment.