1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
|
#[macro_use]
extern crate clap;
use clap::ErrorKind;
#[test]
fn basic() {
clap_app!(claptests =>
(version: "0.1")
(about: "tests clap library")
(author: "Kevin K. <kbknapp@gmail.com>")
(@arg opt: -o --option +takes_value ... "tests options")
(@arg positional: index(1) "tests positionals")
(@arg flag: -f --flag ... +global "tests flags")
(@arg flag2: -F conflicts_with[flag] requires[option2]
"tests flags with exclusions")
(@arg option2: --long_option_2 conflicts_with[option] requires[positional2]
"tests long options with exclusions")
(@arg positional2: index(2) "tests positionals with exclusions")
(@arg option3: -O --Option +takes_value possible_value[fast slow]
"tests options with specific value sets")
(@arg positional3: index(3) ... possible_value[vi emacs]
"tests positionals with specific values")
(@arg multvals: --multvals +takes_value value_name[one two]
"Tests multiple values, not mult occs")
(@arg multvalsmo: --multvalsmo ... +takes_value value_name[one two]
"Tests multiple values, not mult occs")
(@arg minvals: --minvals2 min_values(1) ... +takes_value "Tests 2 min vals")
(@arg maxvals: --maxvals3 ... +takes_value max_values(3) "Tests 3 max vals")
(@subcommand subcmd =>
(about: "tests subcommands")
(version: "0.1")
(author: "Kevin K. <kbknapp@gmail.com>")
(@arg scoption: -o --option ... +takes_value "tests options")
(@arg scpositional: index(1) "tests positionals"))
);
}
#[test]
fn quoted_app_name() {
let app = clap_app!(("app name with spaces-and-hyphens") =>
(version: "0.1")
(about: "tests clap library")
(author: "Kevin K. <kbknapp@gmail.com>")
(@arg opt: -o --option +takes_value ... "tests options")
(@arg positional: index(1) "tests positionals")
(@arg flag: -f --flag ... +global "tests flags")
(@arg flag2: -F conflicts_with[flag] requires[option2]
"tests flags with exclusions")
(@arg option2: --long_option_2 conflicts_with[option] requires[positional2]
"tests long options with exclusions")
(@arg positional2: index(2) "tests positionals with exclusions")
(@arg option3: -O --Option +takes_value possible_value[fast slow]
"tests options with specific value sets")
(@arg positional3: index(3) ... possible_value[vi emacs]
"tests positionals with specific values")
(@arg multvals: --multvals +takes_value value_name[one two]
"Tests multiple values, not mult occs")
(@arg multvalsmo: --multvalsmo ... +takes_value value_name[one two]
"Tests multiple values, not mult occs")
(@arg minvals: --minvals2 min_values(1) ... +takes_value "Tests 2 min vals")
(@arg maxvals: --maxvals3 ... +takes_value max_values(3) "Tests 3 max vals")
(@subcommand subcmd =>
(about: "tests subcommands")
(version: "0.1")
(author: "Kevin K. <kbknapp@gmail.com>")
(@arg scoption: -o --option ... +takes_value "tests options")
(@arg scpositional: index(1) "tests positionals"))
);
assert_eq!(app.p.meta.name, "app name with spaces-and-hyphens");
let mut help_text = vec![];
app.write_help(&mut help_text).expect("Could not write help text.");
let help_text = String::from_utf8(help_text).expect("Help text is not valid utf-8");
assert!(help_text.starts_with("app name with spaces-and-hyphens 0.1\n"));
}
#[test]
fn quoted_arg_long_name() {
let app = clap_app!(claptests =>
(version: "0.1")
(about: "tests clap library")
(author: "Kevin K. <kbknapp@gmail.com>")
(@arg opt: -o --option +takes_value ... "tests options")
(@arg positional: index(1) "tests positionals")
(@arg flag: -f --flag ... +global "tests flags")
(@arg flag2: -F conflicts_with[flag] requires[option2]
"tests flags with exclusions")
(@arg option2: --("long-option-2") conflicts_with[option] requires[positional2]
"tests long options with exclusions")
(@arg positional2: index(2) "tests positionals with exclusions")
(@arg option3: -O --Option +takes_value possible_value[fast slow]
"tests options with specific value sets")
(@arg positional3: index(3) ... possible_value[vi emacs]
"tests positionals with specific values")
(@arg multvals: --multvals +takes_value value_name[one two]
"Tests multiple values, not mult occs")
(@arg multvalsmo: --multvalsmo ... +takes_value value_name[one two]
"Tests multiple values, not mult occs")
(@arg minvals: --minvals2 min_values(1) ... +takes_value "Tests 2 min vals")
(@arg maxvals: --maxvals3 ... +takes_value max_values(3) "Tests 3 max vals")
(@subcommand subcmd =>
(about: "tests subcommands")
(version: "0.1")
(author: "Kevin K. <kbknapp@gmail.com>")
(@arg scoption: -o --option ... +takes_value "tests options")
(@arg scpositional: index(1) "tests positionals"))
);
let matches = app.get_matches_from_safe(vec!["bin_name", "value1", "value2", "--long-option-2"])
.expect("Expected to successfully match the given args.");
assert!(matches.is_present("option2"));
}
#[test]
fn quoted_arg_name() {
let app = clap_app!(claptests =>
(version: "0.1")
(about: "tests clap library")
(author: "Kevin K. <kbknapp@gmail.com>")
(@arg opt: -o --option +takes_value ... "tests options")
(@arg ("positional-arg"): index(1) "tests positionals")
(@arg flag: -f --flag ... +global "tests flags")
(@arg flag2: -F conflicts_with[flag] requires[option2]
"tests flags with exclusions")
(@arg option2: --("long-option-2") conflicts_with[option] requires[positional2]
"tests long options with exclusions")
(@arg positional2: index(2) "tests positionals with exclusions")
(@arg option3: -O --Option +takes_value possible_value[fast slow]
"tests options with specific value sets")
(@arg ("positional-3"): index(3) ... possible_value[vi emacs]
"tests positionals with specific values")
(@arg multvals: --multvals +takes_value value_name[one two]
"Tests multiple values, not mult occs")
(@arg multvalsmo: --multvalsmo ... +takes_value value_name[one two]
"Tests multiple values, not mult occs")
(@arg minvals: --minvals2 min_values(1) ... +takes_value "Tests 2 min vals")
(@arg maxvals: --maxvals3 ... +takes_value max_values(3) "Tests 3 max vals")
(@subcommand subcmd =>
(about: "tests subcommands")
(version: "0.1")
(author: "Kevin K. <kbknapp@gmail.com>")
(@arg scoption: -o --option ... +takes_value "tests options")
(@arg scpositional: index(1) "tests positionals"))
);
let matches = app.get_matches_from_safe(vec!["bin_name", "value1", "value2", "--long-option-2"])
.expect("Expected to successfully match the given args.");
assert!(matches.is_present("option2"));
}
#[test]
fn group_macro() {
let app = clap_app!(claptests =>
(version: "0.1")
(about: "tests clap library")
(author: "Kevin K. <kbknapp@gmail.com>")
(@group difficulty =>
(@arg hard: -h --hard "Sets hard mode")
(@arg normal: -n --normal "Sets normal mode")
(@arg easy: -e --easy "Sets easy mode")
)
);
let result = app.get_matches_from_safe(vec!["bin_name", "--hard"]);
assert!(result.is_ok());
let matches = result.expect("Expected to successfully match the given args.");
assert!(matches.is_present("difficulty"));
assert!(matches.is_present("hard"));
}
#[test]
fn group_macro_set_multiple() {
let app = clap_app!(claptests =>
(version: "0.1")
(about: "tests clap library")
(author: "Kevin K. <kbknapp@gmail.com>")
(@group difficulty +multiple =>
(@arg hard: -h --hard "Sets hard mode")
(@arg normal: -n --normal "Sets normal mode")
(@arg easy: -e --easy "Sets easy mode")
)
);
let result = app.get_matches_from_safe(vec!["bin_name", "--hard", "--easy"]);
assert!(result.is_ok());
let matches = result.expect("Expected to successfully match the given args.");
assert!(matches.is_present("difficulty"));
assert!(matches.is_present("hard"));
assert!(matches.is_present("easy"));
assert!(!matches.is_present("normal"));
}
#[test]
fn group_macro_set_not_multiple() {
let app = clap_app!(claptests =>
(version: "0.1")
(about: "tests clap library")
(author: "Kevin K. <kbknapp@gmail.com>")
(@group difficulty !multiple =>
(@arg hard: -h --hard "Sets hard mode")
(@arg normal: -n --normal "Sets normal mode")
(@arg easy: -e --easy "Sets easy mode")
)
);
let result = app.get_matches_from_safe(vec!["bin_name", "--hard", "--easy"]);
assert!(result.is_err());
let err = result.unwrap_err();
assert_eq!(err.kind, ErrorKind::ArgumentConflict);
}
#[test]
fn group_macro_set_required() {
let app = clap_app!(claptests =>
(version: "0.1")
(about: "tests clap library")
(author: "Kevin K. <kbknapp@gmail.com>")
(@group difficulty +required =>
(@arg hard: -h --hard "Sets hard mode")
(@arg normal: -n --normal "Sets normal mode")
(@arg easy: -e --easy "Sets easy mode")
)
);
let result = app.get_matches_from_safe(vec!["bin_name"]);
assert!(result.is_err());
let err = result.unwrap_err();
assert_eq!(err.kind, ErrorKind::MissingRequiredArgument);
}
#[test]
fn group_macro_set_not_required() {
let app = clap_app!(claptests =>
(version: "0.1")
(about: "tests clap library")
(author: "Kevin K. <kbknapp@gmail.com>")
(@group difficulty !required =>
(@arg hard: -h --hard "Sets hard mode")
(@arg normal: -n --normal "Sets normal mode")
(@arg easy: -e --easy "Sets easy mode")
)
);
let result = app.get_matches_from_safe(vec!["bin_name"]);
assert!(result.is_ok());
let matches = result.expect("Expected to successfully match the given args.");
assert!(!matches.is_present("difficulty"));
}
#[test]
fn multiarg() {
let app = || clap_app!(
claptests =>
(@arg flag: --flag "value")
(@arg multiarg: --multiarg
default_value("flag-unset") default_value_if("flag", None, "flag-set")
"multiarg")
(@arg multiarg2: --multiarg2
default_value("flag-unset") default_value_if("flag", None, "flag-set",)
"multiarg2")
);
let matches = app()
.get_matches_from_safe(vec!["bin_name"])
.expect("match failed");
assert_eq!(matches.value_of("multiarg"), Some("flag-unset"));
assert_eq!(matches.value_of("multiarg2"), Some("flag-unset"));
let matches = app()
.get_matches_from_safe(vec!["bin_name", "--flag"])
.expect("match failed");
assert_eq!(matches.value_of("multiarg"), Some("flag-set"));
assert_eq!(matches.value_of("multiarg2"), Some("flag-set"));
}
#[test]
fn arg_enum() {
// Helper macros to avoid repetition
macro_rules! test_greek {
($arg_enum:item, $tests:block) => {{
$arg_enum
// FromStr implementation
assert!("Charlie".parse::<Greek>().is_err());
// Display implementation
assert_eq!(format!("{}", Greek::Alpha), "Alpha");
assert_eq!(format!("{}", Greek::Bravo), "Bravo");
// fn variants()
assert_eq!(Greek::variants(), ["Alpha", "Bravo"]);
// rest of tests
$tests
}};
}
macro_rules! test_greek_no_meta {
{$arg_enum:item} => {
test_greek!($arg_enum, {
// FromStr implementation
assert!("Alpha".parse::<Greek>().is_ok());
assert!("Bravo".parse::<Greek>().is_ok());
})
};
}
macro_rules! test_greek_meta {
{$arg_enum:item} => {
test_greek!($arg_enum, {
// FromStr implementation
assert_eq!("Alpha".parse::<Greek>(), Ok(Greek::Alpha));
assert_eq!("Bravo".parse::<Greek>(), Ok(Greek::Bravo));
})
};
}
// Tests for each pattern
// meta NO, pub NO, trailing comma NO
test_greek_no_meta!{
arg_enum!{
enum Greek {
Alpha,
Bravo
}
}
};
// meta NO, pub NO, trailing comma YES
test_greek_no_meta!{
arg_enum!{
enum Greek {
Alpha,
Bravo,
}
}
};
// meta NO, pub YES, trailing comma NO
test_greek_no_meta!{
arg_enum!{
pub enum Greek {
Alpha,
Bravo
}
}
};
// meta NO, pub YES, trailing comma YES
test_greek_no_meta!{
arg_enum!{
pub enum Greek {
Alpha,
Bravo,
}
}
};
// meta YES, pub NO, trailing comma NO
test_greek_meta!{
arg_enum!{
#[derive(Debug, PartialEq, Copy, Clone)]
enum Greek {
Alpha,
Bravo
}
}
};
// meta YES, pub NO, trailing comma YES
test_greek_meta!{
arg_enum!{
#[derive(Debug, PartialEq, Copy, Clone)]
enum Greek {
Alpha,
Bravo,
}
}
};
// meta YES, pub YES, trailing comma NO
test_greek_meta!{
arg_enum!{
#[derive(Debug, PartialEq, Copy, Clone)]
pub enum Greek {
Alpha,
Bravo
}
}
};
// meta YES, pub YES, trailing comma YES
test_greek_meta!{
arg_enum!{
#[derive(Debug, PartialEq, Copy, Clone)]
pub enum Greek {
Alpha,
Bravo,
}
}
};
}
|