File tree 2 files changed +35
-2
lines changed
2 files changed +35
-2
lines changed Original file line number Diff line number Diff line change @@ -4363,8 +4363,20 @@ impl Arg {
4363
4363
}
4364
4364
4365
4365
debug_assert ! ( self . is_takes_value_set( ) ) ;
4366
+
4367
+ let is_required = |n| match self . is_positional ( ) {
4368
+ true => required && ( num_vals. min_values ( ) != 0 ) ,
4369
+ false => {
4370
+ // If all values are optional, the [] get rendered by the caller:
4371
+ // --foo[=<bar>]
4372
+ // In this case, treat as required.
4373
+ let required = self . get_min_vals ( ) == 0 ;
4374
+ required || ( n < num_vals. min_values ( ) )
4375
+ }
4376
+ } ;
4377
+
4366
4378
for ( n, val_name) in val_names. iter ( ) . enumerate ( ) {
4367
- let arg_name = if self . is_positional ( ) && ( num_vals . min_values ( ) == 0 || !required ) {
4379
+ let arg_name = if ! is_required ( n ) {
4368
4380
format ! ( "[{val_name}]" )
4369
4381
} else {
4370
4382
format ! ( "<{val_name}>" )
@@ -4646,7 +4658,7 @@ mod test {
4646
4658
. value_names ( [ "file" , "name" ] ) ;
4647
4659
o. _build ( ) ;
4648
4660
4649
- assert_eq ! ( o. to_string( ) , "-o <file> < name> ..." ) ;
4661
+ assert_eq ! ( o. to_string( ) , "-o <file> [ name] ..." ) ;
4650
4662
}
4651
4663
4652
4664
#[ test]
Original file line number Diff line number Diff line change @@ -2845,3 +2845,24 @@ fn display_name_subcommand_explicit() {
2845
2845
Some ( "child.display" )
2846
2846
) ;
2847
2847
}
2848
+
2849
+ #[ test]
2850
+ fn issue_4847_usage ( ) {
2851
+ static USAGE_WITH_GROUP : & str = "\
2852
+ Usage: deno [OPTIONS]
2853
+
2854
+ Options:
2855
+ --example <REQUIRED> [OPTIONAL] issue 4847
2856
+ -h, --help Print help
2857
+ " ;
2858
+
2859
+ let cmd = clap:: Command :: new ( "hello" ) . bin_name ( "deno" ) . arg (
2860
+ Arg :: new ( "example" )
2861
+ . long ( "example" )
2862
+ . num_args ( 1 ..=2 )
2863
+ . help ( "issue 4847" )
2864
+ . value_names ( & [ "REQUIRED" , "OPTIONAL" ] ) ,
2865
+ ) ;
2866
+
2867
+ utils:: assert_output ( cmd, "deno --help" , USAGE_WITH_GROUP , false ) ;
2868
+ }
You can’t perform that action at this time.
0 commit comments