@@ -4,28 +4,38 @@ use super::utils;
4
4
5
5
#[ test]
6
6
fn single_short_arg_without_value ( ) {
7
- let cmd = Command :: new ( "cmd" ) . ignore_errors ( true ) . arg ( arg ! (
8
- -c --config <FILE > "Sets a custom config file"
9
- ) ) ;
7
+ let cmd = Command :: new ( "cmd" )
8
+ . ignore_errors ( true )
9
+ . arg ( arg ! (
10
+ -c --config <FILE > "Sets a custom config file"
11
+ ) )
12
+ . arg ( arg ! ( --"unset-flag" ) ) ;
10
13
11
14
let r = cmd. try_get_matches_from ( vec ! [ "cmd" , "-c" /* missing: , "config file" */ ] ) ;
12
15
13
16
assert ! ( r. is_ok( ) , "unexpected error: {r:?}" ) ;
14
17
let m = r. unwrap ( ) ;
15
18
assert ! ( m. contains_id( "config" ) ) ;
19
+ assert_eq ! ( m. get_one:: <String >( "config" ) . cloned( ) , None ) ;
20
+ assert_eq ! ( m. get_one:: <bool >( "unset-flag" ) . copied( ) , Some ( false ) ) ;
16
21
}
17
22
18
23
#[ test]
19
24
fn single_long_arg_without_value ( ) {
20
- let cmd = Command :: new ( "cmd" ) . ignore_errors ( true ) . arg ( arg ! (
21
- -c --config <FILE > "Sets a custom config file"
22
- ) ) ;
25
+ let cmd = Command :: new ( "cmd" )
26
+ . ignore_errors ( true )
27
+ . arg ( arg ! (
28
+ -c --config <FILE > "Sets a custom config file"
29
+ ) )
30
+ . arg ( arg ! ( --"unset-flag" ) ) ;
23
31
24
32
let r = cmd. try_get_matches_from ( vec ! [ "cmd" , "--config" /* missing: , "config file" */ ] ) ;
25
33
26
34
assert ! ( r. is_ok( ) , "unexpected error: {r:?}" ) ;
27
35
let m = r. unwrap ( ) ;
28
36
assert ! ( m. contains_id( "config" ) ) ;
37
+ assert_eq ! ( m. get_one:: <String >( "config" ) . cloned( ) , None ) ;
38
+ assert_eq ! ( m. get_one:: <bool >( "unset-flag" ) . copied( ) , Some ( false ) ) ;
29
39
}
30
40
31
41
#[ test]
@@ -38,7 +48,8 @@ fn multiple_args_and_final_arg_without_value() {
38
48
. arg ( arg ! (
39
49
-x --stuff <FILE > "Sets a custom stuff file"
40
50
) )
41
- . arg ( arg ! ( f: -f "Flag" ) . action ( ArgAction :: SetTrue ) ) ;
51
+ . arg ( arg ! ( f: -f "Flag" ) . action ( ArgAction :: SetTrue ) )
52
+ . arg ( arg ! ( --"unset-flag" ) ) ;
42
53
43
54
let r = cmd. try_get_matches_from ( vec ! [
44
55
"cmd" , "-c" , "file" , "-f" , "-x" , /* missing: , "some stuff" */
@@ -50,8 +61,9 @@ fn multiple_args_and_final_arg_without_value() {
50
61
m. get_one:: <String >( "config" ) . map( |v| v. as_str( ) ) ,
51
62
Some ( "file" )
52
63
) ;
53
- assert ! ( * m. get_one:: <bool >( "f" ) . expect ( "defaulted by clap" ) ) ;
64
+ assert_eq ! ( m. get_one:: <bool >( "f" ) . copied ( ) , Some ( true ) ) ;
54
65
assert_eq ! ( m. get_one:: <String >( "stuff" ) . map( |v| v. as_str( ) ) , None ) ;
66
+ assert_eq ! ( m. get_one:: <bool >( "unset-flag" ) . copied( ) , Some ( false ) ) ;
55
67
}
56
68
57
69
#[ test]
@@ -64,7 +76,8 @@ fn multiple_args_and_intermittent_arg_without_value() {
64
76
. arg ( arg ! (
65
77
-x --stuff <FILE > "Sets a custom stuff file"
66
78
) )
67
- . arg ( arg ! ( f: -f "Flag" ) . action ( ArgAction :: SetTrue ) ) ;
79
+ . arg ( arg ! ( f: -f "Flag" ) . action ( ArgAction :: SetTrue ) )
80
+ . arg ( arg ! ( --"unset-flag" ) ) ;
68
81
69
82
let r = cmd. try_get_matches_from ( vec ! [
70
83
"cmd" , "-x" , /* missing: ,"some stuff" */
@@ -77,8 +90,9 @@ fn multiple_args_and_intermittent_arg_without_value() {
77
90
m. get_one:: <String >( "config" ) . map( |v| v. as_str( ) ) ,
78
91
Some ( "file" )
79
92
) ;
80
- assert ! ( * m. get_one:: <bool >( "f" ) . expect ( "defaulted by clap" ) ) ;
93
+ assert_eq ! ( m. get_one:: <bool >( "f" ) . copied ( ) , Some ( true ) ) ;
81
94
assert_eq ! ( m. get_one:: <String >( "stuff" ) . map( |v| v. as_str( ) ) , None ) ;
95
+ assert_eq ! ( m. get_one:: <bool >( "unset-flag" ) . copied( ) , Some ( false ) ) ;
82
96
}
83
97
84
98
#[ test]
@@ -100,9 +114,11 @@ fn subcommand() {
100
114
. long ( "stuff" )
101
115
. action ( ArgAction :: Set )
102
116
. help ( "stuf value" ) ,
103
- ) ,
117
+ )
118
+ . arg ( arg ! ( --"unset-flag" ) ) ,
104
119
)
105
- . arg ( Arg :: new ( "other" ) . long ( "other" ) ) ;
120
+ . arg ( Arg :: new ( "other" ) . long ( "other" ) )
121
+ . arg ( arg ! ( --"unset-flag" ) ) ;
106
122
107
123
let m = cmd
108
124
. try_get_matches_from ( vec ! [
@@ -125,6 +141,9 @@ fn subcommand() {
125
141
sub_m. get_one:: <String >( "stuff" ) . map( |v| v. as_str( ) ) ,
126
142
Some ( "some other val" )
127
143
) ;
144
+ assert_eq ! ( sub_m. get_one:: <bool >( "unset-flag" ) . copied( ) , Some ( false ) ) ;
145
+
146
+ assert_eq ! ( m. get_one:: <bool >( "unset-flag" ) . copied( ) , Some ( false ) ) ;
128
147
}
129
148
130
149
#[ test]
0 commit comments