@@ -4,41 +4,52 @@ 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]
32
42
fn multiple_args_and_final_arg_without_value ( ) {
33
43
let cmd = Command :: new ( "cmd" )
34
44
. ignore_errors ( true )
35
45
. arg ( arg ! (
36
- -c --config [ FILE ] "Sets a custom config file"
46
+ -c --config < FILE > "Sets a custom config file"
37
47
) )
38
48
. arg ( arg ! (
39
- -x --stuff [ FILE ] "Sets a custom stuff file"
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,21 +61,23 @@ 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]
58
70
fn multiple_args_and_intermittent_arg_without_value ( ) {
59
71
let cmd = Command :: new ( "cmd" )
60
72
. ignore_errors ( true )
61
73
. arg ( arg ! (
62
- -c --config[ FILE ] "Sets a custom config file"
74
+ -c --config < FILE > "Sets a custom config file"
63
75
) )
64
76
. arg ( arg ! (
65
- -x --stuff[ FILE ] "Sets a custom stuff file"
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,30 @@ 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 ) ) ;
96
+ }
97
+
98
+ #[ test]
99
+ fn unexpected_argument ( ) {
100
+ let cmd = Command :: new ( "cmd" )
101
+ . ignore_errors ( true )
102
+ . arg ( arg ! (
103
+ -c --config [ FILE ] "Sets a custom config file"
104
+ ) )
105
+ . arg ( arg ! ( --"unset-flag" ) ) ;
106
+
107
+ let r = cmd. try_get_matches_from ( vec ! [ "cmd" , "-c" , "config file" , "unexpected" ] ) ;
108
+
109
+ assert ! ( r. is_ok( ) , "unexpected error: {r:?}" ) ;
110
+ let m = r. unwrap ( ) ;
111
+ assert ! ( m. contains_id( "config" ) ) ;
112
+ assert_eq ! (
113
+ m. get_one:: <String >( "config" ) . cloned( ) ,
114
+ Some ( "config file" . to_owned( ) )
115
+ ) ;
116
+ assert_eq ! ( m. get_one:: <bool >( "unset-flag" ) . copied( ) , Some ( false ) ) ;
82
117
}
83
118
84
119
#[ test]
@@ -100,9 +135,11 @@ fn subcommand() {
100
135
. long ( "stuff" )
101
136
. action ( ArgAction :: Set )
102
137
. help ( "stuf value" ) ,
103
- ) ,
138
+ )
139
+ . arg ( arg ! ( --"unset-flag" ) ) ,
104
140
)
105
- . arg ( Arg :: new ( "other" ) . long ( "other" ) ) ;
141
+ . arg ( Arg :: new ( "other" ) . long ( "other" ) )
142
+ . arg ( arg ! ( --"unset-flag" ) ) ;
106
143
107
144
let m = cmd
108
145
. try_get_matches_from ( vec ! [
@@ -125,6 +162,9 @@ fn subcommand() {
125
162
sub_m. get_one:: <String >( "stuff" ) . map( |v| v. as_str( ) ) ,
126
163
Some ( "some other val" )
127
164
) ;
165
+ assert_eq ! ( sub_m. get_one:: <bool >( "unset-flag" ) . copied( ) , Some ( false ) ) ;
166
+
167
+ assert_eq ! ( m. get_one:: <bool >( "unset-flag" ) . copied( ) , Some ( false ) ) ;
128
168
}
129
169
130
170
#[ test]
0 commit comments