Repository: clap-rs/clap
Pull request #4002 (2022-07-29 14:44:40)

fix(help): Remove '...' for optional values
<!--
Thanks for helping out!

Please link the appropriate issue from your PR.

If you don't have an issue, we'd recommend starting with one first so the PR can focus on the
implementation (unless its an obvious bug or documentation fix that will have
little conversation).
-->



Patch:
diff --git a/src/builder/arg.rs b/src/builder/arg.rs
index ff5a96f1..0df3489e 100644
--- a/src/builder/arg.rs
+++ b/src/builder/arg.rs
@@ -4554,6 +4554,7 @@ pub(crate) fn render_arg_val(arg: &Arg) -> String {
             rendered.push_str(&arg_name);
         }
         extra_values |= arg.num_vals.is_none() && arg.is_multiple_values_set();
+        extra_values |= min < num_vals.max_values();
     } else {
         debug_assert!(1 < val_names.len());
         for (n, val_name) in val_names.iter().enumerate() {
@@ -4565,7 +4566,6 @@ pub(crate) fn render_arg_val(arg: &Arg) -> String {
         }
         extra_values |= val_names.len() < num_vals.max_values();
     }
-    extra_values |= !num_vals.is_fixed();
     if arg.is_positional() {
         if matches!(*arg.get_action(), ArgAction::Append) {
             extra_values = true;
@@ -4712,6 +4712,17 @@ mod test {
         assert_eq!(o.to_string(), "-o <file>...");
     }
 
+    #[test]
+    fn option_display_optional_value() {
+        let mut o = Arg::new("opt")
+            .long("option")
+            .action(ArgAction::Set)
+            .number_of_values(0..=1);
+        o._build();
+
+        assert_eq!(o.to_string(), "--option [<opt>]");
+    }
+
     #[test]
     fn option_display_value_names() {
         let mut o = Arg::new("opt")
@@ -4807,6 +4818,17 @@ mod test {
         assert_eq!(p.to_string(), "<pos>...");
     }
 
+    #[test]
+    fn positional_display_optional_value() {
+        let mut p = Arg::new("pos")
+            .index(1)
+            .number_of_values(0..=1)
+            .action(ArgAction::Set);
+        p._build();
+
+        assert_eq!(p.to_string(), "[<pos>]");
+    }
+
     #[test]
     fn positional_display_multiple_occurrences() {
         let mut p = Arg::new("pos").index(1).action(ArgAction::Append);


Test patch:
diff --git a/tests/builder/help.rs b/tests/builder/help.rs
index 188969e8..126fc9b8 100644
--- a/tests/builder/help.rs
+++ b/tests/builder/help.rs
@@ -227,8 +227,8 @@ OPTIONS:
         --multvalsmo <one> <two>             Tests multiple values, and mult occs
         --minvals2 <minvals> <minvals>...    Tests 2 min vals
         --maxvals3 <maxvals>...              Tests 3 max vals
-        --optvaleq[=<optval>...]             Tests optional value, require = sign
-        --optvalnoeq [<optval>...]           Tests optional value
+        --optvaleq[=<optval>]                Tests optional value, require = sign
+        --optvalnoeq [<optval>]              Tests optional value
     -h, --help                               Print help information
     -V, --version                            Print version information
 
diff --git a/tests/builder/multiple_values.rs b/tests/builder/multiple_values.rs
index 8efba4f9..5b11141f 100644
--- a/tests/builder/multiple_values.rs
+++ b/tests/builder/multiple_values.rs
@@ -435,8 +435,8 @@ USAGE:
     test [OPTIONS]
 
 OPTIONS:
-    -p [<NUM>...]        
-    -h, --help           Print help information
+    -p [<NUM>]        
+    -h, --help        Print help information
 ";
     snapbox::assert_eq(HELP, help);
 }
diff --git a/tests/derive/options.rs b/tests/derive/options.rs
index 7e2dd4ef..1ebbf6d7 100644
--- a/tests/derive/options.rs
+++ b/tests/derive/options.rs
@@ -271,7 +271,8 @@ fn option_option_type_help() {
         arg: Option<Option<i32>>,
     }
     let help = utils::get_help::<Opt>();
-    assert!(help.contains("--arg [<val>...]"));
+    assert!(help.contains("--arg [<val>]"));
+    assert!(!help.contains("--arg [<val>...]"));
 }
 
 #[test]
diff --git a/tests/macros.rs b/tests/macros.rs
index f95153e4..3fdabeb3 100644
--- a/tests/macros.rs
+++ b/tests/macros.rs
@@ -229,8 +229,8 @@ USAGE:
     test [OPTIONS]
 
 OPTIONS:
-    -p [<NUM>...]        
-    -h, --help           Print help information
+    -p [<NUM>]        
+    -h, --help        Print help information
 ";
         snapbox::assert_eq(HELP, help);
     }