blob: 0da7bbf9c4eca8a90b39817cadb4926b9b51ec53 [file] [log] [blame]
Pavan Kumar Sunkarab7f76d82020-02-04 08:10:531mod utils;
Alexander Kuvaev7a2375b2015-09-06 20:08:132
Kevin K1ab10272018-01-25 04:05:053use clap::{App, Arg, ArgGroup, ErrorKind};
Alexander Kuvaev7a2375b2015-09-06 20:08:134
Oleksii Filonenko1e399672019-10-02 13:27:195static REQUIRE_EQUALS: &str = "error: The following required arguments were not provided:
Kevin Kc84416f2017-03-17 01:45:456 --opt=<FILE>
7
8USAGE:
9 clap-test --opt=<FILE>
10
11For more information try --help";
12
Oleksii Filonenko1e399672019-10-02 13:27:1913static MISSING_REQ: &str = "error: The following required arguments were not provided:
Kevin Kf9672352017-01-03 04:05:2314 <positional2>
15 --long-option-2 <option2>
16
17USAGE:
Kevin K5a06a822018-07-25 18:46:2818 clap-test <positional2> --long-option-2 <option2> -F
Kevin Kf9672352017-01-03 04:05:2319
20For more information try --help";
21
danieleadesaf454202020-01-11 18:15:4622static COND_REQ_IN_USAGE: &str = "error: The following required arguments were not provided:
Kevin K92919f52017-02-03 22:43:4923 --output <output>
24
25USAGE:
Kevin K5a06a822018-07-25 18:46:2826 test --target <target> --input <input> --output <output>
Kevin K92919f52017-02-03 22:43:4927
28For more information try --help";
29
Alexander Kuvaev7a2375b2015-09-06 20:08:1330#[test]
31fn flag_required() {
32 let result = App::new("flag_required")
Corentin Henry4f602b72018-04-21 18:59:1933 .arg(Arg::from("-f, --flag 'some flag'").requires("color"))
34 .arg(Arg::from("-c, --color 'third flag'"))
Kevin K03333802018-10-19 20:42:1335 .try_get_matches_from(vec!["", "-f"]);
Alexander Kuvaev7a2375b2015-09-06 20:08:1336 assert!(result.is_err());
37 let err = result.err().unwrap();
Kevin K7fc18e62016-01-21 05:18:5338 assert_eq!(err.kind, ErrorKind::MissingRequiredArgument);
Alexander Kuvaev7a2375b2015-09-06 20:08:1339}
40
41#[test]
42fn flag_required_2() {
43 let m = App::new("flag_required")
Corentin Henry4f602b72018-04-21 18:59:1944 .arg(Arg::from("-f, --flag 'some flag'").requires("color"))
45 .arg(Arg::from("-c, --color 'third flag'"))
Kevin Kf17e1502016-01-25 20:56:3746 .get_matches_from(vec!["", "-f", "-c"]);
Alexander Kuvaev7a2375b2015-09-06 20:08:1347 assert!(m.is_present("color"));
48 assert!(m.is_present("flag"));
49}
50
51#[test]
52fn option_required() {
53 let result = App::new("option_required")
Corentin Henry4f602b72018-04-21 18:59:1954 .arg(Arg::from("-f [flag] 'some flag'").requires("c"))
55 .arg(Arg::from("-c [color] 'third flag'"))
Kevin K03333802018-10-19 20:42:1356 .try_get_matches_from(vec!["", "-f", "val"]);
Alexander Kuvaev7a2375b2015-09-06 20:08:1357 assert!(result.is_err());
58 let err = result.err().unwrap();
Kevin K7fc18e62016-01-21 05:18:5359 assert_eq!(err.kind, ErrorKind::MissingRequiredArgument);
Alexander Kuvaev7a2375b2015-09-06 20:08:1360}
61
62#[test]
63fn option_required_2() {
64 let m = App::new("option_required")
Corentin Henry4f602b72018-04-21 18:59:1965 .arg(Arg::from("-f [flag] 'some flag'").requires("c"))
66 .arg(Arg::from("-c [color] 'third flag'"))
Kevin Kf17e1502016-01-25 20:56:3767 .get_matches_from(vec!["", "-f", "val", "-c", "other_val"]);
68 assert!(m.is_present("c"));
69 assert_eq!(m.value_of("c").unwrap(), "other_val");
70 assert!(m.is_present("f"));
71 assert_eq!(m.value_of("f").unwrap(), "val");
Alexander Kuvaev7a2375b2015-09-06 20:08:1372}
73
74#[test]
75fn positional_required() {
76 let result = App::new("positional_required")
Kevin K1ab10272018-01-25 04:05:0577 .arg(Arg::with_name("flag").index(1).required(true))
Kevin K03333802018-10-19 20:42:1378 .try_get_matches_from(vec![""]);
Alexander Kuvaev7a2375b2015-09-06 20:08:1379 assert!(result.is_err());
80 let err = result.err().unwrap();
Kevin K7fc18e62016-01-21 05:18:5381 assert_eq!(err.kind, ErrorKind::MissingRequiredArgument);
Alexander Kuvaev7a2375b2015-09-06 20:08:1382}
83
84#[test]
85fn positional_required_2() {
86 let m = App::new("positional_required")
Kevin K1ab10272018-01-25 04:05:0587 .arg(Arg::with_name("flag").index(1).required(true))
Kevin Kf17e1502016-01-25 20:56:3788 .get_matches_from(vec!["", "someval"]);
Alexander Kuvaev7a2375b2015-09-06 20:08:1389 assert!(m.is_present("flag"));
90 assert_eq!(m.value_of("flag").unwrap(), "someval");
91}
92
93#[test]
94fn group_required() {
95 let result = App::new("group_required")
Corentin Henry4f602b72018-04-21 18:59:1996 .arg(Arg::from("-f, --flag 'some flag'"))
Kevin K1ab10272018-01-25 04:05:0597 .group(
98 ArgGroup::with_name("gr")
99 .required(true)
100 .arg("some")
101 .arg("other"),
102 )
Corentin Henry4f602b72018-04-21 18:59:19103 .arg(Arg::from("--some 'some arg'"))
104 .arg(Arg::from("--other 'other arg'"))
Kevin K03333802018-10-19 20:42:13105 .try_get_matches_from(vec!["", "-f"]);
Alexander Kuvaev7a2375b2015-09-06 20:08:13106 assert!(result.is_err());
107 let err = result.err().unwrap();
Kevin K7fc18e62016-01-21 05:18:53108 assert_eq!(err.kind, ErrorKind::MissingRequiredArgument);
Alexander Kuvaev7a2375b2015-09-06 20:08:13109}
110
111#[test]
112fn group_required_2() {
113 let m = App::new("group_required")
Corentin Henry4f602b72018-04-21 18:59:19114 .arg(Arg::from("-f, --flag 'some flag'"))
Kevin K1ab10272018-01-25 04:05:05115 .group(
116 ArgGroup::with_name("gr")
117 .required(true)
118 .arg("some")
119 .arg("other"),
120 )
Corentin Henry4f602b72018-04-21 18:59:19121 .arg(Arg::from("--some 'some arg'"))
122 .arg(Arg::from("--other 'other arg'"))
Kevin Kf17e1502016-01-25 20:56:37123 .get_matches_from(vec!["", "-f", "--some"]);
Alexander Kuvaev7a2375b2015-09-06 20:08:13124 assert!(m.is_present("some"));
125 assert!(!m.is_present("other"));
126 assert!(m.is_present("flag"));
127}
128
129#[test]
130fn group_required_3() {
131 let m = App::new("group_required")
Corentin Henry4f602b72018-04-21 18:59:19132 .arg(Arg::from("-f, --flag 'some flag'"))
Kevin K1ab10272018-01-25 04:05:05133 .group(
134 ArgGroup::with_name("gr")
135 .required(true)
136 .arg("some")
137 .arg("other"),
138 )
Corentin Henry4f602b72018-04-21 18:59:19139 .arg(Arg::from("--some 'some arg'"))
140 .arg(Arg::from("--other 'other arg'"))
Kevin Kf17e1502016-01-25 20:56:37141 .get_matches_from(vec!["", "-f", "--other"]);
Alexander Kuvaev7a2375b2015-09-06 20:08:13142 assert!(!m.is_present("some"));
143 assert!(m.is_present("other"));
144 assert!(m.is_present("flag"));
145}
146
147#[test]
148fn arg_require_group() {
149 let result = App::new("arg_require_group")
Corentin Henry4f602b72018-04-21 18:59:19150 .arg(Arg::from("-f, --flag 'some flag'").requires("gr"))
Kevin K1ab10272018-01-25 04:05:05151 .group(ArgGroup::with_name("gr").arg("some").arg("other"))
Corentin Henry4f602b72018-04-21 18:59:19152 .arg(Arg::from("--some 'some arg'"))
153 .arg(Arg::from("--other 'other arg'"))
Kevin K5a06a822018-07-25 18:46:28154 .try_get_matches_from(vec!["", "-f"]);
Alexander Kuvaev7a2375b2015-09-06 20:08:13155 assert!(result.is_err());
156 let err = result.err().unwrap();
Kevin K7fc18e62016-01-21 05:18:53157 assert_eq!(err.kind, ErrorKind::MissingRequiredArgument);
Alexander Kuvaev7a2375b2015-09-06 20:08:13158}
159
160#[test]
161fn arg_require_group_2() {
Kevin K5a06a822018-07-25 18:46:28162 let res = App::new("arg_require_group")
Corentin Henry4f602b72018-04-21 18:59:19163 .arg(Arg::from("-f, --flag 'some flag'").requires("gr"))
Kevin K1ab10272018-01-25 04:05:05164 .group(ArgGroup::with_name("gr").arg("some").arg("other"))
Corentin Henry4f602b72018-04-21 18:59:19165 .arg(Arg::from("--some 'some arg'"))
166 .arg(Arg::from("--other 'other arg'"))
Kevin K5a06a822018-07-25 18:46:28167 .try_get_matches_from(vec!["", "-f", "--some"]);
168 assert!(res.is_ok());
169 let m = res.unwrap();
Alexander Kuvaev7a2375b2015-09-06 20:08:13170 assert!(m.is_present("some"));
171 assert!(!m.is_present("other"));
172 assert!(m.is_present("flag"));
173}
174
175#[test]
176fn arg_require_group_3() {
Kevin K5a06a822018-07-25 18:46:28177 let res = App::new("arg_require_group")
Corentin Henry4f602b72018-04-21 18:59:19178 .arg(Arg::from("-f, --flag 'some flag'").requires("gr"))
Kevin K1ab10272018-01-25 04:05:05179 .group(ArgGroup::with_name("gr").arg("some").arg("other"))
Corentin Henry4f602b72018-04-21 18:59:19180 .arg(Arg::from("--some 'some arg'"))
181 .arg(Arg::from("--other 'other arg'"))
Kevin K5a06a822018-07-25 18:46:28182 .try_get_matches_from(vec!["", "-f", "--other"]);
183 assert!(res.is_ok());
184 let m = res.unwrap();
Alexander Kuvaev7a2375b2015-09-06 20:08:13185 assert!(!m.is_present("some"));
186 assert!(m.is_present("other"));
187 assert!(m.is_present("flag"));
Kevin Kf17e1502016-01-25 20:56:37188}
Kevin K9fdad2e2016-05-02 18:12:57189
190// REQUIRED_UNLESS
191
192#[test]
Kevin Kd20331b2016-11-20 14:48:16193fn issue_753() {
194 let m = App::new("test")
Corentin Henry4f602b72018-04-21 18:59:19195 .arg(Arg::from(
Kevin K1ab10272018-01-25 04:05:05196 "-l, --list 'List available interfaces (and stop there)'",
197 ))
198 .arg(
Kevin K5a06a822018-07-25 18:46:28199 Arg::from("-i, --iface=[INTERFACE] 'Ethernet interface for fetching NTP packets'")
200 .required_unless("list"),
Kevin K1ab10272018-01-25 04:05:05201 )
202 .arg(
Corentin Henry4f602b72018-04-21 18:59:19203 Arg::from("-f, --file=[TESTFILE] 'Fetch NTP packets from pcap file'")
Kevin K1ab10272018-01-25 04:05:05204 .conflicts_with("iface")
205 .required_unless("list"),
206 )
Kevin K5a06a822018-07-25 18:46:28207 .arg(Arg::from("-s, --server=[SERVER_IP] 'NTP server IP address'").required_unless("list"))
Corentin Henry4f602b72018-04-21 18:59:19208 .arg(Arg::from("-p, --port=[SERVER_PORT] 'NTP server port'").default_value("123"))
Kevin K03333802018-10-19 20:42:13209 .try_get_matches_from(vec!["test", "--list"]);
Kevin K92919f52017-02-03 22:43:49210 assert!(m.is_ok());
Kevin Kd20331b2016-11-20 14:48:16211}
212
213#[test]
Kevin K9fdad2e2016-05-02 18:12:57214fn required_unless() {
215 let res = App::new("unlesstest")
Kevin K1ab10272018-01-25 04:05:05216 .arg(
217 Arg::with_name("cfg")
218 .required_unless("dbg")
219 .takes_value(true)
220 .long("config"),
221 )
Kevin K92919f52017-02-03 22:43:49222 .arg(Arg::with_name("dbg").long("debug"))
Kevin K03333802018-10-19 20:42:13223 .try_get_matches_from(vec!["unlesstest", "--debug"]);
Kevin K9fdad2e2016-05-02 18:12:57224
225 assert!(res.is_ok());
226 let m = res.unwrap();
227 assert!(m.is_present("dbg"));
228 assert!(!m.is_present("cfg"));
229}
230
231#[test]
232fn required_unless_err() {
233 let res = App::new("unlesstest")
Kevin K1ab10272018-01-25 04:05:05234 .arg(
235 Arg::with_name("cfg")
236 .required_unless("dbg")
237 .takes_value(true)
238 .long("config"),
239 )
Kevin K92919f52017-02-03 22:43:49240 .arg(Arg::with_name("dbg").long("debug"))
Kevin K03333802018-10-19 20:42:13241 .try_get_matches_from(vec!["unlesstest"]);
Kevin K9fdad2e2016-05-02 18:12:57242
243 assert!(res.is_err());
244 assert_eq!(res.unwrap_err().kind, ErrorKind::MissingRequiredArgument);
245}
246
247// REQUIRED_UNLESS_ALL
248
249#[test]
250fn required_unless_all() {
251 let res = App::new("unlessall")
Kevin K1ab10272018-01-25 04:05:05252 .arg(
253 Arg::with_name("cfg")
254 .required_unless_all(&["dbg", "infile"])
255 .takes_value(true)
256 .long("config"),
257 )
Kevin K92919f52017-02-03 22:43:49258 .arg(Arg::with_name("dbg").long("debug"))
Kevin K94872e02018-07-23 19:09:42259 .arg(Arg::with_name("infile").short('i').takes_value(true))
Kevin K03333802018-10-19 20:42:13260 .try_get_matches_from(vec!["unlessall", "--debug", "-i", "file"]);
Kevin K9fdad2e2016-05-02 18:12:57261
262 assert!(res.is_ok());
263 let m = res.unwrap();
264 assert!(m.is_present("dbg"));
265 assert!(m.is_present("infile"));
266 assert!(!m.is_present("cfg"));
267}
268
269#[test]
270fn required_unless_all_err() {
271 let res = App::new("unlessall")
Kevin K1ab10272018-01-25 04:05:05272 .arg(
273 Arg::with_name("cfg")
274 .required_unless_all(&["dbg", "infile"])
275 .takes_value(true)
276 .long("config"),
277 )
Kevin K92919f52017-02-03 22:43:49278 .arg(Arg::with_name("dbg").long("debug"))
Kevin K94872e02018-07-23 19:09:42279 .arg(Arg::with_name("infile").short('i').takes_value(true))
Kevin K03333802018-10-19 20:42:13280 .try_get_matches_from(vec!["unlessall", "--debug"]);
Kevin K9fdad2e2016-05-02 18:12:57281
282 assert!(res.is_err());
283 assert_eq!(res.unwrap_err().kind, ErrorKind::MissingRequiredArgument);
284}
285
286// REQUIRED_UNLESS_ONE
287
288#[test]
289fn required_unless_one() {
290 let res = App::new("unlessone")
Kevin K1ab10272018-01-25 04:05:05291 .arg(
292 Arg::with_name("cfg")
293 .required_unless_one(&["dbg", "infile"])
294 .takes_value(true)
295 .long("config"),
296 )
Kevin K92919f52017-02-03 22:43:49297 .arg(Arg::with_name("dbg").long("debug"))
Kevin K94872e02018-07-23 19:09:42298 .arg(Arg::with_name("infile").short('i').takes_value(true))
Kevin K03333802018-10-19 20:42:13299 .try_get_matches_from(vec!["unlessone", "--debug"]);
Kevin K9fdad2e2016-05-02 18:12:57300
301 assert!(res.is_ok());
302 let m = res.unwrap();
303 assert!(m.is_present("dbg"));
304 assert!(!m.is_present("cfg"));
305}
306
307#[test]
Christopher Field1b990912016-07-23 17:43:22308fn required_unless_one_2() {
309 // This tests that the required_unless_one works when the second arg in the array is used
310 // instead of the first.
311 let res = App::new("unlessone")
Kevin K1ab10272018-01-25 04:05:05312 .arg(
313 Arg::with_name("cfg")
314 .required_unless_one(&["dbg", "infile"])
315 .takes_value(true)
316 .long("config"),
317 )
Kevin K92919f52017-02-03 22:43:49318 .arg(Arg::with_name("dbg").long("debug"))
Kevin K94872e02018-07-23 19:09:42319 .arg(Arg::with_name("infile").short('i').takes_value(true))
Kevin K03333802018-10-19 20:42:13320 .try_get_matches_from(vec!["unlessone", "-i", "file"]);
Christopher Field1b990912016-07-23 17:43:22321
322 assert!(res.is_ok());
323 let m = res.unwrap();
324 assert!(m.is_present("infile"));
325 assert!(!m.is_present("cfg"));
326}
327
328#[test]
Will Murphy63cc4bc2017-12-26 16:16:18329fn required_unless_one_works_with_short() {
330 // GitHub issue: https://github.com/kbknapp/clap-rs/issues/1135
Will Murphy63cc4bc2017-12-26 16:16:18331 let res = App::new("unlessone")
Kevin K94872e02018-07-23 19:09:42332 .arg(Arg::with_name("a").conflicts_with("b").short('a'))
333 .arg(Arg::with_name("b").short('b'))
Will Murphy63cc4bc2017-12-26 16:16:18334 .arg(
335 Arg::with_name("x")
Kevin K94872e02018-07-23 19:09:42336 .short('x')
Kevin K1ab10272018-01-25 04:05:05337 .required_unless_one(&["a", "b"]),
338 )
Kevin K03333802018-10-19 20:42:13339 .try_get_matches_from(vec!["unlessone", "-a"]);
Will Murphy63cc4bc2017-12-26 16:16:18340
341 assert!(res.is_ok());
342}
343
344#[test]
Will Murphyc759d202018-01-19 01:41:23345fn required_unless_one_works_with_short_err() {
346 let res = App::new("unlessone")
Kevin K94872e02018-07-23 19:09:42347 .arg(Arg::with_name("a").conflicts_with("b").short('a'))
348 .arg(Arg::with_name("b").short('b'))
Will Murphyc759d202018-01-19 01:41:23349 .arg(
350 Arg::with_name("x")
Kevin K94872e02018-07-23 19:09:42351 .short('x')
Kevin K1ab10272018-01-25 04:05:05352 .required_unless_one(&["a", "b"]),
353 )
Kevin K03333802018-10-19 20:42:13354 .try_get_matches_from(vec!["unlessone"]);
Will Murphyc759d202018-01-19 01:41:23355
356 assert!(!res.is_ok());
357}
358
359#[test]
Will Murphy74976a02018-01-19 01:30:12360fn required_unless_one_works_without() {
Will Murphy74976a02018-01-19 01:30:12361 let res = App::new("unlessone")
Kevin K94872e02018-07-23 19:09:42362 .arg(Arg::with_name("a").conflicts_with("b").short('a'))
363 .arg(Arg::with_name("b").short('b'))
Kevin K1ab10272018-01-25 04:05:05364 .arg(Arg::with_name("x").required_unless_one(&["a", "b"]))
Kevin K03333802018-10-19 20:42:13365 .try_get_matches_from(vec!["unlessone", "-a"]);
Will Murphy74976a02018-01-19 01:30:12366
367 assert!(res.is_ok());
368}
369
370#[test]
371fn required_unless_one_works_with_long() {
372 let res = App::new("unlessone")
Kevin K94872e02018-07-23 19:09:42373 .arg(Arg::with_name("a").conflicts_with("b").short('a'))
374 .arg(Arg::with_name("b").short('b'))
Will Murphy74976a02018-01-19 01:30:12375 .arg(
376 Arg::with_name("x")
377 .long("x_is_the_option")
Kevin K1ab10272018-01-25 04:05:05378 .required_unless_one(&["a", "b"]),
379 )
Kevin K03333802018-10-19 20:42:13380 .try_get_matches_from(vec!["unlessone", "-a"]);
Will Murphy74976a02018-01-19 01:30:12381
382 assert!(res.is_ok());
383}
384
385#[test]
Kevin K625cbbc2016-07-23 21:22:14386fn required_unless_one_1() {
387 let res = App::new("unlessone")
Kevin K1ab10272018-01-25 04:05:05388 .arg(
389 Arg::with_name("cfg")
390 .required_unless_one(&["dbg", "infile"])
391 .takes_value(true)
392 .long("config"),
393 )
Kevin K92919f52017-02-03 22:43:49394 .arg(Arg::with_name("dbg").long("debug"))
Kevin K94872e02018-07-23 19:09:42395 .arg(Arg::with_name("infile").short('i').takes_value(true))
Kevin K03333802018-10-19 20:42:13396 .try_get_matches_from(vec!["unlessone", "--debug"]);
Kevin K625cbbc2016-07-23 21:22:14397
398 assert!(res.is_ok());
399 let m = res.unwrap();
400 assert!(!m.is_present("infile"));
401 assert!(!m.is_present("cfg"));
402 assert!(m.is_present("dbg"));
403}
404
405#[test]
Kevin K9fdad2e2016-05-02 18:12:57406fn required_unless_one_err() {
407 let res = App::new("unlessone")
Kevin K1ab10272018-01-25 04:05:05408 .arg(
409 Arg::with_name("cfg")
410 .required_unless_one(&["dbg", "infile"])
411 .takes_value(true)
412 .long("config"),
413 )
Kevin K92919f52017-02-03 22:43:49414 .arg(Arg::with_name("dbg").long("debug"))
Kevin K94872e02018-07-23 19:09:42415 .arg(Arg::with_name("infile").short('i').takes_value(true))
Kevin K03333802018-10-19 20:42:13416 .try_get_matches_from(vec!["unlessone"]);
Kevin K9fdad2e2016-05-02 18:12:57417
418 assert!(res.is_err());
419 assert_eq!(res.unwrap_err().kind, ErrorKind::MissingRequiredArgument);
420}
Kevin K852e5812016-05-09 03:20:50421
422#[test]
423fn missing_required_output() {
Pavan Kumar Sunkarab7f76d82020-02-04 08:10:53424 assert!(utils::compare_output(
425 utils::complex_app(),
Kevin K1ab10272018-01-25 04:05:05426 "clap-test -F",
427 MISSING_REQ,
428 true
429 ));
Kevin K852e5812016-05-09 03:20:50430}
Kevin Keca60912016-12-14 16:41:21431
432// Conditional external requirements
433
434#[test]
435fn requires_if_present_val() {
436 let res = App::new("unlessone")
Kevin K1ab10272018-01-25 04:05:05437 .arg(
438 Arg::with_name("cfg")
439 .requires_if("my.cfg", "extra")
440 .takes_value(true)
441 .long("config"),
442 )
Kevin K92919f52017-02-03 22:43:49443 .arg(Arg::with_name("extra").long("extra"))
Kevin K03333802018-10-19 20:42:13444 .try_get_matches_from(vec!["unlessone", "--config=my.cfg"]);
Kevin Keca60912016-12-14 16:41:21445
446 assert!(res.is_err());
447 assert_eq!(res.unwrap_err().kind, ErrorKind::MissingRequiredArgument);
448}
449
450#[test]
451fn requires_if_present_mult() {
452 let res = App::new("unlessone")
Kevin K1ab10272018-01-25 04:05:05453 .arg(
454 Arg::with_name("cfg")
455 .requires_ifs(&[("my.cfg", "extra"), ("other.cfg", "other")])
456 .takes_value(true)
457 .long("config"),
458 )
Kevin K92919f52017-02-03 22:43:49459 .arg(Arg::with_name("extra").long("extra"))
460 .arg(Arg::with_name("other").long("other"))
Kevin K03333802018-10-19 20:42:13461 .try_get_matches_from(vec!["unlessone", "--config=other.cfg"]);
Kevin Keca60912016-12-14 16:41:21462
463 assert!(res.is_err());
464 assert_eq!(res.unwrap_err().kind, ErrorKind::MissingRequiredArgument);
465}
466
467#[test]
468fn requires_if_present_mult_pass() {
469 let res = App::new("unlessone")
Kevin K1ab10272018-01-25 04:05:05470 .arg(
471 Arg::with_name("cfg")
472 .requires_ifs(&[("my.cfg", "extra"), ("other.cfg", "other")])
473 .takes_value(true)
474 .long("config"),
475 )
Kevin K92919f52017-02-03 22:43:49476 .arg(Arg::with_name("extra").long("extra"))
477 .arg(Arg::with_name("other").long("other"))
Kevin K03333802018-10-19 20:42:13478 .try_get_matches_from(vec!["unlessone", "--config=some.cfg"]);
Kevin Keca60912016-12-14 16:41:21479
480 assert!(res.is_ok());
Kevin Keca60912016-12-14 16:41:21481}
482
483#[test]
484fn requires_if_present_val_no_present_pass() {
485 let res = App::new("unlessone")
Kevin K1ab10272018-01-25 04:05:05486 .arg(
487 Arg::with_name("cfg")
488 .requires_if("my.cfg", "extra")
489 .takes_value(true)
490 .long("config"),
491 )
Kevin K92919f52017-02-03 22:43:49492 .arg(Arg::with_name("extra").long("extra"))
Kevin K03333802018-10-19 20:42:13493 .try_get_matches_from(vec!["unlessone"]);
Kevin Keca60912016-12-14 16:41:21494
495 assert!(res.is_ok());
Kevin K60e1a3a2016-12-29 01:35:34496}
497
498// Conditionally required
499
500#[test]
501fn required_if_val_present_pass() {
502 let res = App::new("ri")
Kevin K1ab10272018-01-25 04:05:05503 .arg(
504 Arg::with_name("cfg")
505 .required_if("extra", "val")
506 .takes_value(true)
507 .long("config"),
508 )
509 .arg(Arg::with_name("extra").takes_value(true).long("extra"))
Kevin K03333802018-10-19 20:42:13510 .try_get_matches_from(vec!["ri", "--extra", "val", "--config", "my.cfg"]);
Kevin K60e1a3a2016-12-29 01:35:34511
512 assert!(res.is_ok());
Kevin K60e1a3a2016-12-29 01:35:34513}
514
515#[test]
516fn required_if_val_present_fail() {
517 let res = App::new("ri")
Kevin K1ab10272018-01-25 04:05:05518 .arg(
519 Arg::with_name("cfg")
520 .required_if("extra", "val")
521 .takes_value(true)
522 .long("config"),
523 )
524 .arg(Arg::with_name("extra").takes_value(true).long("extra"))
Kevin K03333802018-10-19 20:42:13525 .try_get_matches_from(vec!["ri", "--extra", "val"]);
Kevin K60e1a3a2016-12-29 01:35:34526
527 assert!(res.is_err());
528 assert_eq!(res.unwrap_err().kind, ErrorKind::MissingRequiredArgument);
529}
530
531#[test]
Kevin K92919f52017-02-03 22:43:49532fn required_if_val_present_fail_error_output() {
533 let app = App::new("Test app")
534 .version("1.0")
535 .author("F0x06")
536 .about("Arg test")
Kevin K1ab10272018-01-25 04:05:05537 .arg(
538 Arg::with_name("target")
539 .takes_value(true)
540 .required(true)
541 .possible_values(&["file", "stdout"])
542 .long("target"),
543 )
544 .arg(
545 Arg::with_name("input")
546 .takes_value(true)
547 .required(true)
548 .long("input"),
549 )
550 .arg(
551 Arg::with_name("output")
552 .takes_value(true)
553 .required_if("target", "file")
554 .long("output"),
555 );
Kevin K92919f52017-02-03 22:43:49556
Pavan Kumar Sunkarab7f76d82020-02-04 08:10:53557 assert!(utils::compare_output(
Kevin K1ab10272018-01-25 04:05:05558 app,
559 "test --input somepath --target file",
560 COND_REQ_IN_USAGE,
561 true
562 ));
Kevin K92919f52017-02-03 22:43:49563}
564
565#[test]
Kevin K60e1a3a2016-12-29 01:35:34566fn required_if_wrong_val() {
567 let res = App::new("ri")
Kevin K1ab10272018-01-25 04:05:05568 .arg(
569 Arg::with_name("cfg")
570 .required_if("extra", "val")
571 .takes_value(true)
572 .long("config"),
573 )
574 .arg(Arg::with_name("extra").takes_value(true).long("extra"))
Kevin K03333802018-10-19 20:42:13575 .try_get_matches_from(vec!["ri", "--extra", "other"]);
Kevin K60e1a3a2016-12-29 01:35:34576
577 assert!(res.is_ok());
578}
579
580#[test]
581fn required_ifs_val_present_pass() {
582 let res = App::new("ri")
Kevin K1ab10272018-01-25 04:05:05583 .arg(
584 Arg::with_name("cfg")
585 .required_ifs(&[("extra", "val"), ("option", "spec")])
586 .takes_value(true)
587 .long("config"),
588 )
589 .arg(Arg::with_name("option").takes_value(true).long("option"))
590 .arg(Arg::with_name("extra").takes_value(true).long("extra"))
Kevin K03333802018-10-19 20:42:13591 .try_get_matches_from(vec!["ri", "--option", "spec", "--config", "my.cfg"]);
Kevin K60e1a3a2016-12-29 01:35:34592
593 assert!(res.is_ok());
Kevin K60e1a3a2016-12-29 01:35:34594}
595
596#[test]
597fn required_ifs_val_present_fail() {
598 let res = App::new("ri")
Kevin K1ab10272018-01-25 04:05:05599 .arg(
600 Arg::with_name("cfg")
601 .required_ifs(&[("extra", "val"), ("option", "spec")])
602 .takes_value(true)
603 .long("config"),
604 )
605 .arg(Arg::with_name("extra").takes_value(true).long("extra"))
606 .arg(Arg::with_name("option").takes_value(true).long("option"))
Kevin K03333802018-10-19 20:42:13607 .try_get_matches_from(vec!["ri", "--option", "spec"]);
Kevin K60e1a3a2016-12-29 01:35:34608
609 assert!(res.is_err());
610 assert_eq!(res.unwrap_err().kind, ErrorKind::MissingRequiredArgument);
611}
612
613#[test]
614fn required_ifs_wrong_val() {
615 let res = App::new("ri")
Kevin K1ab10272018-01-25 04:05:05616 .arg(
617 Arg::with_name("cfg")
618 .required_ifs(&[("extra", "val"), ("option", "spec")])
619 .takes_value(true)
620 .long("config"),
621 )
622 .arg(Arg::with_name("extra").takes_value(true).long("extra"))
623 .arg(Arg::with_name("option").takes_value(true).long("option"))
Kevin K03333802018-10-19 20:42:13624 .try_get_matches_from(vec!["ri", "--option", "other"]);
Kevin K60e1a3a2016-12-29 01:35:34625
626 assert!(res.is_ok());
627}
628
629#[test]
630fn required_ifs_wrong_val_mult_fail() {
631 let res = App::new("ri")
Kevin K1ab10272018-01-25 04:05:05632 .arg(
633 Arg::with_name("cfg")
634 .required_ifs(&[("extra", "val"), ("option", "spec")])
635 .takes_value(true)
636 .long("config"),
637 )
638 .arg(Arg::with_name("extra").takes_value(true).long("extra"))
639 .arg(Arg::with_name("option").takes_value(true).long("option"))
Kevin K03333802018-10-19 20:42:13640 .try_get_matches_from(vec!["ri", "--extra", "other", "--option", "spec"]);
Kevin K60e1a3a2016-12-29 01:35:34641
642 assert!(res.is_err());
643 assert_eq!(res.unwrap_err().kind, ErrorKind::MissingRequiredArgument);
Kevin K0efa4112017-02-21 04:51:20644}
Kevin Kc84416f2017-03-17 01:45:45645
646#[test]
647fn require_eq() {
Kevin K1ab10272018-01-25 04:05:05648 let app = App::new("clap-test").version("v1.4.8").arg(
649 Arg::with_name("opt")
Kevin Kc84416f2017-03-17 01:45:45650 .long("opt")
Kevin K94872e02018-07-23 19:09:42651 .short('o')
Kevin Kc84416f2017-03-17 01:45:45652 .required(true)
653 .require_equals(true)
654 .value_name("FILE")
Kevin K1ab10272018-01-25 04:05:05655 .help("some"),
656 );
Pavan Kumar Sunkarab7f76d82020-02-04 08:10:53657 assert!(utils::compare_output(
658 app,
659 "clap-test",
660 REQUIRE_EQUALS,
661 true
662 ));
Kevin K1ab10272018-01-25 04:05:05663}
Kevin K5a06a822018-07-25 18:46:28664
Oleksii Filonenko1e399672019-10-02 13:27:19665static ISSUE_1158: &str = "error: The following required arguments were not provided:
Kevin K5a06a822018-07-25 18:46:28666 -x <X>
667 -y <Y>
668 -z <Z>
669
670USAGE:
671 example <ID> -x <X> -y <Y> -z <Z>
672
673For more information try --help";
674
Kevin K4cc85992019-04-05 02:06:23675fn issue_1158_app() -> App<'static> {
Kevin K5a06a822018-07-25 18:46:28676 App::new("example")
677 .arg(
Kevin K03333802018-10-19 20:42:13678 Arg::from("-c, --config [FILE] 'Custom config file.'")
Kevin K5a06a822018-07-25 18:46:28679 .required_unless("ID")
680 .conflicts_with("ID"),
681 )
682 .arg(
Kevin K03333802018-10-19 20:42:13683 Arg::from("[ID] 'ID'")
Kevin K5a06a822018-07-25 18:46:28684 .required_unless("config")
685 .conflicts_with("config")
686 .requires_all(&["x", "y", "z"]),
687 )
Kevin K03333802018-10-19 20:42:13688 .arg(Arg::from("-x [X] 'X'"))
689 .arg(Arg::from("-y [Y] 'Y'"))
690 .arg(Arg::from("-z [Z] 'Z'"))
Kevin K5a06a822018-07-25 18:46:28691}
692
693#[test]
694fn issue_1158_conflicting_requirements() {
695 let app = issue_1158_app();
696
Pavan Kumar Sunkarab7f76d82020-02-04 08:10:53697 assert!(utils::compare_output(app, "example id", ISSUE_1158, true));
Kevin K5a06a822018-07-25 18:46:28698}
699
700#[test]
701fn issue_1158_conflicting_requirements_rev() {
702 let res = issue_1158_app().try_get_matches_from(&["", "--config", "some.conf"]);
703
704 assert!(res.is_ok());
705}