@@ -126,3 +126,77 @@ fn deeply_nested_discovery() {
126
126
let m = m. subcommand_matches ( "c" ) . unwrap ( ) ;
127
127
assert ! ( * m. get_one:: <bool >( "long-c" ) . expect( "defaulted by clap" ) ) ;
128
128
}
129
+
130
+ #[ test]
131
+ fn global_overrides_default ( ) {
132
+ let cmd = Command :: new ( "test" )
133
+ . arg (
134
+ Arg :: new ( "name" )
135
+ . long ( "name" )
136
+ . global ( true )
137
+ . takes_value ( true )
138
+ . default_value ( "from_default" ) ,
139
+ )
140
+ . subcommand ( Command :: new ( "sub" ) ) ;
141
+
142
+ let m = cmd. clone ( ) . try_get_matches_from ( [ "test" ] ) . unwrap ( ) ;
143
+ assert_eq ! (
144
+ m. get_one:: <String >( "name" ) . unwrap( ) . as_str( ) ,
145
+ "from_default"
146
+ ) ;
147
+
148
+ let m = cmd
149
+ . clone ( )
150
+ . try_get_matches_from ( [ "test" , "--name" , "from_arg" ] )
151
+ . unwrap ( ) ;
152
+ assert_eq ! ( m. get_one:: <String >( "name" ) . unwrap( ) . as_str( ) , "from_arg" ) ;
153
+
154
+ let m = cmd
155
+ . clone ( )
156
+ . try_get_matches_from ( [ "test" , "--name" , "from_arg" , "sub" ] )
157
+ . unwrap ( ) ;
158
+ assert_eq ! ( m. get_one:: <String >( "name" ) . unwrap( ) . as_str( ) , "from_arg" ) ;
159
+
160
+ let m = cmd
161
+ . clone ( )
162
+ . try_get_matches_from ( [ "test" , "sub" , "--name" , "from_arg" ] )
163
+ . unwrap ( ) ;
164
+ assert_eq ! ( m. get_one:: <String >( "name" ) . unwrap( ) . as_str( ) , "from_arg" ) ;
165
+ }
166
+
167
+ #[ test]
168
+ #[ cfg( feature = "env" ) ]
169
+ fn global_overrides_env ( ) {
170
+ std:: env:: set_var ( "GLOBAL_OVERRIDES_ENV" , "from_env" ) ;
171
+
172
+ let cmd = Command :: new ( "test" )
173
+ . arg (
174
+ Arg :: new ( "name" )
175
+ . long ( "name" )
176
+ . global ( true )
177
+ . takes_value ( true )
178
+ . env ( "GLOBAL_OVERRIDES_ENV" ) ,
179
+ )
180
+ . subcommand ( Command :: new ( "sub" ) ) ;
181
+
182
+ let m = cmd. clone ( ) . try_get_matches_from ( [ "test" ] ) . unwrap ( ) ;
183
+ assert_eq ! ( m. get_one:: <String >( "name" ) . unwrap( ) . as_str( ) , "from_env" ) ;
184
+
185
+ let m = cmd
186
+ . clone ( )
187
+ . try_get_matches_from ( [ "test" , "--name" , "from_arg" ] )
188
+ . unwrap ( ) ;
189
+ assert_eq ! ( m. get_one:: <String >( "name" ) . unwrap( ) . as_str( ) , "from_arg" ) ;
190
+
191
+ let m = cmd
192
+ . clone ( )
193
+ . try_get_matches_from ( [ "test" , "--name" , "from_arg" , "sub" ] )
194
+ . unwrap ( ) ;
195
+ assert_eq ! ( m. get_one:: <String >( "name" ) . unwrap( ) . as_str( ) , "from_arg" ) ;
196
+
197
+ let m = cmd
198
+ . clone ( )
199
+ . try_get_matches_from ( [ "test" , "sub" , "--name" , "from_arg" ] )
200
+ . unwrap ( ) ;
201
+ assert_eq ! ( m. get_one:: <String >( "name" ) . unwrap( ) . as_str( ) , "from_arg" ) ;
202
+ }
0 commit comments