Skip to content

Commit

Permalink
fix(help): Render partially optional values with []
Browse files Browse the repository at this point in the history
Fixes: #4847
  • Loading branch information
fabianfreyer committed May 15, 2023
1 parent 3fa7b8f commit e0a43cd
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
6 changes: 5 additions & 1 deletion clap_builder/src/builder/arg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4364,7 +4364,11 @@ impl Arg {

debug_assert!(self.is_takes_value_set());
for (n, val_name) in val_names.iter().enumerate() {
let arg_name = if self.is_positional() && (num_vals.min_values() == 0 || !required) {
let all_optional = self.get_min_vals() == 0;
let arg_name = if match self.is_positional() {
true => num_vals.min_values() == 0 || !required,
false => !all_optional && (n + 1 > num_vals.min_values()),
} {
format!("[{val_name}]")
} else {
format!("<{val_name}>")
Expand Down
21 changes: 21 additions & 0 deletions tests/builder/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2845,3 +2845,24 @@ fn display_name_subcommand_explicit() {
Some("child.display")
);
}

#[test]
fn issue_4847_usage() {
static USAGE_WITH_GROUP: &str = "\
Usage: deno [OPTIONS]
Options:
--example <REQUIRED> [OPTIONAL] issue 4847
-h, --help Print help
";

let cmd = clap::Command::new("hello").bin_name("deno").arg(
Arg::new("example")
.long("example")
.num_args(1..=2)
.help("issue 4847")
.value_names(&["REQUIRED", "OPTIONAL"]),
);

utils::assert_output(cmd, "deno --help", USAGE_WITH_GROUP, false);
}

0 comments on commit e0a43cd

Please sign in to comment.