File tree Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change @@ -126,3 +126,40 @@ fn deeply_nested_discovery() {
126126    let  m = m. subcommand_matches ( "c" ) . unwrap ( ) ; 
127127    assert ! ( * m. get_one:: <bool >( "long-c" ) . expect( "defaulted by clap" ) ) ; 
128128} 
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+ } 
    
 
   
 
     
   
   
          
     
  
    
     
 
    
      
     
 
     
    You can’t perform that action at this time.
  
 
    
  
     
    
      
        
     
 
       
      
     
   
 
    
    
  
 
  
 
     
    
0 commit comments