Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(help): Render partially optional values with [] #4910

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions clap_builder/src/builder/arg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4363,8 +4363,19 @@ 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 value_is_required = if self.is_positional() {
required && (num_vals.min_values() != 0)
} else {
// If all values are optional, the [] get rendered by the caller:
// --foo[=<bar>]
// In this case, treat as required.
let required = self.get_min_vals() == 0;
required || (n < num_vals.min_values())
};

let arg_name = if !value_is_required {
format!("[{val_name}]")
} else {
format!("<{val_name}>")
Expand Down Expand Up @@ -4646,7 +4657,7 @@ mod test {
.value_names(["file", "name"]);
o._build();

assert_eq!(o.to_string(), "-o <file> <name>...");
assert_eq!(o.to_string(), "-o <file> [name]...");
}

#[test]
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);
}