1 use clap::{arg, Arg, ArgAction, Command};
2
3 #[test]
single_short_arg_without_value()4 fn single_short_arg_without_value() {
5 let cmd = Command::new("cmd").ignore_errors(true).arg(arg!(
6 -c --config [FILE] "Sets a custom config file"
7 ));
8
9 let r = cmd.try_get_matches_from(vec!["cmd", "-c" /* missing: , "config file" */]);
10
11 assert!(r.is_ok(), "unexpected error: {:?}", r);
12 let m = r.unwrap();
13 assert!(m.contains_id("config"));
14 }
15
16 #[test]
single_long_arg_without_value()17 fn single_long_arg_without_value() {
18 let cmd = Command::new("cmd").ignore_errors(true).arg(arg!(
19 -c --config [FILE] "Sets a custom config file"
20 ));
21
22 let r = cmd.try_get_matches_from(vec!["cmd", "--config" /* missing: , "config file" */]);
23
24 assert!(r.is_ok(), "unexpected error: {:?}", r);
25 let m = r.unwrap();
26 assert!(m.contains_id("config"));
27 }
28
29 #[test]
multiple_args_and_final_arg_without_value()30 fn multiple_args_and_final_arg_without_value() {
31 let cmd = Command::new("cmd")
32 .ignore_errors(true)
33 .arg(arg!(
34 -c --config [FILE] "Sets a custom config file"
35 ))
36 .arg(arg!(
37 -x --stuff [FILE] "Sets a custom stuff file"
38 ))
39 .arg(arg!(f: -f "Flag").action(ArgAction::SetTrue));
40
41 let r = cmd.try_get_matches_from(vec![
42 "cmd", "-c", "file", "-f", "-x", /* missing: , "some stuff" */
43 ]);
44
45 assert!(r.is_ok(), "unexpected error: {:?}", r);
46 let m = r.unwrap();
47 assert_eq!(
48 m.get_one::<String>("config").map(|v| v.as_str()),
49 Some("file")
50 );
51 assert!(*m.get_one::<bool>("f").expect("defaulted by clap"));
52 assert_eq!(m.get_one::<String>("stuff").map(|v| v.as_str()), None);
53 }
54
55 #[test]
multiple_args_and_intermittent_arg_without_value()56 fn multiple_args_and_intermittent_arg_without_value() {
57 let cmd = Command::new("cmd")
58 .ignore_errors(true)
59 .arg(arg!(
60 -c --config[FILE] "Sets a custom config file"
61 ))
62 .arg(arg!(
63 -x --stuff[FILE] "Sets a custom stuff file"
64 ))
65 .arg(arg!(f: -f "Flag").action(ArgAction::SetTrue));
66
67 let r = cmd.try_get_matches_from(vec![
68 "cmd", "-x", /* missing: ,"some stuff" */
69 "-c", "file", "-f",
70 ]);
71
72 assert!(r.is_ok(), "unexpected error: {:?}", r);
73 let m = r.unwrap();
74 assert_eq!(
75 m.get_one::<String>("config").map(|v| v.as_str()),
76 Some("file")
77 );
78 assert!(*m.get_one::<bool>("f").expect("defaulted by clap"));
79 assert_eq!(m.get_one::<String>("stuff").map(|v| v.as_str()), None);
80 }
81
82 #[test]
subcommand()83 fn subcommand() {
84 let cmd = Command::new("test")
85 .ignore_errors(true)
86 .subcommand(
87 Command::new("some")
88 .arg(
89 Arg::new("test")
90 .short('t')
91 .long("test")
92 .action(ArgAction::Set)
93 .help("testing testing"),
94 )
95 .arg(
96 Arg::new("stuff")
97 .short('x')
98 .long("stuff")
99 .action(ArgAction::Set)
100 .help("stuf value"),
101 ),
102 )
103 .arg(Arg::new("other").long("other"));
104
105 let m = cmd
106 .try_get_matches_from(vec![
107 "myprog",
108 "some",
109 "--test", /* missing: ,"some val" */
110 "-x",
111 "some other val",
112 ])
113 .unwrap();
114
115 assert_eq!(m.subcommand_name().unwrap(), "some");
116 let sub_m = m.subcommand_matches("some").unwrap();
117 assert!(
118 sub_m.contains_id("test"),
119 "expected subcommand to be present due to partial parsing"
120 );
121 assert_eq!(sub_m.get_one::<String>("test").map(|v| v.as_str()), None);
122 assert_eq!(
123 sub_m.get_one::<String>("stuff").map(|v| v.as_str()),
124 Some("some other val")
125 );
126 }
127