File tree 2 files changed +27
-2
lines changed
2 files changed +27
-2
lines changed Original file line number Diff line number Diff line change @@ -4364,7 +4364,11 @@ impl Arg {
4364
4364
4365
4365
debug_assert ! ( self . is_takes_value_set( ) ) ;
4366
4366
for ( n, val_name) in val_names. iter ( ) . enumerate ( ) {
4367
- let arg_name = if self . is_positional ( ) && ( num_vals. min_values ( ) == 0 || !required) {
4367
+ let all_optional = self . get_min_vals ( ) == 0 ;
4368
+ let arg_name = if match self . is_positional ( ) {
4369
+ true => num_vals. min_values ( ) == 0 || !required,
4370
+ false => !all_optional && ( n + 1 > num_vals. min_values ( ) ) ,
4371
+ } {
4368
4372
format ! ( "[{val_name}]" )
4369
4373
} else {
4370
4374
format ! ( "<{val_name}>" )
@@ -4646,7 +4650,7 @@ mod test {
4646
4650
. value_names ( [ "file" , "name" ] ) ;
4647
4651
o. _build ( ) ;
4648
4652
4649
- assert_eq ! ( o. to_string( ) , "-o <file> < name> ..." ) ;
4653
+ assert_eq ! ( o. to_string( ) , "-o <file> [ name] ..." ) ;
4650
4654
}
4651
4655
4652
4656
#[ 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