1#![allow(clippy::bool_assert_comparison)]
2
3use clap::builder::ArgPredicate;
4use clap::error::ErrorKind;
5use clap::Arg;
6use clap::ArgAction;
7use clap::Command;
8
9#[test]
10fn set() {
11    let cmd = Command::new("test").arg(Arg::new("mammal").long("mammal").action(ArgAction::Set));
12
13    let matches = cmd.clone().try_get_matches_from(["test"]).unwrap();
14    assert_eq!(matches.get_one::<String>("mammal"), None);
15    assert_eq!(matches.contains_id("mammal"), false);
16    assert_eq!(matches.index_of("mammal"), None);
17
18    let matches = cmd
19        .clone()
20        .try_get_matches_from(["test", "--mammal", "dog"])
21        .unwrap();
22    assert_eq!(matches.get_one::<String>("mammal").unwrap(), "dog");
23    assert_eq!(matches.contains_id("mammal"), true);
24    assert_eq!(matches.index_of("mammal"), Some(2));
25
26    let result = cmd
27        .clone()
28        .try_get_matches_from(["test", "--mammal", "dog", "--mammal", "cat"]);
29    let err = result.err().unwrap();
30    assert_eq!(err.kind(), ErrorKind::ArgumentConflict);
31
32    let matches = cmd
33        .clone()
34        .args_override_self(true)
35        .try_get_matches_from(["test", "--mammal", "dog", "--mammal", "cat"])
36        .unwrap();
37    assert_eq!(matches.get_one::<String>("mammal").unwrap(), "cat");
38    assert_eq!(matches.contains_id("mammal"), true);
39    assert_eq!(matches.index_of("mammal"), Some(4));
40}
41
42#[test]
43fn append() {
44    let cmd = Command::new("test").arg(Arg::new("mammal").long("mammal").action(ArgAction::Append));
45
46    let matches = cmd.clone().try_get_matches_from(["test"]).unwrap();
47    assert_eq!(matches.get_one::<String>("mammal"), None);
48    assert_eq!(matches.contains_id("mammal"), false);
49    assert_eq!(matches.index_of("mammal"), None);
50
51    let matches = cmd
52        .clone()
53        .try_get_matches_from(["test", "--mammal", "dog"])
54        .unwrap();
55    assert_eq!(matches.get_one::<String>("mammal").unwrap(), "dog");
56    assert_eq!(matches.contains_id("mammal"), true);
57    assert_eq!(
58        matches.indices_of("mammal").unwrap().collect::<Vec<_>>(),
59        vec![2]
60    );
61
62    let matches = cmd
63        .clone()
64        .try_get_matches_from(["test", "--mammal", "dog", "--mammal", "cat"])
65        .unwrap();
66    assert_eq!(
67        matches
68            .get_many::<String>("mammal")
69            .unwrap()
70            .map(|s| s.as_str())
71            .collect::<Vec<_>>(),
72        vec!["dog", "cat"]
73    );
74    assert_eq!(matches.contains_id("mammal"), true);
75    assert_eq!(
76        matches.indices_of("mammal").unwrap().collect::<Vec<_>>(),
77        vec![2, 4]
78    );
79}
80
81#[test]
82fn set_true() {
83    let cmd =
84        Command::new("test").arg(Arg::new("mammal").long("mammal").action(ArgAction::SetTrue));
85
86    let matches = cmd.clone().try_get_matches_from(["test"]).unwrap();
87    assert_eq!(matches.get_flag("mammal"), false);
88    assert_eq!(matches.contains_id("mammal"), true);
89    assert_eq!(matches.index_of("mammal"), Some(1));
90
91    let matches = cmd
92        .clone()
93        .try_get_matches_from(["test", "--mammal"])
94        .unwrap();
95    assert_eq!(matches.get_flag("mammal"), true);
96    assert_eq!(matches.contains_id("mammal"), true);
97    assert_eq!(matches.index_of("mammal"), Some(1));
98
99    let result = cmd
100        .clone()
101        .try_get_matches_from(["test", "--mammal", "--mammal"]);
102    let err = result.err().unwrap();
103    assert_eq!(err.kind(), ErrorKind::ArgumentConflict);
104
105    let matches = cmd
106        .clone()
107        .args_override_self(true)
108        .try_get_matches_from(["test", "--mammal", "--mammal"])
109        .unwrap();
110    assert_eq!(matches.get_flag("mammal"), true);
111    assert_eq!(matches.contains_id("mammal"), true);
112    assert_eq!(matches.index_of("mammal"), Some(2));
113}
114
115#[test]
116fn set_true_with_explicit_default_value() {
117    let cmd = Command::new("test").arg(
118        Arg::new("mammal")
119            .long("mammal")
120            .action(ArgAction::SetTrue)
121            .default_value("false"),
122    );
123
124    let matches = cmd
125        .clone()
126        .try_get_matches_from(["test", "--mammal"])
127        .unwrap();
128    assert_eq!(matches.get_flag("mammal"), true);
129    assert_eq!(matches.contains_id("mammal"), true);
130    assert_eq!(matches.index_of("mammal"), Some(1));
131
132    let matches = cmd.clone().try_get_matches_from(["test"]).unwrap();
133    assert_eq!(matches.get_flag("mammal"), false);
134    assert_eq!(matches.contains_id("mammal"), true);
135    assert_eq!(matches.index_of("mammal"), Some(1));
136}
137
138#[test]
139fn set_true_with_default_value_if_present() {
140    let cmd = Command::new("test")
141        .arg(
142            Arg::new("mammal")
143                .long("mammal")
144                .action(ArgAction::SetTrue)
145                .default_value_if("dog", ArgPredicate::IsPresent, Some("true")),
146        )
147        .arg(Arg::new("dog").long("dog").action(ArgAction::SetTrue));
148
149    let matches = cmd.clone().try_get_matches_from(["test"]).unwrap();
150    assert_eq!(matches.get_flag("dog"), false);
151    assert_eq!(matches.get_flag("mammal"), false);
152
153    let matches = cmd.clone().try_get_matches_from(["test", "--dog"]).unwrap();
154    assert_eq!(matches.get_flag("dog"), true);
155    assert_eq!(matches.get_flag("mammal"), true);
156
157    let matches = cmd
158        .clone()
159        .try_get_matches_from(["test", "--mammal"])
160        .unwrap();
161    assert_eq!(matches.get_flag("dog"), false);
162    assert_eq!(matches.get_flag("mammal"), true);
163}
164
165#[test]
166fn set_true_with_default_value_if_value() {
167    let cmd = Command::new("test")
168        .arg(
169            Arg::new("mammal")
170                .long("mammal")
171                .action(ArgAction::SetTrue)
172                .default_value_if("dog", "true", Some("true")),
173        )
174        .arg(Arg::new("dog").long("dog").action(ArgAction::SetTrue));
175
176    let matches = cmd.clone().try_get_matches_from(["test"]).unwrap();
177    assert_eq!(matches.get_flag("dog"), false);
178    assert_eq!(matches.get_flag("mammal"), false);
179
180    let matches = cmd.clone().try_get_matches_from(["test", "--dog"]).unwrap();
181    assert_eq!(matches.get_flag("dog"), true);
182    assert_eq!(matches.get_flag("mammal"), true);
183
184    let matches = cmd
185        .clone()
186        .try_get_matches_from(["test", "--mammal"])
187        .unwrap();
188    assert_eq!(matches.get_flag("dog"), false);
189    assert_eq!(matches.get_flag("mammal"), true);
190}
191
192#[test]
193fn set_true_with_required_if_eq() {
194    let cmd = Command::new("test")
195        .arg(
196            Arg::new("mammal")
197                .long("mammal")
198                .action(ArgAction::SetTrue)
199                .required_if_eq("dog", "true"),
200        )
201        .arg(Arg::new("dog").long("dog").action(ArgAction::SetTrue));
202
203    let matches = cmd
204        .clone()
205        .try_get_matches_from(["test", "--mammal"])
206        .unwrap();
207    assert_eq!(matches.get_flag("dog"), false);
208    assert_eq!(matches.get_flag("mammal"), true);
209
210    cmd.clone()
211        .try_get_matches_from(["test", "--dog"])
212        .unwrap_err();
213
214    let matches = cmd
215        .clone()
216        .try_get_matches_from(["test", "--dog", "--mammal"])
217        .unwrap();
218    assert_eq!(matches.get_flag("dog"), true);
219    assert_eq!(matches.get_flag("mammal"), true);
220}
221
222#[test]
223fn set_false() {
224    let cmd = Command::new("test").arg(
225        Arg::new("mammal")
226            .long("mammal")
227            .action(ArgAction::SetFalse),
228    );
229
230    let matches = cmd.clone().try_get_matches_from(["test"]).unwrap();
231    assert_eq!(matches.get_flag("mammal"), true);
232    assert_eq!(matches.contains_id("mammal"), true);
233    assert_eq!(matches.index_of("mammal"), Some(1));
234
235    let matches = cmd
236        .clone()
237        .try_get_matches_from(["test", "--mammal"])
238        .unwrap();
239    assert_eq!(matches.get_flag("mammal"), false);
240    assert_eq!(matches.contains_id("mammal"), true);
241    assert_eq!(matches.index_of("mammal"), Some(1));
242
243    let result = cmd
244        .clone()
245        .try_get_matches_from(["test", "--mammal", "--mammal"]);
246    let err = result.err().unwrap();
247    assert_eq!(err.kind(), ErrorKind::ArgumentConflict);
248
249    let matches = cmd
250        .clone()
251        .args_override_self(true)
252        .try_get_matches_from(["test", "--mammal", "--mammal"])
253        .unwrap();
254    assert_eq!(matches.get_flag("mammal"), false);
255    assert_eq!(matches.contains_id("mammal"), true);
256    assert_eq!(matches.index_of("mammal"), Some(2));
257}
258
259#[test]
260fn set_false_with_explicit_default_value() {
261    let cmd = Command::new("test").arg(
262        Arg::new("mammal")
263            .long("mammal")
264            .action(ArgAction::SetFalse)
265            .default_value("true"),
266    );
267
268    let matches = cmd
269        .clone()
270        .try_get_matches_from(["test", "--mammal"])
271        .unwrap();
272    assert_eq!(matches.get_flag("mammal"), false);
273    assert_eq!(matches.contains_id("mammal"), true);
274    assert_eq!(matches.index_of("mammal"), Some(1));
275
276    let matches = cmd.clone().try_get_matches_from(["test"]).unwrap();
277    assert_eq!(matches.get_flag("mammal"), true);
278    assert_eq!(matches.contains_id("mammal"), true);
279    assert_eq!(matches.index_of("mammal"), Some(1));
280}
281
282#[test]
283fn set_false_with_default_value_if_present() {
284    let cmd = Command::new("test")
285        .arg(
286            Arg::new("mammal")
287                .long("mammal")
288                .action(ArgAction::SetFalse)
289                .default_value_if("dog", ArgPredicate::IsPresent, Some("false")),
290        )
291        .arg(Arg::new("dog").long("dog").action(ArgAction::SetFalse));
292
293    let matches = cmd.clone().try_get_matches_from(["test"]).unwrap();
294    assert_eq!(matches.get_flag("dog"), true);
295    assert_eq!(matches.get_flag("mammal"), true);
296
297    let matches = cmd.clone().try_get_matches_from(["test", "--dog"]).unwrap();
298    assert_eq!(matches.get_flag("dog"), false);
299    assert_eq!(matches.get_flag("mammal"), false);
300
301    let matches = cmd
302        .clone()
303        .try_get_matches_from(["test", "--mammal"])
304        .unwrap();
305    assert_eq!(matches.get_flag("dog"), true);
306    assert_eq!(matches.get_flag("mammal"), false);
307}
308
309#[test]
310fn set_false_with_default_value_if_value() {
311    let cmd = Command::new("test")
312        .arg(
313            Arg::new("mammal")
314                .long("mammal")
315                .action(ArgAction::SetFalse)
316                .default_value_if("dog", "false", Some("false")),
317        )
318        .arg(Arg::new("dog").long("dog").action(ArgAction::SetFalse));
319
320    let matches = cmd.clone().try_get_matches_from(["test"]).unwrap();
321    assert_eq!(matches.get_flag("dog"), true);
322    assert_eq!(matches.get_flag("mammal"), true);
323
324    let matches = cmd.clone().try_get_matches_from(["test", "--dog"]).unwrap();
325    assert_eq!(matches.get_flag("dog"), false);
326    assert_eq!(matches.get_flag("mammal"), false);
327
328    let matches = cmd
329        .clone()
330        .try_get_matches_from(["test", "--mammal"])
331        .unwrap();
332    assert_eq!(matches.get_flag("dog"), true);
333    assert_eq!(matches.get_flag("mammal"), false);
334}
335
336#[test]
337fn count() {
338    let cmd = Command::new("test").arg(Arg::new("mammal").long("mammal").action(ArgAction::Count));
339
340    let matches = cmd.clone().try_get_matches_from(["test"]).unwrap();
341    assert_eq!(*matches.get_one::<u8>("mammal").unwrap(), 0);
342    assert_eq!(matches.contains_id("mammal"), true);
343    assert_eq!(matches.index_of("mammal"), Some(1));
344
345    let matches = cmd
346        .clone()
347        .try_get_matches_from(["test", "--mammal"])
348        .unwrap();
349    assert_eq!(*matches.get_one::<u8>("mammal").unwrap(), 1);
350    assert_eq!(matches.contains_id("mammal"), true);
351    assert_eq!(matches.index_of("mammal"), Some(1));
352
353    let matches = cmd
354        .clone()
355        .try_get_matches_from(["test", "--mammal", "--mammal"])
356        .unwrap();
357    assert_eq!(*matches.get_one::<u8>("mammal").unwrap(), 2);
358    assert_eq!(matches.contains_id("mammal"), true);
359    assert_eq!(matches.index_of("mammal"), Some(2));
360}
361
362#[test]
363fn count_with_explicit_default_value() {
364    let cmd = Command::new("test").arg(
365        Arg::new("mammal")
366            .long("mammal")
367            .action(ArgAction::Count)
368            .default_value("10"),
369    );
370
371    let matches = cmd
372        .clone()
373        .try_get_matches_from(["test", "--mammal"])
374        .unwrap();
375    assert_eq!(*matches.get_one::<u8>("mammal").unwrap(), 1);
376    assert_eq!(matches.contains_id("mammal"), true);
377    assert_eq!(matches.index_of("mammal"), Some(1));
378
379    let matches = cmd.clone().try_get_matches_from(["test"]).unwrap();
380    assert_eq!(*matches.get_one::<u8>("mammal").unwrap(), 10);
381    assert_eq!(matches.contains_id("mammal"), true);
382    assert_eq!(matches.index_of("mammal"), Some(1));
383}
384
385#[test]
386fn count_with_default_value_if_present() {
387    let cmd = Command::new("test")
388        .arg(
389            Arg::new("mammal")
390                .long("mammal")
391                .action(ArgAction::Count)
392                .default_value_if("dog", ArgPredicate::IsPresent, Some("10")),
393        )
394        .arg(Arg::new("dog").long("dog").action(ArgAction::Count));
395
396    let matches = cmd.clone().try_get_matches_from(["test"]).unwrap();
397    assert_eq!(*matches.get_one::<u8>("dog").unwrap(), 0);
398    assert_eq!(*matches.get_one::<u8>("mammal").unwrap(), 0);
399
400    let matches = cmd.clone().try_get_matches_from(["test", "--dog"]).unwrap();
401    assert_eq!(*matches.get_one::<u8>("dog").unwrap(), 1);
402    assert_eq!(*matches.get_one::<u8>("mammal").unwrap(), 10);
403
404    let matches = cmd
405        .clone()
406        .try_get_matches_from(["test", "--mammal"])
407        .unwrap();
408    assert_eq!(*matches.get_one::<u8>("dog").unwrap(), 0);
409    assert_eq!(*matches.get_one::<u8>("mammal").unwrap(), 1);
410}
411
412#[test]
413fn count_with_default_value_if_value() {
414    let cmd = Command::new("test")
415        .arg(
416            Arg::new("mammal")
417                .long("mammal")
418                .action(ArgAction::Count)
419                .default_value_if("dog", "2", Some("10")),
420        )
421        .arg(Arg::new("dog").long("dog").action(ArgAction::Count));
422
423    let matches = cmd.clone().try_get_matches_from(["test"]).unwrap();
424    assert_eq!(*matches.get_one::<u8>("dog").unwrap(), 0);
425    assert_eq!(*matches.get_one::<u8>("mammal").unwrap(), 0);
426
427    let matches = cmd.clone().try_get_matches_from(["test", "--dog"]).unwrap();
428    assert_eq!(*matches.get_one::<u8>("dog").unwrap(), 1);
429    assert_eq!(*matches.get_one::<u8>("mammal").unwrap(), 0);
430
431    let matches = cmd
432        .clone()
433        .try_get_matches_from(["test", "--dog", "--dog"])
434        .unwrap();
435    assert_eq!(*matches.get_one::<u8>("dog").unwrap(), 2);
436    assert_eq!(*matches.get_one::<u8>("mammal").unwrap(), 10);
437
438    let matches = cmd
439        .clone()
440        .try_get_matches_from(["test", "--mammal"])
441        .unwrap();
442    assert_eq!(*matches.get_one::<u8>("dog").unwrap(), 0);
443    assert_eq!(*matches.get_one::<u8>("mammal").unwrap(), 1);
444}
445