Skip to content

Commit

Permalink
fix(help): Clarify short vs long help
Browse files Browse the repository at this point in the history
In reviewing CLIs for clap-rs#4132, I found some were providing helps on `-h`
vs `--help` and figured that could be built directly into clap.  I had
considered not making this hint automatic but I figured the overhead of
checking if long exists wouldn't be too bad.  The code exists (no binary
size increase) and just a simple iteration is probably not too slow
compared to everything else.

Fixes clap-rs#1015
  • Loading branch information
epage committed Aug 31, 2022
1 parent 75e340a commit c1c269b
Show file tree
Hide file tree
Showing 11 changed files with 44 additions and 28 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ Deprecated
- *(help)* Show all `[positional]` in usage
- *(help)* Polish up subcommands by referring to them as commands
- *(help)* Trim extra whitespace to avoid artifacts from different uses of templates
- *(help)* Hint to the user the difference between `-h` / `--help` when applicable (#4159)
- *(version)* Use `Command::display_name` rather than `Command::bin_name`
- *(parser)* Assert on unknown args when using external subcommands (#3703)
- *(parser)* Always fill in `""` argument for external subcommands (#3263)
Expand Down
2 changes: 1 addition & 1 deletion clap_mangen/tests/snapshots/possible_values.bash.roff
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ slow: use the slow method
.RE
.TP
/fB/-h/fR, /fB/-/-help/fR
Print help information
Print help information (use `/-h` for a summary)
.TP
[/fIpositional_choice/fR]
Pick the Position you want the command to run in
Expand Down
2 changes: 1 addition & 1 deletion clap_mangen/tests/snapshots/value_env.bash.roff
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ May also be specified with the /fBCONFIG_FILE/fR environment variable.
.RE
.TP
/fB/-h/fR, /fB/-/-help/fR
Print help information
Print help information (use `/-h` for a summary)
29 changes: 22 additions & 7 deletions src/builder/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ pub struct Command {
subcommand_value_name: Option<Str>,
subcommand_heading: Option<Str>,
external_value_parser: Option<super::ValueParser>,
long_help_exists: bool,
}

/// # Basic API
Expand Down Expand Up @@ -4124,13 +4125,21 @@ impl Command {
pub(crate) fn _check_help_and_version(&mut self) {
debug!("Command::_check_help_and_version:{}", self.name,);

self.long_help_exists = self.long_help_exists_();

if !self.is_disable_help_flag_set() {
debug!("Command::_check_help_and_version: Building default --help");
let arg = Arg::new(Id::HELP)
let mut arg = Arg::new(Id::HELP)
.short('h')
.long("help")
.action(ArgAction::Help)
.help("Print help information");
.action(ArgAction::Help);
if self.long_help_exists {
arg = arg
.help("Print help information (use `--help` for more detail)")
.long_help("Print help information (use `-h` for a summary)");
} else {
arg = arg.help("Print help information");
}
// Avoiding `arg_internal` to not be sensitive to `next_help_heading` /
// `next_display_order`
self.args.push(arg);
Expand Down Expand Up @@ -4454,10 +4463,10 @@ impl Command {
debug!(
"Command::write_help_err: {}, use_long={:?}",
self.get_display_name().unwrap_or_else(|| self.get_name()),
use_long && self.use_long_help(),
use_long && self.long_help_exists(),
);

use_long = use_long && self.use_long_help();
use_long = use_long && self.long_help_exists();
let usage = Usage::new(self);

let mut styled = StyledStr::new();
Expand All @@ -4473,8 +4482,13 @@ impl Command {
styled
}

pub(crate) fn use_long_help(&self) -> bool {
debug!("Command::use_long_help");
pub(crate) fn long_help_exists(&self) -> bool {
debug!("Command::long_help_exists: {}", self.long_help_exists);
self.long_help_exists
}

fn long_help_exists_(&self) -> bool {
debug!("Command::long_help_exists");
// In this case, both must be checked. This allows the retention of
// original formatting, but also ensures that the actual -h or --help
// specified by the user is sent through. If hide_short_help is not included,
Expand Down Expand Up @@ -4545,6 +4559,7 @@ impl Default for Command {
subcommand_value_name: Default::default(),
subcommand_heading: Default::default(),
external_value_parser: Default::default(),
long_help_exists: false,
}
}
}
Expand Down
20 changes: 10 additions & 10 deletions tests/builder/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ Usage:
clap-test
Options:
-h, --help Print help information
-h, --help Print help information (use `--help` for more detail)
-V, --version Print version information
some text that comes after the help
Expand All @@ -288,7 +288,7 @@ Usage:
Options:
-h, --help
Print help information
Print help information (use `-h` for a summary)
-V, --version
Print version information
Expand Down Expand Up @@ -494,7 +494,7 @@ Options:
- second: short help
-h, --help
Print help information
Print help information (use `-h` for a summary)
";
let cmd = Command::new("test")
.term_width(67)
Expand Down Expand Up @@ -678,7 +678,7 @@ Options:
A coffeehouse, coffee shop, or café.
-h, --help
Print help information
Print help information (use `-h` for a summary)
-V, --version
Print version information
Expand Down Expand Up @@ -1021,7 +1021,7 @@ Arguments:
Options:
-h, --help
Print help information
Print help information (use `-h` for a summary)
-V, --version
Print version information
Expand Down Expand Up @@ -1743,7 +1743,7 @@ Usage:
test [OPTIONS] --song <song> --song-volume <volume>
Options:
-h, --help Print help information
-h, --help Print help information (use `--help` for more detail)
-V, --version Print version information
OVERRIDE SPECIAL:
Expand Down Expand Up @@ -1796,7 +1796,7 @@ Usage:
Options:
-h, --help
Print help information
Print help information (use `-h` for a summary)
-V, --version
Print version information
Expand All @@ -1820,7 +1820,7 @@ Usage:
ctest foo
Options:
-h, --help Print help information
-h, --help Print help information (use `--help` for more detail)
-V, --version Print version information
";

Expand All @@ -1847,7 +1847,7 @@ Arguments:
Options:
-f
-h, --help Print help information
-h, --help Print help information (use `--help` for more detail)
";

let cmd = Command::new("demo")
Expand Down Expand Up @@ -2006,7 +2006,7 @@ Options:
and on, so I'll stop now.
-h, --help
Print help information
Print help information (use `-h` for a summary)
";

let cmd = Command::new("prog").arg(
Expand Down
8 changes: 4 additions & 4 deletions tests/builder/hidden_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Usage:
Options:
-v, --visible This text should be visible
-h, --help Print help information
-h, --help Print help information (use `--help` for more detail)
-V, --version Print version information
";

Expand Down Expand Up @@ -86,7 +86,7 @@ Options:
This text should be visible
-h, --help
Print help information
Print help information (use `-h` for a summary)
-V, --version
Print version information
Expand Down Expand Up @@ -125,7 +125,7 @@ Options:
This text should be visible
-h, --help
Print help information
Print help information (use `-h` for a summary)
-V, --version
Print version information
Expand Down Expand Up @@ -164,7 +164,7 @@ Usage:
Options:
-c, --config Some help text describing the --config arg
-v, --visible This text should be visible
-h, --help Print help information
-h, --help Print help information (use `--help` for more detail)
-V, --version Print version information
";

Expand Down
2 changes: 1 addition & 1 deletion tests/derive/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ Arguments:
Options:
-h, --help
Print help information
Print help information (use `-h` for a summary)
";

/// Application help
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/arg_required_else_help_stderr.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ Commands:
Options:
--verbose log
-h, --help Print help information
-h, --help Print help information (use `--help` for more detail)
-V, --version Print version information
"""
2 changes: 1 addition & 1 deletion tests/ui/h_flag_stdout.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Commands:
Options:
--verbose log
-h, --help Print help information
-h, --help Print help information (use `--help` for more detail)
-V, --version Print version information
"""
stderr = ""
2 changes: 1 addition & 1 deletion tests/ui/help_cmd_stdout.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Options:
more log
-h, --help
Print help information
Print help information (use `-h` for a summary)
-V, --version
Print version information
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/help_flag_stdout.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Options:
more log
-h, --help
Print help information
Print help information (use `-h` for a summary)
-V, --version
Print version information
Expand Down

0 comments on commit c1c269b

Please sign in to comment.