@@ -4,35 +4,27 @@ extern crate regex;
4
4
#[ cfg( test) ]
5
5
mod tests {
6
6
include ! ( "../clap-test.rs" ) ;
7
- use clap:: { App , Arg , SubCommand , AppSettings , ArgMatches } ;
8
-
9
- fn get_global_arg ( ) -> Arg < ' static , ' static > {
10
- Arg :: with_name ( "GLOBAL_ARG" )
11
- . long ( "global-arg" )
12
- . help (
13
- "Specifies something needed by the subcommands" ,
14
- )
15
- . global ( true )
16
- . takes_value ( true )
17
- }
18
-
19
- fn get_global_flag ( ) -> Arg < ' static , ' static > {
20
- Arg :: with_name ( "GLOBAL_FLAG" )
21
- . long ( "global-flag" )
22
- . help (
23
- "Specifies something needed by the subcommands" ,
24
- )
25
- . global ( true )
26
- }
7
+ use clap:: { App , Arg , SubCommand , ArgMatches } ;
27
8
28
9
fn get_app ( ) -> App < ' static , ' static > {
29
10
App :: new ( "myprog" )
30
- . global_setting ( AppSettings :: PropagateGlobalValuesDown )
31
- }
32
-
33
- fn get_subcommands ( ) -> App < ' static , ' static > {
34
- SubCommand :: with_name ( "outer" )
35
- . subcommand ( SubCommand :: with_name ( "inner" ) )
11
+ . arg ( Arg :: with_name ( "GLOBAL_ARG" )
12
+ . long ( "global-arg" )
13
+ . help (
14
+ "Specifies something needed by the subcommands" ,
15
+ )
16
+ . global ( true )
17
+ . takes_value ( true )
18
+ . default_value ( "default_value" ) )
19
+ . arg ( Arg :: with_name ( "GLOBAL_FLAG" )
20
+ . long ( "global-flag" )
21
+ . help (
22
+ "Specifies something needed by the subcommands" ,
23
+ )
24
+ . multiple ( true )
25
+ . global ( true ) )
26
+ . subcommand ( SubCommand :: with_name ( "outer" )
27
+ . subcommand ( SubCommand :: with_name ( "inner" ) ) )
36
28
}
37
29
38
30
fn get_matches ( app : App < ' static , ' static > , argv : & ' static str ) -> ArgMatches < ' static > {
@@ -59,6 +51,10 @@ mod tests {
59
51
get_outer_matches ( m) . value_of ( "GLOBAL_ARG" ) == val. into ( )
60
52
}
61
53
54
+ fn top_can_access_flag ( m : & ArgMatches < ' static > , present : bool , occurrences : u64 ) -> bool {
55
+ ( m. is_present ( "GLOBAL_FLAG" ) == present) && ( m. occurrences_of ( "GLOBAL_FLAG" ) == occurrences)
56
+ }
57
+
62
58
fn inner_can_access_flag ( m : & ArgMatches < ' static > , present : bool , occurrences : u64 ) -> bool {
63
59
let m = get_inner_matches ( m) ;
64
60
( m. is_present ( "GLOBAL_FLAG" ) == present) && ( m. occurrences_of ( "GLOBAL_FLAG" ) == occurrences)
@@ -70,80 +66,83 @@ mod tests {
70
66
}
71
67
72
68
#[ test]
73
- fn global_arg_defined_top_level_used_top_level ( ) {
74
- let app = get_app ( )
75
- . arg ( get_global_arg ( ) )
76
- . subcommand ( get_subcommands ( ) ) ;
77
-
78
- let m = get_matches ( app, "myprog --global-arg=some_value outer inner" ) ;
69
+ fn global_arg_used_top_level ( ) {
70
+ let m = get_matches ( get_app ( ) , "myprog --global-arg=some_value outer inner" ) ;
79
71
80
72
assert ! ( top_can_access_arg( & m, "some_value" ) ) ;
81
73
assert ! ( inner_can_access_arg( & m, "some_value" ) ) ;
82
74
assert ! ( outer_can_access_arg( & m, "some_value" ) ) ;
83
75
}
84
76
85
77
#[ test]
86
- fn global_arg_defined_top_level_used_outer ( ) {
87
- let app = get_app ( )
88
- . arg ( get_global_arg ( ) )
89
- . subcommand ( get_subcommands ( ) ) ;
90
-
91
- let m = get_matches ( app, "myprog outer --global-arg=some_value inner" ) ;
78
+ fn global_arg_used_outer ( ) {
79
+ let m = get_matches ( get_app ( ) , "myprog outer --global-arg=some_value inner" ) ;
92
80
93
81
assert ! ( top_can_access_arg( & m, "some_value" ) ) ;
94
82
assert ! ( inner_can_access_arg( & m, "some_value" ) ) ;
95
83
assert ! ( outer_can_access_arg( & m, "some_value" ) ) ;
96
84
}
97
85
98
86
#[ test]
99
- fn global_arg_defined_top_level_used_inner ( ) {
100
- let app = get_app ( )
101
- . arg ( get_global_arg ( ) )
102
- . subcommand ( get_subcommands ( ) ) ;
87
+ fn global_arg_used_inner ( ) {
88
+ let m = get_matches ( get_app ( ) , "myprog outer inner --global-arg=some_value" ) ;
103
89
104
- let m = get_matches ( app, "myprog outer inner --global-arg=some_value" ) ;
105
-
106
- assert ! ( top_can_access_arg( & m, "some_value" ) , "{:?}" , m) ;
90
+ assert ! ( top_can_access_arg( & m, "some_value" ) ) ;
107
91
assert ! ( inner_can_access_arg( & m, "some_value" ) ) ;
108
92
assert ! ( outer_can_access_arg( & m, "some_value" ) ) ;
109
93
}
110
94
111
- // #[test]
112
- // fn global_arg_defined_nested_used_top_level() {
113
- // let app = get_app()
114
- // .subcommand(get_subcommands()
115
- // .arg(get_global_arg()));
95
+ #[ test]
96
+ fn global_arg_default_value ( ) {
97
+ let m = get_matches ( get_app ( ) , "myprog outer inner" ) ;
116
98
117
- // let m = get_matches(app, "myprog --global-arg=some_value outer inner");
99
+ assert ! ( top_can_access_arg( & m, "default_value" ) ) ;
100
+ assert ! ( inner_can_access_arg( & m, "default_value" ) ) ;
101
+ assert ! ( outer_can_access_arg( & m, "default_value" ) ) ;
102
+ }
118
103
119
- // assert!(top_can_access_arg(&m, "some_value"));
120
- // assert!(inner_can_access_arg(&m, "some_value"));
121
- // assert!(outer_can_access_arg(&m, "some_value"));
122
- // }
104
+ #[ test]
105
+ fn global_flag_used_top_level ( ) {
106
+ let m = get_matches ( get_app ( ) , "myprog --global-flag outer inner" ) ;
123
107
124
- // #[test]
125
- // fn global_arg_defined_nested_used_outer() {
126
- // let app = get_app()
127
- // .subcommand(get_subcommands()
128
- // .arg(get_global_arg()));
108
+ assert ! ( top_can_access_flag( & m, true , 1 ) ) ;
109
+ assert ! ( inner_can_access_flag( & m, true , 1 ) ) ;
110
+ assert ! ( outer_can_access_flag( & m, true , 1 ) ) ;
111
+ }
129
112
130
- // let m = get_matches(app, "myprog outer --global-arg=some_value inner");
113
+ #[ test]
114
+ fn global_flag_used_outer ( ) {
115
+ let m = get_matches ( get_app ( ) , "myprog outer --global-flag inner" ) ;
131
116
132
- // assert!(top_can_access_arg (&m, "some_value" ));
133
- // assert!(inner_can_access_arg (&m, "some_value" ));
134
- // assert!(outer_can_access_arg (&m, "some_value" ));
135
- // }
117
+ assert ! ( top_can_access_flag ( & m, true , 1 ) ) ;
118
+ assert ! ( inner_can_access_flag ( & m, true , 1 ) ) ;
119
+ assert ! ( outer_can_access_flag ( & m, true , 1 ) ) ;
120
+ }
136
121
137
- // #[test]
138
- // fn global_arg_defined_nested_used_inner() {
139
- // let app = get_app()
140
- // .subcommand(get_subcommands()
141
- // .arg(get_global_arg()));
122
+ #[ test]
123
+ fn global_flag_used_inner ( ) {
124
+ let m = get_matches ( get_app ( ) , "myprog outer inner --global-flag" ) ;
142
125
143
- // let m = get_matches(app, "myprog outer inner --global-arg=some_value");
126
+ assert ! ( top_can_access_flag( & m, true , 1 ) ) ;
127
+ assert ! ( inner_can_access_flag( & m, true , 1 ) ) ;
128
+ assert ! ( outer_can_access_flag( & m, true , 1 ) ) ;
129
+ }
130
+
131
+ #[ test]
132
+ fn global_flag_2x_used_top_level ( ) {
133
+ let m = get_matches ( get_app ( ) , "myprog --global-flag --global-flag outer inner" ) ;
134
+
135
+ assert ! ( top_can_access_flag( & m, true , 2 ) ) ;
136
+ assert ! ( inner_can_access_flag( & m, true , 2 ) ) ;
137
+ assert ! ( outer_can_access_flag( & m, true , 2 ) ) ;
138
+ }
144
139
145
- // assert!(top_can_access_arg(&m, "some_value"));
146
- // assert!(inner_can_access_arg(&m, "some_value"));
147
- // assert!(outer_can_access_arg(&m, "some_value"));
148
- // }
140
+ #[ test]
141
+ fn global_flag_2x_used_inner ( ) {
142
+ let m = get_matches ( get_app ( ) , "myprog outer inner --global-flag --global-flag" ) ;
143
+
144
+ assert ! ( top_can_access_flag( & m, true , 2 ) ) ;
145
+ assert ! ( inner_can_access_flag( & m, true , 2 ) ) ;
146
+ assert ! ( outer_can_access_flag( & m, true , 2 ) ) ;
147
+ }
149
148
}
0 commit comments