File tree Expand file tree Collapse file tree 4 files changed +21
-10
lines changed Expand file tree Collapse file tree 4 files changed +21
-10
lines changed Original file line number Diff line number Diff line change @@ -376,8 +376,8 @@ impl ArgAction {
376
376
match self {
377
377
Self :: Set => ValueRange :: FULL ,
378
378
Self :: Append => ValueRange :: FULL ,
379
- Self :: SetTrue => ValueRange :: EMPTY ,
380
- Self :: SetFalse => ValueRange :: EMPTY ,
379
+ Self :: SetTrue => ValueRange :: OPTIONAL ,
380
+ Self :: SetFalse => ValueRange :: OPTIONAL ,
381
381
Self :: Count => ValueRange :: EMPTY ,
382
382
Self :: Help => ValueRange :: EMPTY ,
383
383
Self :: HelpShort => ValueRange :: EMPTY ,
Original file line number Diff line number Diff line change @@ -18,6 +18,12 @@ impl ValueRange {
18
18
end_inclusive : 1 ,
19
19
} ;
20
20
21
+ #[ cfg( debug_assertions) ]
22
+ pub ( crate ) const OPTIONAL : Self = Self {
23
+ start_inclusive : 0 ,
24
+ end_inclusive : 1 ,
25
+ } ;
26
+
21
27
pub ( crate ) const FULL : Self = Self {
22
28
start_inclusive : 0 ,
23
29
end_inclusive : usize:: MAX ,
Original file line number Diff line number Diff line change @@ -225,19 +225,25 @@ fn leading_dash_stripped() {
225
225
226
226
#[ test]
227
227
fn optional_value ( ) {
228
- let cmd = Command :: new ( "flag" ) . args ( [ arg ! ( -f --flag "some flag" ) . action ( ArgAction :: SetTrue ) ] ) ;
228
+ let cmd = Command :: new ( "flag" ) . args ( [ arg ! ( -f --flag "some flag" )
229
+ . action ( ArgAction :: SetTrue )
230
+ . num_args ( 0 ..=1 ) ] ) ;
229
231
230
232
let m = cmd. clone ( ) . try_get_matches_from ( vec ! [ "" ] ) . unwrap ( ) ;
231
233
assert ! ( !* m. get_one:: <bool >( "flag" ) . expect( "defaulted by clap" ) ) ;
232
234
233
235
let m = cmd. clone ( ) . try_get_matches_from ( vec ! [ "" , "-f" ] ) . unwrap ( ) ;
234
236
assert ! ( * m. get_one:: <bool >( "flag" ) . expect( "defaulted by clap" ) ) ;
235
237
236
- cmd. clone ( )
237
- . try_get_matches_from ( vec ! [ "" , "-f" , "true" ] )
238
- . unwrap_err ( ) ;
239
-
240
- cmd. clone ( )
238
+ let m = cmd
239
+ . clone ( )
241
240
. try_get_matches_from ( vec ! [ "" , "-f" , "false" ] )
242
- . unwrap_err ( ) ;
241
+ . unwrap ( ) ;
242
+ assert ! ( !* m. get_one:: <bool >( "flag" ) . expect( "defaulted by clap" ) ) ;
243
+
244
+ let m = cmd
245
+ . clone ( )
246
+ . try_get_matches_from ( vec ! [ "" , "-f" , "true" ] )
247
+ . unwrap ( ) ;
248
+ assert ! ( * m. get_one:: <bool >( "flag" ) . expect( "defaulted by clap" ) ) ;
243
249
}
Original file line number Diff line number Diff line change @@ -331,7 +331,6 @@ fn unit_for_negation() {
331
331
}
332
332
333
333
#[ test]
334
- #[ should_panic = "Argument `alice`'s action SetTrue is incompatible with `num_args(0..=1)`" ]
335
334
fn optional_value_flag ( ) {
336
335
#[ derive( Parser , PartialEq , Eq , Debug ) ]
337
336
struct Opt {
You can’t perform that action at this time.
0 commit comments