2021-11-30 15:31:19 +00:00
|
|
|
use crate::utils;
|
2015-09-06 20:08:13 +00:00
|
|
|
|
2022-02-12 03:48:29 +00:00
|
|
|
use clap::{arg, error::ErrorKind, Arg, ArgGroup, Command};
|
2015-09-06 20:08:13 +00:00
|
|
|
|
2019-10-02 13:27:19 +00:00
|
|
|
static REQUIRE_EQUALS: &str = "error: The following required arguments were not provided:
|
2017-03-17 01:45:45 +00:00
|
|
|
--opt=<FILE>
|
|
|
|
|
|
|
|
USAGE:
|
|
|
|
clap-test --opt=<FILE>
|
|
|
|
|
2021-09-24 15:58:39 +00:00
|
|
|
For more information try --help
|
|
|
|
";
|
2017-03-17 01:45:45 +00:00
|
|
|
|
2020-05-15 03:14:47 +00:00
|
|
|
static REQUIRE_EQUALS_FILTERED: &str = "error: The following required arguments were not provided:
|
|
|
|
--opt=<FILE>
|
|
|
|
|
|
|
|
USAGE:
|
|
|
|
clap-test --opt=<FILE> --foo=<FILE>
|
|
|
|
|
2021-09-24 15:58:39 +00:00
|
|
|
For more information try --help
|
|
|
|
";
|
2020-05-15 03:14:47 +00:00
|
|
|
|
|
|
|
static REQUIRE_EQUALS_FILTERED_GROUP: &str =
|
|
|
|
"error: The following required arguments were not provided:
|
|
|
|
--opt=<FILE>
|
|
|
|
|
|
|
|
USAGE:
|
|
|
|
clap-test --opt=<FILE> --foo=<FILE> <--g1=<FILE>|--g2=<FILE>>
|
|
|
|
|
2021-09-24 15:58:39 +00:00
|
|
|
For more information try --help
|
|
|
|
";
|
2020-05-15 03:14:47 +00:00
|
|
|
|
2019-10-02 13:27:19 +00:00
|
|
|
static MISSING_REQ: &str = "error: The following required arguments were not provided:
|
2017-01-03 04:05:23 +00:00
|
|
|
--long-option-2 <option2>
|
2021-05-20 18:59:21 +00:00
|
|
|
<positional2>
|
2017-01-03 04:05:23 +00:00
|
|
|
|
|
|
|
USAGE:
|
2021-05-20 18:59:21 +00:00
|
|
|
clap-test --long-option-2 <option2> -F <positional2>
|
2017-01-03 04:05:23 +00:00
|
|
|
|
2021-09-24 15:58:39 +00:00
|
|
|
For more information try --help
|
|
|
|
";
|
2017-01-03 04:05:23 +00:00
|
|
|
|
2020-01-11 18:15:46 +00:00
|
|
|
static COND_REQ_IN_USAGE: &str = "error: The following required arguments were not provided:
|
2017-02-03 22:43:49 +00:00
|
|
|
--output <output>
|
|
|
|
|
|
|
|
USAGE:
|
2018-07-25 18:46:28 +00:00
|
|
|
test --target <target> --input <input> --output <output>
|
2017-02-03 22:43:49 +00:00
|
|
|
|
2021-09-24 15:58:39 +00:00
|
|
|
For more information try --help
|
|
|
|
";
|
2017-02-03 22:43:49 +00:00
|
|
|
|
2015-09-06 20:08:13 +00:00
|
|
|
#[test]
|
|
|
|
fn flag_required() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let result = Command::new("flag_required")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(-f --flag "some flag").requires("color"))
|
|
|
|
.arg(arg!(-c --color "third flag"))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "-f"]);
|
2015-09-06 20:08:13 +00:00
|
|
|
assert!(result.is_err());
|
|
|
|
let err = result.err().unwrap();
|
2022-01-25 22:19:28 +00:00
|
|
|
assert_eq!(err.kind(), ErrorKind::MissingRequiredArgument);
|
2015-09-06 20:08:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn flag_required_2() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::new("flag_required")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(-f --flag "some flag").requires("color"))
|
|
|
|
.arg(arg!(-c --color "third flag"))
|
2021-12-27 18:56:12 +00:00
|
|
|
.try_get_matches_from(vec!["", "-f", "-c"])
|
|
|
|
.unwrap();
|
2015-09-06 20:08:13 +00:00
|
|
|
assert!(m.is_present("color"));
|
|
|
|
assert!(m.is_present("flag"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn option_required() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let result = Command::new("option_required")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(f: -f <flag> "some flag").required(false).requires("c"))
|
|
|
|
.arg(arg!(c: -c <color> "third flag").required(false))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "-f", "val"]);
|
2015-09-06 20:08:13 +00:00
|
|
|
assert!(result.is_err());
|
|
|
|
let err = result.err().unwrap();
|
2022-01-25 22:19:28 +00:00
|
|
|
assert_eq!(err.kind(), ErrorKind::MissingRequiredArgument);
|
2015-09-06 20:08:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn option_required_2() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::new("option_required")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(f: -f <flag> "some flag").required(false).requires("c"))
|
|
|
|
.arg(arg!(c: -c <color> "third flag").required(false))
|
2021-12-27 18:56:12 +00:00
|
|
|
.try_get_matches_from(vec!["", "-f", "val", "-c", "other_val"])
|
|
|
|
.unwrap();
|
2016-01-25 20:56:37 +00:00
|
|
|
assert!(m.is_present("c"));
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("c").map(|v| v.as_str()).unwrap(),
|
|
|
|
"other_val"
|
|
|
|
);
|
2016-01-25 20:56:37 +00:00
|
|
|
assert!(m.is_present("f"));
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(m.get_one::<String>("f").map(|v| v.as_str()).unwrap(), "val");
|
2015-09-06 20:08:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn positional_required() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let result = Command::new("positional_required")
|
2020-05-14 20:50:56 +00:00
|
|
|
.arg(Arg::new("flag").index(1).required(true))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec![""]);
|
2015-09-06 20:08:13 +00:00
|
|
|
assert!(result.is_err());
|
|
|
|
let err = result.err().unwrap();
|
2022-01-25 22:19:28 +00:00
|
|
|
assert_eq!(err.kind(), ErrorKind::MissingRequiredArgument);
|
2015-09-06 20:08:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn positional_required_2() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::new("positional_required")
|
2020-05-14 20:50:56 +00:00
|
|
|
.arg(Arg::new("flag").index(1).required(true))
|
2021-12-27 18:56:12 +00:00
|
|
|
.try_get_matches_from(vec!["", "someval"])
|
|
|
|
.unwrap();
|
2015-09-06 20:08:13 +00:00
|
|
|
assert!(m.is_present("flag"));
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("flag").map(|v| v.as_str()).unwrap(),
|
|
|
|
"someval"
|
|
|
|
);
|
2015-09-06 20:08:13 +00:00
|
|
|
}
|
|
|
|
|
2022-02-02 01:04:53 +00:00
|
|
|
#[test]
|
|
|
|
fn positional_required_with_requires() {
|
2022-02-14 21:47:20 +00:00
|
|
|
let cmd = Command::new("positional_required")
|
2022-02-02 01:04:53 +00:00
|
|
|
.arg(Arg::new("flag").required(true).requires("opt"))
|
|
|
|
.arg(Arg::new("opt"))
|
|
|
|
.arg(Arg::new("bar"));
|
|
|
|
|
2022-04-29 20:32:25 +00:00
|
|
|
utils::assert_output(cmd, "clap-test", POSITIONAL_REQ, true);
|
2022-02-02 01:04:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static POSITIONAL_REQ: &str = "error: The following required arguments were not provided:
|
|
|
|
<flag>
|
|
|
|
<opt>
|
|
|
|
|
|
|
|
USAGE:
|
|
|
|
clap-test <flag> <opt> [ARGS]
|
|
|
|
|
|
|
|
For more information try --help
|
|
|
|
";
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn positional_required_with_requires_if_no_value() {
|
2022-02-14 21:47:20 +00:00
|
|
|
let cmd = Command::new("positional_required")
|
2022-02-02 01:04:53 +00:00
|
|
|
.arg(Arg::new("flag").required(true).requires_if("val", "opt"))
|
|
|
|
.arg(Arg::new("opt"))
|
|
|
|
.arg(Arg::new("bar"));
|
|
|
|
|
2022-04-29 20:32:25 +00:00
|
|
|
utils::assert_output(cmd, "clap-test", POSITIONAL_REQ_IF_NO_VAL, true);
|
2022-02-02 01:04:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static POSITIONAL_REQ_IF_NO_VAL: &str = "error: The following required arguments were not provided:
|
|
|
|
<flag>
|
|
|
|
|
|
|
|
USAGE:
|
|
|
|
clap-test <flag> [ARGS]
|
|
|
|
|
|
|
|
For more information try --help
|
|
|
|
";
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn positional_required_with_requires_if_value() {
|
2022-02-14 21:47:20 +00:00
|
|
|
let cmd = Command::new("positional_required")
|
2022-02-02 01:04:53 +00:00
|
|
|
.arg(Arg::new("flag").required(true).requires_if("val", "opt"))
|
|
|
|
.arg(Arg::new("foo").required(true))
|
|
|
|
.arg(Arg::new("opt"))
|
|
|
|
.arg(Arg::new("bar"));
|
|
|
|
|
2022-04-29 20:32:25 +00:00
|
|
|
utils::assert_output(cmd, "clap-test val", POSITIONAL_REQ_IF_VAL, true);
|
2022-02-02 01:04:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static POSITIONAL_REQ_IF_VAL: &str = "error: The following required arguments were not provided:
|
|
|
|
<foo>
|
|
|
|
<opt>
|
|
|
|
|
|
|
|
USAGE:
|
2022-05-05 22:02:46 +00:00
|
|
|
clap-test <flag> <foo> <opt>
|
2022-02-02 01:04:53 +00:00
|
|
|
|
|
|
|
For more information try --help
|
|
|
|
";
|
|
|
|
|
2015-09-06 20:08:13 +00:00
|
|
|
#[test]
|
|
|
|
fn group_required() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let result = Command::new("group_required")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(-f --flag "some flag"))
|
2020-05-14 20:50:56 +00:00
|
|
|
.group(ArgGroup::new("gr").required(true).arg("some").arg("other"))
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(--some "some arg"))
|
|
|
|
.arg(arg!(--other "other arg"))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "-f"]);
|
2015-09-06 20:08:13 +00:00
|
|
|
assert!(result.is_err());
|
|
|
|
let err = result.err().unwrap();
|
2022-01-25 22:19:28 +00:00
|
|
|
assert_eq!(err.kind(), ErrorKind::MissingRequiredArgument);
|
2015-09-06 20:08:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn group_required_2() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::new("group_required")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(-f --flag "some flag"))
|
2020-05-14 20:50:56 +00:00
|
|
|
.group(ArgGroup::new("gr").required(true).arg("some").arg("other"))
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(--some "some arg"))
|
|
|
|
.arg(arg!(--other "other arg"))
|
2021-12-27 18:56:12 +00:00
|
|
|
.try_get_matches_from(vec!["", "-f", "--some"])
|
|
|
|
.unwrap();
|
2015-09-06 20:08:13 +00:00
|
|
|
assert!(m.is_present("some"));
|
|
|
|
assert!(!m.is_present("other"));
|
|
|
|
assert!(m.is_present("flag"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn group_required_3() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::new("group_required")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(-f --flag "some flag"))
|
2020-05-14 20:50:56 +00:00
|
|
|
.group(ArgGroup::new("gr").required(true).arg("some").arg("other"))
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(--some "some arg"))
|
|
|
|
.arg(arg!(--other "other arg"))
|
2021-12-27 18:56:12 +00:00
|
|
|
.try_get_matches_from(vec!["", "-f", "--other"])
|
|
|
|
.unwrap();
|
2015-09-06 20:08:13 +00:00
|
|
|
assert!(!m.is_present("some"));
|
|
|
|
assert!(m.is_present("other"));
|
|
|
|
assert!(m.is_present("flag"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn arg_require_group() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let result = Command::new("arg_require_group")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(-f --flag "some flag").requires("gr"))
|
2020-05-14 20:50:56 +00:00
|
|
|
.group(ArgGroup::new("gr").arg("some").arg("other"))
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(--some "some arg"))
|
|
|
|
.arg(arg!(--other "other arg"))
|
2018-07-25 18:46:28 +00:00
|
|
|
.try_get_matches_from(vec!["", "-f"]);
|
2015-09-06 20:08:13 +00:00
|
|
|
assert!(result.is_err());
|
|
|
|
let err = result.err().unwrap();
|
2022-01-25 22:19:28 +00:00
|
|
|
assert_eq!(err.kind(), ErrorKind::MissingRequiredArgument);
|
2015-09-06 20:08:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn arg_require_group_2() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let res = Command::new("arg_require_group")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(-f --flag "some flag").requires("gr"))
|
2020-05-14 20:50:56 +00:00
|
|
|
.group(ArgGroup::new("gr").arg("some").arg("other"))
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(--some "some arg"))
|
|
|
|
.arg(arg!(--other "other arg"))
|
2018-07-25 18:46:28 +00:00
|
|
|
.try_get_matches_from(vec!["", "-f", "--some"]);
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(res.is_ok(), "{}", res.unwrap_err());
|
2018-07-25 18:46:28 +00:00
|
|
|
let m = res.unwrap();
|
2015-09-06 20:08:13 +00:00
|
|
|
assert!(m.is_present("some"));
|
|
|
|
assert!(!m.is_present("other"));
|
|
|
|
assert!(m.is_present("flag"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn arg_require_group_3() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let res = Command::new("arg_require_group")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(-f --flag "some flag").requires("gr"))
|
2020-05-14 20:50:56 +00:00
|
|
|
.group(ArgGroup::new("gr").arg("some").arg("other"))
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(--some "some arg"))
|
|
|
|
.arg(arg!(--other "other arg"))
|
2018-07-25 18:46:28 +00:00
|
|
|
.try_get_matches_from(vec!["", "-f", "--other"]);
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(res.is_ok(), "{}", res.unwrap_err());
|
2018-07-25 18:46:28 +00:00
|
|
|
let m = res.unwrap();
|
2015-09-06 20:08:13 +00:00
|
|
|
assert!(!m.is_present("some"));
|
|
|
|
assert!(m.is_present("other"));
|
|
|
|
assert!(m.is_present("flag"));
|
2016-01-25 20:56:37 +00:00
|
|
|
}
|
2016-05-02 18:12:57 +00:00
|
|
|
|
|
|
|
// REQUIRED_UNLESS
|
|
|
|
|
2016-11-20 14:48:16 +00:00
|
|
|
#[test]
|
|
|
|
fn issue_753() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::new("test")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(
|
|
|
|
-l --list "List available interfaces (and stop there)"
|
2018-01-25 04:05:05 +00:00
|
|
|
))
|
|
|
|
.arg(
|
2021-11-19 20:33:11 +00:00
|
|
|
arg!(
|
|
|
|
-i --iface <INTERFACE> "Ethernet interface for fetching NTP packets"
|
2021-11-18 20:17:31 +00:00
|
|
|
)
|
2021-11-19 20:33:11 +00:00
|
|
|
.required(false)
|
2021-11-18 20:17:31 +00:00
|
|
|
.required_unless_present("list"),
|
2018-01-25 04:05:05 +00:00
|
|
|
)
|
|
|
|
.arg(
|
2021-11-19 20:33:11 +00:00
|
|
|
arg!(-f --file <TESTFILE> "Fetch NTP packets from pcap file")
|
|
|
|
.required(false)
|
2018-01-25 04:05:05 +00:00
|
|
|
.conflicts_with("iface")
|
2020-05-31 01:50:53 +00:00
|
|
|
.required_unless_present("list"),
|
|
|
|
)
|
|
|
|
.arg(
|
2021-11-19 20:33:11 +00:00
|
|
|
arg!(-s --server <SERVER_IP> "NTP server IP address")
|
|
|
|
.required(false)
|
2020-05-31 01:50:53 +00:00
|
|
|
.required_unless_present("list"),
|
2018-01-25 04:05:05 +00:00
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["test", "--list"]);
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(m.is_ok(), "{}", m.unwrap_err());
|
2016-11-20 14:48:16 +00:00
|
|
|
}
|
|
|
|
|
2016-05-02 18:12:57 +00:00
|
|
|
#[test]
|
2020-05-31 01:50:53 +00:00
|
|
|
fn required_unless_present() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let res = Command::new("unlesstest")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("cfg")
|
2020-05-31 01:50:53 +00:00
|
|
|
.required_unless_present("dbg")
|
2018-01-25 04:05:05 +00:00
|
|
|
.takes_value(true)
|
|
|
|
.long("config"),
|
|
|
|
)
|
2020-05-14 20:50:56 +00:00
|
|
|
.arg(Arg::new("dbg").long("debug"))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["unlesstest", "--debug"]);
|
2016-05-02 18:12:57 +00:00
|
|
|
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(res.is_ok(), "{}", res.unwrap_err());
|
2016-05-02 18:12:57 +00:00
|
|
|
let m = res.unwrap();
|
|
|
|
assert!(m.is_present("dbg"));
|
|
|
|
assert!(!m.is_present("cfg"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2022-02-08 03:45:30 +00:00
|
|
|
fn required_unless_present_err() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let res = Command::new("unlesstest")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("cfg")
|
2020-05-31 01:50:53 +00:00
|
|
|
.required_unless_present("dbg")
|
2018-01-25 04:05:05 +00:00
|
|
|
.takes_value(true)
|
|
|
|
.long("config"),
|
|
|
|
)
|
2020-05-14 20:50:56 +00:00
|
|
|
.arg(Arg::new("dbg").long("debug"))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["unlesstest"]);
|
2016-05-02 18:12:57 +00:00
|
|
|
|
|
|
|
assert!(res.is_err());
|
2022-01-25 22:19:28 +00:00
|
|
|
assert_eq!(res.unwrap_err().kind(), ErrorKind::MissingRequiredArgument);
|
2016-05-02 18:12:57 +00:00
|
|
|
}
|
|
|
|
|
2021-12-22 02:35:07 +00:00
|
|
|
#[test]
|
|
|
|
fn required_unless_present_with_optional_value() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let res = Command::new("unlesstest")
|
2021-12-22 02:35:07 +00:00
|
|
|
.arg(Arg::new("opt").long("opt").min_values(0).max_values(1))
|
|
|
|
.arg(
|
|
|
|
Arg::new("cfg")
|
|
|
|
.required_unless_present("dbg")
|
|
|
|
.takes_value(true)
|
|
|
|
.long("config"),
|
|
|
|
)
|
|
|
|
.arg(Arg::new("dbg").long("debug"))
|
|
|
|
.try_get_matches_from(vec!["unlesstest", "--opt"]);
|
|
|
|
|
|
|
|
assert!(res.is_err());
|
2022-01-25 22:19:28 +00:00
|
|
|
assert_eq!(res.unwrap_err().kind(), ErrorKind::MissingRequiredArgument);
|
2021-12-22 02:35:07 +00:00
|
|
|
}
|
|
|
|
|
2016-05-02 18:12:57 +00:00
|
|
|
// REQUIRED_UNLESS_ALL
|
|
|
|
|
|
|
|
#[test]
|
2020-08-29 09:48:35 +00:00
|
|
|
fn required_unless_present_all() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let res = Command::new("unlessall")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("cfg")
|
2020-08-29 09:48:35 +00:00
|
|
|
.required_unless_present_all(&["dbg", "infile"])
|
2018-01-25 04:05:05 +00:00
|
|
|
.takes_value(true)
|
|
|
|
.long("config"),
|
|
|
|
)
|
2020-05-14 20:50:56 +00:00
|
|
|
.arg(Arg::new("dbg").long("debug"))
|
|
|
|
.arg(Arg::new("infile").short('i').takes_value(true))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["unlessall", "--debug", "-i", "file"]);
|
2016-05-02 18:12:57 +00:00
|
|
|
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(res.is_ok(), "{}", res.unwrap_err());
|
2016-05-02 18:12:57 +00:00
|
|
|
let m = res.unwrap();
|
|
|
|
assert!(m.is_present("dbg"));
|
|
|
|
assert!(m.is_present("infile"));
|
|
|
|
assert!(!m.is_present("cfg"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn required_unless_all_err() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let res = Command::new("unlessall")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("cfg")
|
2020-08-29 09:48:35 +00:00
|
|
|
.required_unless_present_all(&["dbg", "infile"])
|
2018-01-25 04:05:05 +00:00
|
|
|
.takes_value(true)
|
|
|
|
.long("config"),
|
|
|
|
)
|
2020-05-14 20:50:56 +00:00
|
|
|
.arg(Arg::new("dbg").long("debug"))
|
|
|
|
.arg(Arg::new("infile").short('i').takes_value(true))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["unlessall", "--debug"]);
|
2016-05-02 18:12:57 +00:00
|
|
|
|
|
|
|
assert!(res.is_err());
|
2022-01-25 22:19:28 +00:00
|
|
|
assert_eq!(res.unwrap_err().kind(), ErrorKind::MissingRequiredArgument);
|
2016-05-02 18:12:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// REQUIRED_UNLESS_ONE
|
|
|
|
|
|
|
|
#[test]
|
2020-08-29 09:48:35 +00:00
|
|
|
fn required_unless_present_any() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let res = Command::new("unlessone")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("cfg")
|
2020-08-29 09:48:35 +00:00
|
|
|
.required_unless_present_any(&["dbg", "infile"])
|
2018-01-25 04:05:05 +00:00
|
|
|
.takes_value(true)
|
|
|
|
.long("config"),
|
|
|
|
)
|
2020-05-14 20:50:56 +00:00
|
|
|
.arg(Arg::new("dbg").long("debug"))
|
|
|
|
.arg(Arg::new("infile").short('i').takes_value(true))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["unlessone", "--debug"]);
|
2016-05-02 18:12:57 +00:00
|
|
|
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(res.is_ok(), "{}", res.unwrap_err());
|
2016-05-02 18:12:57 +00:00
|
|
|
let m = res.unwrap();
|
|
|
|
assert!(m.is_present("dbg"));
|
|
|
|
assert!(!m.is_present("cfg"));
|
|
|
|
}
|
|
|
|
|
2016-07-23 17:43:22 +00:00
|
|
|
#[test]
|
2020-05-31 01:39:24 +00:00
|
|
|
fn required_unless_any_2() {
|
2020-08-29 09:48:35 +00:00
|
|
|
// This tests that the required_unless_present_any works when the second arg in the array is used
|
2016-07-23 17:43:22 +00:00
|
|
|
// instead of the first.
|
2022-02-12 03:48:29 +00:00
|
|
|
let res = Command::new("unlessone")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("cfg")
|
2020-08-29 09:48:35 +00:00
|
|
|
.required_unless_present_any(&["dbg", "infile"])
|
2018-01-25 04:05:05 +00:00
|
|
|
.takes_value(true)
|
|
|
|
.long("config"),
|
|
|
|
)
|
2020-05-14 20:50:56 +00:00
|
|
|
.arg(Arg::new("dbg").long("debug"))
|
|
|
|
.arg(Arg::new("infile").short('i').takes_value(true))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["unlessone", "-i", "file"]);
|
2016-07-23 17:43:22 +00:00
|
|
|
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(res.is_ok(), "{}", res.unwrap_err());
|
2016-07-23 17:43:22 +00:00
|
|
|
let m = res.unwrap();
|
|
|
|
assert!(m.is_present("infile"));
|
|
|
|
assert!(!m.is_present("cfg"));
|
|
|
|
}
|
|
|
|
|
2017-12-26 16:16:18 +00:00
|
|
|
#[test]
|
2020-05-31 01:39:24 +00:00
|
|
|
fn required_unless_any_works_with_short() {
|
2021-12-07 14:14:41 +00:00
|
|
|
// GitHub issue: https://github.com/clap-rs/clap/issues/1135
|
2022-02-12 03:48:29 +00:00
|
|
|
let res = Command::new("unlessone")
|
2020-05-14 20:50:56 +00:00
|
|
|
.arg(Arg::new("a").conflicts_with("b").short('a'))
|
|
|
|
.arg(Arg::new("b").short('b'))
|
2020-08-29 09:48:35 +00:00
|
|
|
.arg(
|
|
|
|
Arg::new("x")
|
|
|
|
.short('x')
|
|
|
|
.required_unless_present_any(&["a", "b"]),
|
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["unlessone", "-a"]);
|
2017-12-26 16:16:18 +00:00
|
|
|
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(res.is_ok(), "{}", res.unwrap_err());
|
2017-12-26 16:16:18 +00:00
|
|
|
}
|
|
|
|
|
2018-01-19 01:41:23 +00:00
|
|
|
#[test]
|
2020-05-31 01:39:24 +00:00
|
|
|
fn required_unless_any_works_with_short_err() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let res = Command::new("unlessone")
|
2020-05-14 20:50:56 +00:00
|
|
|
.arg(Arg::new("a").conflicts_with("b").short('a'))
|
|
|
|
.arg(Arg::new("b").short('b'))
|
2020-08-29 09:48:35 +00:00
|
|
|
.arg(
|
|
|
|
Arg::new("x")
|
|
|
|
.short('x')
|
|
|
|
.required_unless_present_any(&["a", "b"]),
|
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["unlessone"]);
|
2018-01-19 01:41:23 +00:00
|
|
|
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(res.is_err());
|
2018-01-19 01:41:23 +00:00
|
|
|
}
|
|
|
|
|
2018-01-19 01:30:12 +00:00
|
|
|
#[test]
|
2020-05-31 01:39:24 +00:00
|
|
|
fn required_unless_any_works_without() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let res = Command::new("unlessone")
|
2020-05-14 20:50:56 +00:00
|
|
|
.arg(Arg::new("a").conflicts_with("b").short('a'))
|
|
|
|
.arg(Arg::new("b").short('b'))
|
2020-08-29 09:48:35 +00:00
|
|
|
.arg(Arg::new("x").required_unless_present_any(&["a", "b"]))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["unlessone", "-a"]);
|
2018-01-19 01:30:12 +00:00
|
|
|
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(res.is_ok(), "{}", res.unwrap_err());
|
2018-01-19 01:30:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-05-31 01:39:24 +00:00
|
|
|
fn required_unless_any_works_with_long() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let res = Command::new("unlessone")
|
2020-05-14 20:50:56 +00:00
|
|
|
.arg(Arg::new("a").conflicts_with("b").short('a'))
|
|
|
|
.arg(Arg::new("b").short('b'))
|
2018-01-19 01:30:12 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("x")
|
2018-01-19 01:30:12 +00:00
|
|
|
.long("x_is_the_option")
|
2020-08-29 09:48:35 +00:00
|
|
|
.required_unless_present_any(&["a", "b"]),
|
2018-01-25 04:05:05 +00:00
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["unlessone", "-a"]);
|
2018-01-19 01:30:12 +00:00
|
|
|
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(res.is_ok(), "{}", res.unwrap_err());
|
2018-01-19 01:30:12 +00:00
|
|
|
}
|
|
|
|
|
2016-07-23 21:22:14 +00:00
|
|
|
#[test]
|
2020-05-31 01:39:24 +00:00
|
|
|
fn required_unless_any_1() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let res = Command::new("unlessone")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("cfg")
|
2020-08-29 09:48:35 +00:00
|
|
|
.required_unless_present_any(&["dbg", "infile"])
|
2018-01-25 04:05:05 +00:00
|
|
|
.takes_value(true)
|
|
|
|
.long("config"),
|
|
|
|
)
|
2020-05-14 20:50:56 +00:00
|
|
|
.arg(Arg::new("dbg").long("debug"))
|
|
|
|
.arg(Arg::new("infile").short('i').takes_value(true))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["unlessone", "--debug"]);
|
2016-07-23 21:22:14 +00:00
|
|
|
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(res.is_ok(), "{}", res.unwrap_err());
|
2016-07-23 21:22:14 +00:00
|
|
|
let m = res.unwrap();
|
|
|
|
assert!(!m.is_present("infile"));
|
|
|
|
assert!(!m.is_present("cfg"));
|
|
|
|
assert!(m.is_present("dbg"));
|
|
|
|
}
|
|
|
|
|
2016-05-02 18:12:57 +00:00
|
|
|
#[test]
|
2020-05-31 01:39:24 +00:00
|
|
|
fn required_unless_any_err() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let res = Command::new("unlessone")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("cfg")
|
2020-08-29 09:48:35 +00:00
|
|
|
.required_unless_present_any(&["dbg", "infile"])
|
2018-01-25 04:05:05 +00:00
|
|
|
.takes_value(true)
|
|
|
|
.long("config"),
|
|
|
|
)
|
2020-05-14 20:50:56 +00:00
|
|
|
.arg(Arg::new("dbg").long("debug"))
|
|
|
|
.arg(Arg::new("infile").short('i').takes_value(true))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["unlessone"]);
|
2016-05-02 18:12:57 +00:00
|
|
|
|
|
|
|
assert!(res.is_err());
|
2022-01-25 22:19:28 +00:00
|
|
|
assert_eq!(res.unwrap_err().kind(), ErrorKind::MissingRequiredArgument);
|
2016-05-02 18:12:57 +00:00
|
|
|
}
|
2016-05-09 03:20:50 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn missing_required_output() {
|
2022-04-29 20:32:25 +00:00
|
|
|
utils::assert_output(utils::complex_app(), "clap-test -F", MISSING_REQ, true);
|
2016-05-09 03:20:50 +00:00
|
|
|
}
|
2016-12-14 16:41:21 +00:00
|
|
|
|
|
|
|
// Conditional external requirements
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn requires_if_present_val() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let res = Command::new("unlessone")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("cfg")
|
2018-01-25 04:05:05 +00:00
|
|
|
.requires_if("my.cfg", "extra")
|
|
|
|
.takes_value(true)
|
|
|
|
.long("config"),
|
|
|
|
)
|
2020-05-14 20:50:56 +00:00
|
|
|
.arg(Arg::new("extra").long("extra"))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["unlessone", "--config=my.cfg"]);
|
2016-12-14 16:41:21 +00:00
|
|
|
|
|
|
|
assert!(res.is_err());
|
2022-01-25 22:19:28 +00:00
|
|
|
assert_eq!(res.unwrap_err().kind(), ErrorKind::MissingRequiredArgument);
|
2016-12-14 16:41:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn requires_if_present_mult() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let res = Command::new("unlessone")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("cfg")
|
2018-01-25 04:05:05 +00:00
|
|
|
.requires_ifs(&[("my.cfg", "extra"), ("other.cfg", "other")])
|
|
|
|
.takes_value(true)
|
|
|
|
.long("config"),
|
|
|
|
)
|
2020-05-14 20:50:56 +00:00
|
|
|
.arg(Arg::new("extra").long("extra"))
|
|
|
|
.arg(Arg::new("other").long("other"))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["unlessone", "--config=other.cfg"]);
|
2016-12-14 16:41:21 +00:00
|
|
|
|
|
|
|
assert!(res.is_err());
|
2022-01-25 22:19:28 +00:00
|
|
|
assert_eq!(res.unwrap_err().kind(), ErrorKind::MissingRequiredArgument);
|
2016-12-14 16:41:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn requires_if_present_mult_pass() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let res = Command::new("unlessone")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("cfg")
|
2018-01-25 04:05:05 +00:00
|
|
|
.requires_ifs(&[("my.cfg", "extra"), ("other.cfg", "other")])
|
|
|
|
.takes_value(true)
|
|
|
|
.long("config"),
|
|
|
|
)
|
2020-05-14 20:50:56 +00:00
|
|
|
.arg(Arg::new("extra").long("extra"))
|
|
|
|
.arg(Arg::new("other").long("other"))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["unlessone", "--config=some.cfg"]);
|
2016-12-14 16:41:21 +00:00
|
|
|
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(res.is_ok(), "{}", res.unwrap_err());
|
2016-12-14 16:41:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn requires_if_present_val_no_present_pass() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let res = Command::new("unlessone")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("cfg")
|
2018-01-25 04:05:05 +00:00
|
|
|
.requires_if("my.cfg", "extra")
|
|
|
|
.takes_value(true)
|
|
|
|
.long("config"),
|
|
|
|
)
|
2020-05-14 20:50:56 +00:00
|
|
|
.arg(Arg::new("extra").long("extra"))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["unlessone"]);
|
2016-12-14 16:41:21 +00:00
|
|
|
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(res.is_ok(), "{}", res.unwrap_err());
|
2016-12-29 01:35:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Conditionally required
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn required_if_val_present_pass() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let res = Command::new("ri")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("cfg")
|
2020-05-23 14:02:03 +00:00
|
|
|
.required_if_eq("extra", "val")
|
2018-01-25 04:05:05 +00:00
|
|
|
.takes_value(true)
|
|
|
|
.long("config"),
|
|
|
|
)
|
2020-05-14 20:50:56 +00:00
|
|
|
.arg(Arg::new("extra").takes_value(true).long("extra"))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["ri", "--extra", "val", "--config", "my.cfg"]);
|
2016-12-29 01:35:34 +00:00
|
|
|
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(res.is_ok(), "{}", res.unwrap_err());
|
2016-12-29 01:35:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn required_if_val_present_fail() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let res = Command::new("ri")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("cfg")
|
2020-05-23 14:02:03 +00:00
|
|
|
.required_if_eq("extra", "val")
|
2018-01-25 04:05:05 +00:00
|
|
|
.takes_value(true)
|
|
|
|
.long("config"),
|
|
|
|
)
|
2020-05-14 20:50:56 +00:00
|
|
|
.arg(Arg::new("extra").takes_value(true).long("extra"))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["ri", "--extra", "val"]);
|
2016-12-29 01:35:34 +00:00
|
|
|
|
|
|
|
assert!(res.is_err());
|
2022-01-25 22:19:28 +00:00
|
|
|
assert_eq!(res.unwrap_err().kind(), ErrorKind::MissingRequiredArgument);
|
2016-12-29 01:35:34 +00:00
|
|
|
}
|
|
|
|
|
2021-04-25 18:19:16 +00:00
|
|
|
#[test]
|
2021-11-29 16:34:42 +00:00
|
|
|
fn required_if_val_present_ignore_case_pass() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let res = Command::new("ri")
|
2021-04-25 18:19:16 +00:00
|
|
|
.arg(
|
|
|
|
Arg::new("cfg")
|
|
|
|
.required_if_eq("extra", "Val")
|
|
|
|
.takes_value(true)
|
|
|
|
.long("config"),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::new("extra")
|
|
|
|
.takes_value(true)
|
|
|
|
.long("extra")
|
2021-11-29 16:34:42 +00:00
|
|
|
.ignore_case(true),
|
2021-04-25 18:19:16 +00:00
|
|
|
)
|
|
|
|
.try_get_matches_from(vec!["ri", "--extra", "vaL", "--config", "my.cfg"]);
|
|
|
|
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(res.is_ok(), "{}", res.unwrap_err());
|
2021-04-25 18:19:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-11-29 16:34:42 +00:00
|
|
|
fn required_if_val_present_ignore_case_fail() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let res = Command::new("ri")
|
2021-04-25 18:19:16 +00:00
|
|
|
.arg(
|
|
|
|
Arg::new("cfg")
|
|
|
|
.required_if_eq("extra", "Val")
|
|
|
|
.takes_value(true)
|
|
|
|
.long("config"),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::new("extra")
|
|
|
|
.takes_value(true)
|
|
|
|
.long("extra")
|
2021-11-29 16:34:42 +00:00
|
|
|
.ignore_case(true),
|
2021-04-25 18:19:16 +00:00
|
|
|
)
|
|
|
|
.try_get_matches_from(vec!["ri", "--extra", "vaL"]);
|
|
|
|
|
|
|
|
assert!(res.is_err());
|
2022-01-25 22:19:28 +00:00
|
|
|
assert_eq!(res.unwrap_err().kind(), ErrorKind::MissingRequiredArgument);
|
2021-04-25 18:19:16 +00:00
|
|
|
}
|
|
|
|
|
2021-02-27 18:10:02 +00:00
|
|
|
#[test]
|
|
|
|
fn required_if_all_values_present_pass() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let res = Command::new("ri")
|
2021-02-27 18:10:02 +00:00
|
|
|
.arg(
|
|
|
|
Arg::new("cfg")
|
|
|
|
.required_if_eq_all(&[("extra", "val"), ("option", "spec")])
|
|
|
|
.takes_value(true)
|
|
|
|
.long("config"),
|
|
|
|
)
|
|
|
|
.arg(Arg::new("extra").takes_value(true).long("extra"))
|
|
|
|
.arg(Arg::new("option").takes_value(true).long("option"))
|
|
|
|
.try_get_matches_from(vec![
|
|
|
|
"ri", "--extra", "val", "--option", "spec", "--config", "my.cfg",
|
|
|
|
]);
|
|
|
|
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(res.is_ok(), "{}", res.unwrap_err());
|
2021-02-27 18:10:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn required_if_some_values_present_pass() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let res = Command::new("ri")
|
2021-02-27 18:10:02 +00:00
|
|
|
.arg(
|
|
|
|
Arg::new("cfg")
|
|
|
|
.required_if_eq_all(&[("extra", "val"), ("option", "spec")])
|
|
|
|
.takes_value(true)
|
|
|
|
.long("config"),
|
|
|
|
)
|
|
|
|
.arg(Arg::new("extra").takes_value(true).long("extra"))
|
|
|
|
.arg(Arg::new("option").takes_value(true).long("option"))
|
|
|
|
.try_get_matches_from(vec!["ri", "--extra", "val"]);
|
|
|
|
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(res.is_ok(), "{}", res.unwrap_err());
|
2021-02-27 18:10:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn required_if_all_values_present_fail() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let res = Command::new("ri")
|
2021-02-27 18:10:02 +00:00
|
|
|
.arg(
|
|
|
|
Arg::new("cfg")
|
|
|
|
.required_if_eq_all(&[("extra", "val"), ("option", "spec")])
|
|
|
|
.takes_value(true)
|
|
|
|
.long("config"),
|
|
|
|
)
|
|
|
|
.arg(Arg::new("extra").takes_value(true).long("extra"))
|
|
|
|
.arg(Arg::new("option").takes_value(true).long("option"))
|
|
|
|
.try_get_matches_from(vec!["ri", "--extra", "val", "--option", "spec"]);
|
|
|
|
|
|
|
|
assert!(res.is_err());
|
2022-01-25 22:19:28 +00:00
|
|
|
assert_eq!(res.unwrap_err().kind(), ErrorKind::MissingRequiredArgument);
|
2021-02-27 18:10:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn required_if_any_all_values_present_pass() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let res = Command::new("ri")
|
2021-02-27 18:10:02 +00:00
|
|
|
.arg(
|
|
|
|
Arg::new("cfg")
|
|
|
|
.required_if_eq_all(&[("extra", "val"), ("option", "spec")])
|
|
|
|
.required_if_eq_any(&[("extra", "val2"), ("option", "spec2")])
|
|
|
|
.takes_value(true)
|
|
|
|
.long("config"),
|
|
|
|
)
|
|
|
|
.arg(Arg::new("extra").takes_value(true).long("extra"))
|
|
|
|
.arg(Arg::new("option").takes_value(true).long("option"))
|
|
|
|
.try_get_matches_from(vec![
|
|
|
|
"ri", "--extra", "val", "--option", "spec", "--config", "my.cfg",
|
|
|
|
]);
|
|
|
|
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(res.is_ok(), "{}", res.unwrap_err());
|
2021-02-27 18:10:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn required_if_any_all_values_present_fail() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let res = Command::new("ri")
|
2021-02-27 18:10:02 +00:00
|
|
|
.arg(
|
|
|
|
Arg::new("cfg")
|
|
|
|
.required_if_eq_all(&[("extra", "val"), ("option", "spec")])
|
|
|
|
.required_if_eq_any(&[("extra", "val2"), ("option", "spec2")])
|
|
|
|
.takes_value(true)
|
|
|
|
.long("config"),
|
|
|
|
)
|
|
|
|
.arg(Arg::new("extra").takes_value(true).long("extra"))
|
|
|
|
.arg(Arg::new("option").takes_value(true).long("option"))
|
|
|
|
.try_get_matches_from(vec!["ri", "--extra", "val", "--option", "spec"]);
|
|
|
|
|
|
|
|
assert!(res.is_err());
|
2022-01-25 22:19:28 +00:00
|
|
|
assert_eq!(res.unwrap_err().kind(), ErrorKind::MissingRequiredArgument);
|
2021-02-27 18:10:02 +00:00
|
|
|
}
|
|
|
|
|
2020-05-15 03:14:47 +00:00
|
|
|
#[test]
|
|
|
|
fn list_correct_required_args() {
|
2022-02-14 21:47:20 +00:00
|
|
|
let cmd = Command::new("Test cmd")
|
2020-05-15 03:14:47 +00:00
|
|
|
.version("1.0")
|
|
|
|
.author("F0x06")
|
|
|
|
.about("Arg test")
|
|
|
|
.arg(
|
2020-05-15 15:25:01 +00:00
|
|
|
Arg::new("target")
|
2020-05-15 03:14:47 +00:00
|
|
|
.takes_value(true)
|
|
|
|
.required(true)
|
2022-05-24 01:16:02 +00:00
|
|
|
.value_parser(["file", "stdout"])
|
2020-05-15 03:14:47 +00:00
|
|
|
.long("target"),
|
|
|
|
)
|
|
|
|
.arg(
|
2020-05-15 15:25:01 +00:00
|
|
|
Arg::new("input")
|
2020-05-15 03:14:47 +00:00
|
|
|
.takes_value(true)
|
|
|
|
.required(true)
|
|
|
|
.long("input"),
|
|
|
|
)
|
|
|
|
.arg(
|
2020-05-15 15:25:01 +00:00
|
|
|
Arg::new("output")
|
2020-05-15 03:14:47 +00:00
|
|
|
.takes_value(true)
|
|
|
|
.required(true)
|
|
|
|
.long("output"),
|
|
|
|
);
|
|
|
|
|
2022-04-29 20:32:25 +00:00
|
|
|
utils::assert_output(
|
2022-02-14 21:47:20 +00:00
|
|
|
cmd,
|
2020-05-15 03:14:47 +00:00
|
|
|
"test --input somepath --target file",
|
|
|
|
COND_REQ_IN_USAGE,
|
2022-04-29 20:32:25 +00:00
|
|
|
true,
|
|
|
|
);
|
2020-05-15 03:14:47 +00:00
|
|
|
}
|
|
|
|
|
2017-02-03 22:43:49 +00:00
|
|
|
#[test]
|
|
|
|
fn required_if_val_present_fail_error_output() {
|
2022-02-14 21:47:20 +00:00
|
|
|
let cmd = Command::new("Test cmd")
|
2017-02-03 22:43:49 +00:00
|
|
|
.version("1.0")
|
|
|
|
.author("F0x06")
|
|
|
|
.about("Arg test")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("target")
|
2018-01-25 04:05:05 +00:00
|
|
|
.takes_value(true)
|
|
|
|
.required(true)
|
2022-05-24 01:16:02 +00:00
|
|
|
.value_parser(["file", "stdout"])
|
2018-01-25 04:05:05 +00:00
|
|
|
.long("target"),
|
|
|
|
)
|
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("input")
|
2018-01-25 04:05:05 +00:00
|
|
|
.takes_value(true)
|
|
|
|
.required(true)
|
|
|
|
.long("input"),
|
|
|
|
)
|
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("output")
|
2018-01-25 04:05:05 +00:00
|
|
|
.takes_value(true)
|
2020-05-23 14:02:03 +00:00
|
|
|
.required_if_eq("target", "file")
|
2018-01-25 04:05:05 +00:00
|
|
|
.long("output"),
|
|
|
|
);
|
2017-02-03 22:43:49 +00:00
|
|
|
|
2022-04-29 20:32:25 +00:00
|
|
|
utils::assert_output(
|
2022-02-14 21:47:20 +00:00
|
|
|
cmd,
|
2018-01-25 04:05:05 +00:00
|
|
|
"test --input somepath --target file",
|
|
|
|
COND_REQ_IN_USAGE,
|
2022-04-29 20:32:25 +00:00
|
|
|
true,
|
|
|
|
);
|
2017-02-03 22:43:49 +00:00
|
|
|
}
|
|
|
|
|
2016-12-29 01:35:34 +00:00
|
|
|
#[test]
|
|
|
|
fn required_if_wrong_val() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let res = Command::new("ri")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("cfg")
|
2020-05-23 14:02:03 +00:00
|
|
|
.required_if_eq("extra", "val")
|
2018-01-25 04:05:05 +00:00
|
|
|
.takes_value(true)
|
|
|
|
.long("config"),
|
|
|
|
)
|
2020-05-14 20:50:56 +00:00
|
|
|
.arg(Arg::new("extra").takes_value(true).long("extra"))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["ri", "--extra", "other"]);
|
2016-12-29 01:35:34 +00:00
|
|
|
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(res.is_ok(), "{}", res.unwrap_err());
|
2016-12-29 01:35:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn required_ifs_val_present_pass() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let res = Command::new("ri")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("cfg")
|
2020-05-23 13:02:19 +00:00
|
|
|
.required_if_eq_any(&[("extra", "val"), ("option", "spec")])
|
2018-01-25 04:05:05 +00:00
|
|
|
.takes_value(true)
|
|
|
|
.long("config"),
|
|
|
|
)
|
2020-05-14 20:50:56 +00:00
|
|
|
.arg(Arg::new("option").takes_value(true).long("option"))
|
|
|
|
.arg(Arg::new("extra").takes_value(true).long("extra"))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["ri", "--option", "spec", "--config", "my.cfg"]);
|
2016-12-29 01:35:34 +00:00
|
|
|
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(res.is_ok(), "{}", res.unwrap_err());
|
2016-12-29 01:35:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn required_ifs_val_present_fail() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let res = Command::new("ri")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("cfg")
|
2020-05-23 13:02:19 +00:00
|
|
|
.required_if_eq_any(&[("extra", "val"), ("option", "spec")])
|
2018-01-25 04:05:05 +00:00
|
|
|
.takes_value(true)
|
|
|
|
.long("config"),
|
|
|
|
)
|
2020-05-14 20:50:56 +00:00
|
|
|
.arg(Arg::new("extra").takes_value(true).long("extra"))
|
|
|
|
.arg(Arg::new("option").takes_value(true).long("option"))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["ri", "--option", "spec"]);
|
2016-12-29 01:35:34 +00:00
|
|
|
|
|
|
|
assert!(res.is_err());
|
2022-01-25 22:19:28 +00:00
|
|
|
assert_eq!(res.unwrap_err().kind(), ErrorKind::MissingRequiredArgument);
|
2016-12-29 01:35:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn required_ifs_wrong_val() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let res = Command::new("ri")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("cfg")
|
2020-05-23 13:02:19 +00:00
|
|
|
.required_if_eq_any(&[("extra", "val"), ("option", "spec")])
|
2018-01-25 04:05:05 +00:00
|
|
|
.takes_value(true)
|
|
|
|
.long("config"),
|
|
|
|
)
|
2020-05-14 20:50:56 +00:00
|
|
|
.arg(Arg::new("extra").takes_value(true).long("extra"))
|
|
|
|
.arg(Arg::new("option").takes_value(true).long("option"))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["ri", "--option", "other"]);
|
2016-12-29 01:35:34 +00:00
|
|
|
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(res.is_ok(), "{}", res.unwrap_err());
|
2016-12-29 01:35:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn required_ifs_wrong_val_mult_fail() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let res = Command::new("ri")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("cfg")
|
2020-05-23 13:02:19 +00:00
|
|
|
.required_if_eq_any(&[("extra", "val"), ("option", "spec")])
|
2018-01-25 04:05:05 +00:00
|
|
|
.takes_value(true)
|
|
|
|
.long("config"),
|
|
|
|
)
|
2020-05-14 20:50:56 +00:00
|
|
|
.arg(Arg::new("extra").takes_value(true).long("extra"))
|
|
|
|
.arg(Arg::new("option").takes_value(true).long("option"))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["ri", "--extra", "other", "--option", "spec"]);
|
2016-12-29 01:35:34 +00:00
|
|
|
|
|
|
|
assert!(res.is_err());
|
2022-01-25 22:19:28 +00:00
|
|
|
assert_eq!(res.unwrap_err().kind(), ErrorKind::MissingRequiredArgument);
|
2017-02-21 04:51:20 +00:00
|
|
|
}
|
2017-03-17 01:45:45 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn require_eq() {
|
2022-02-14 21:47:20 +00:00
|
|
|
let cmd = Command::new("clap-test").version("v1.4.8").arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("opt")
|
2017-03-17 01:45:45 +00:00
|
|
|
.long("opt")
|
2018-07-23 19:09:42 +00:00
|
|
|
.short('o')
|
2017-03-17 01:45:45 +00:00
|
|
|
.required(true)
|
|
|
|
.require_equals(true)
|
|
|
|
.value_name("FILE")
|
2021-11-18 16:17:15 +00:00
|
|
|
.help("some"),
|
2018-01-25 04:05:05 +00:00
|
|
|
);
|
2022-04-29 20:32:25 +00:00
|
|
|
utils::assert_output(cmd, "clap-test", REQUIRE_EQUALS, true);
|
2018-01-25 04:05:05 +00:00
|
|
|
}
|
2018-07-25 18:46:28 +00:00
|
|
|
|
2020-05-15 03:14:47 +00:00
|
|
|
#[test]
|
|
|
|
fn require_eq_filtered() {
|
2022-02-14 21:47:20 +00:00
|
|
|
let cmd = Command::new("clap-test")
|
2020-05-15 03:14:47 +00:00
|
|
|
.version("v1.4.8")
|
|
|
|
.arg(
|
2020-05-15 15:25:01 +00:00
|
|
|
Arg::new("opt")
|
2020-05-15 03:14:47 +00:00
|
|
|
.long("opt")
|
|
|
|
.short('o')
|
|
|
|
.required(true)
|
|
|
|
.require_equals(true)
|
|
|
|
.value_name("FILE")
|
2021-11-18 16:17:15 +00:00
|
|
|
.help("some"),
|
2020-05-15 03:14:47 +00:00
|
|
|
)
|
|
|
|
.arg(
|
2020-05-15 15:25:01 +00:00
|
|
|
Arg::new("foo")
|
2020-05-15 03:14:47 +00:00
|
|
|
.long("foo")
|
|
|
|
.short('f')
|
|
|
|
.required(true)
|
|
|
|
.require_equals(true)
|
|
|
|
.value_name("FILE")
|
2021-11-18 16:17:15 +00:00
|
|
|
.help("some other arg"),
|
2020-05-15 03:14:47 +00:00
|
|
|
);
|
2022-04-29 20:32:25 +00:00
|
|
|
utils::assert_output(cmd, "clap-test -f=blah", REQUIRE_EQUALS_FILTERED, true);
|
2020-05-15 03:14:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn require_eq_filtered_group() {
|
2022-02-14 21:47:20 +00:00
|
|
|
let cmd = Command::new("clap-test")
|
2020-05-15 03:14:47 +00:00
|
|
|
.version("v1.4.8")
|
|
|
|
.arg(
|
2020-05-15 15:25:01 +00:00
|
|
|
Arg::new("opt")
|
2020-05-15 03:14:47 +00:00
|
|
|
.long("opt")
|
|
|
|
.short('o')
|
|
|
|
.required(true)
|
|
|
|
.require_equals(true)
|
|
|
|
.value_name("FILE")
|
2021-11-18 16:17:15 +00:00
|
|
|
.help("some"),
|
2020-05-15 03:14:47 +00:00
|
|
|
)
|
|
|
|
.arg(
|
2020-05-15 15:25:01 +00:00
|
|
|
Arg::new("foo")
|
2020-05-15 03:14:47 +00:00
|
|
|
.long("foo")
|
|
|
|
.short('f')
|
|
|
|
.required(true)
|
|
|
|
.require_equals(true)
|
|
|
|
.value_name("FILE")
|
2021-11-18 16:17:15 +00:00
|
|
|
.help("some other arg"),
|
2020-05-15 03:14:47 +00:00
|
|
|
)
|
|
|
|
.arg(
|
2020-05-15 15:25:01 +00:00
|
|
|
Arg::new("g1")
|
2020-05-15 03:14:47 +00:00
|
|
|
.long("g1")
|
|
|
|
.require_equals(true)
|
|
|
|
.value_name("FILE"),
|
|
|
|
)
|
|
|
|
.arg(
|
2020-05-15 15:25:01 +00:00
|
|
|
Arg::new("g2")
|
2020-05-15 03:14:47 +00:00
|
|
|
.long("g2")
|
|
|
|
.require_equals(true)
|
|
|
|
.value_name("FILE"),
|
|
|
|
)
|
|
|
|
.group(
|
2020-05-15 15:25:01 +00:00
|
|
|
ArgGroup::new("test_group")
|
2020-05-15 03:14:47 +00:00
|
|
|
.args(&["g1", "g2"])
|
|
|
|
.required(true),
|
|
|
|
);
|
2022-04-29 20:32:25 +00:00
|
|
|
utils::assert_output(
|
2022-02-14 21:47:20 +00:00
|
|
|
cmd,
|
2020-05-15 03:14:47 +00:00
|
|
|
"clap-test -f=blah --g1=blah",
|
|
|
|
REQUIRE_EQUALS_FILTERED_GROUP,
|
2022-04-29 20:32:25 +00:00
|
|
|
true,
|
|
|
|
);
|
2020-05-15 03:14:47 +00:00
|
|
|
}
|
|
|
|
|
2019-10-02 13:27:19 +00:00
|
|
|
static ISSUE_1158: &str = "error: The following required arguments were not provided:
|
2018-07-25 18:46:28 +00:00
|
|
|
-x <X>
|
|
|
|
-y <Y>
|
|
|
|
-z <Z>
|
|
|
|
|
|
|
|
USAGE:
|
2021-05-20 18:59:21 +00:00
|
|
|
example -x <X> -y <Y> -z <Z> <ID>
|
2018-07-25 18:46:28 +00:00
|
|
|
|
2021-09-24 15:58:39 +00:00
|
|
|
For more information try --help
|
|
|
|
";
|
2018-07-25 18:46:28 +00:00
|
|
|
|
2022-02-12 03:48:29 +00:00
|
|
|
fn issue_1158_app() -> Command<'static> {
|
|
|
|
Command::new("example")
|
2018-07-25 18:46:28 +00:00
|
|
|
.arg(
|
2021-11-19 20:33:11 +00:00
|
|
|
arg!(-c --config <FILE> "Custom config file.")
|
|
|
|
.required(false)
|
2020-05-31 01:50:53 +00:00
|
|
|
.required_unless_present("ID")
|
2018-07-25 18:46:28 +00:00
|
|
|
.conflicts_with("ID"),
|
|
|
|
)
|
|
|
|
.arg(
|
2021-11-19 20:33:11 +00:00
|
|
|
arg!([ID] "ID")
|
2020-05-31 01:50:53 +00:00
|
|
|
.required_unless_present("config")
|
2018-07-25 18:46:28 +00:00
|
|
|
.conflicts_with("config")
|
|
|
|
.requires_all(&["x", "y", "z"]),
|
|
|
|
)
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(x: -x <X> "X").required(false))
|
|
|
|
.arg(arg!(y: -y <Y> "Y").required(false))
|
|
|
|
.arg(arg!(z: -z <Z> "Z").required(false))
|
2018-07-25 18:46:28 +00:00
|
|
|
}
|
|
|
|
|
2020-12-11 21:33:39 +00:00
|
|
|
#[test]
|
|
|
|
fn multiple_required_unless_usage_printing() {
|
2021-08-02 21:05:21 +00:00
|
|
|
static MULTIPLE_REQUIRED_UNLESS_USAGE: &str =
|
2020-12-11 21:33:39 +00:00
|
|
|
"error: The following required arguments were not provided:
|
|
|
|
--a <a>
|
|
|
|
--b <b>
|
|
|
|
|
|
|
|
USAGE:
|
|
|
|
test --c <c> --a <a> --b <b>
|
|
|
|
|
2021-09-24 15:58:39 +00:00
|
|
|
For more information try --help
|
|
|
|
";
|
2022-02-14 21:47:20 +00:00
|
|
|
let cmd = Command::new("test")
|
2020-12-11 21:33:39 +00:00
|
|
|
.arg(
|
|
|
|
Arg::new("a")
|
|
|
|
.long("a")
|
|
|
|
.takes_value(true)
|
|
|
|
.required_unless_present("b")
|
|
|
|
.conflicts_with("b"),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::new("b")
|
|
|
|
.long("b")
|
|
|
|
.takes_value(true)
|
|
|
|
.required_unless_present("a")
|
|
|
|
.conflicts_with("a"),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::new("c")
|
|
|
|
.long("c")
|
|
|
|
.takes_value(true)
|
|
|
|
.required_unless_present("d")
|
|
|
|
.conflicts_with("d"),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::new("d")
|
|
|
|
.long("d")
|
|
|
|
.takes_value(true)
|
|
|
|
.required_unless_present("c")
|
|
|
|
.conflicts_with("c"),
|
|
|
|
);
|
2022-04-29 20:32:25 +00:00
|
|
|
utils::assert_output(cmd, "test --c asd", MULTIPLE_REQUIRED_UNLESS_USAGE, true);
|
2020-12-11 21:33:39 +00:00
|
|
|
}
|
|
|
|
|
2018-07-25 18:46:28 +00:00
|
|
|
#[test]
|
|
|
|
fn issue_1158_conflicting_requirements() {
|
2022-02-14 21:47:20 +00:00
|
|
|
let cmd = issue_1158_app();
|
2018-07-25 18:46:28 +00:00
|
|
|
|
2022-04-29 20:32:25 +00:00
|
|
|
utils::assert_output(cmd, "example id", ISSUE_1158, true);
|
2018-07-25 18:46:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn issue_1158_conflicting_requirements_rev() {
|
|
|
|
let res = issue_1158_app().try_get_matches_from(&["", "--config", "some.conf"]);
|
|
|
|
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(res.is_ok(), "{}", res.unwrap_err());
|
2018-07-25 18:46:28 +00:00
|
|
|
}
|
2020-03-03 19:00:35 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn issue_1643_args_mutually_require_each_other() {
|
|
|
|
use clap::*;
|
|
|
|
|
2022-02-14 21:47:20 +00:00
|
|
|
let cmd = Command::new("test")
|
2020-03-05 10:34:29 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("relation_id")
|
2021-11-18 16:17:15 +00:00
|
|
|
.help("The relation id to get the data from")
|
2020-03-05 10:34:29 +00:00
|
|
|
.long("relation-id")
|
|
|
|
.short('r')
|
|
|
|
.takes_value(true)
|
|
|
|
.requires("remote_unit_name"),
|
|
|
|
)
|
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("remote_unit_name")
|
2021-11-18 16:17:15 +00:00
|
|
|
.help("The name of the remote unit to get data from")
|
2020-03-05 10:34:29 +00:00
|
|
|
.long("remote-unit")
|
|
|
|
.short('u')
|
|
|
|
.takes_value(true)
|
|
|
|
.requires("relation_id"),
|
|
|
|
);
|
|
|
|
|
2022-02-14 21:47:20 +00:00
|
|
|
cmd.try_get_matches_from(&["test", "-u", "hello", "-r", "farewell"])
|
2021-12-27 18:56:12 +00:00
|
|
|
.unwrap();
|
2020-03-03 19:00:35 +00:00
|
|
|
}
|
2020-04-09 12:03:52 +00:00
|
|
|
|
2021-07-30 04:00:58 +00:00
|
|
|
#[test]
|
|
|
|
fn short_flag_require_equals_with_minvals_zero() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::new("foo")
|
2021-07-30 04:00:58 +00:00
|
|
|
.arg(
|
|
|
|
Arg::new("check")
|
|
|
|
.short('c')
|
|
|
|
.min_values(0)
|
|
|
|
.require_equals(true),
|
|
|
|
)
|
|
|
|
.arg(Arg::new("unique").short('u'))
|
2021-12-27 18:56:12 +00:00
|
|
|
.try_get_matches_from(&["foo", "-cu"])
|
|
|
|
.unwrap();
|
2021-07-30 04:00:58 +00:00
|
|
|
assert!(m.is_present("check"));
|
|
|
|
assert!(m.is_present("unique"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn issue_2624() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let matches = Command::new("foo")
|
2021-07-30 04:00:58 +00:00
|
|
|
.arg(
|
|
|
|
Arg::new("check")
|
|
|
|
.short('c')
|
|
|
|
.long("check")
|
|
|
|
.require_equals(true)
|
|
|
|
.min_values(0)
|
2022-05-24 01:16:02 +00:00
|
|
|
.value_parser(["silent", "quiet", "diagnose-first"]),
|
2021-07-30 04:00:58 +00:00
|
|
|
)
|
|
|
|
.arg(Arg::new("unique").short('u').long("unique"))
|
2021-12-27 18:56:12 +00:00
|
|
|
.try_get_matches_from(&["foo", "-cu"])
|
|
|
|
.unwrap();
|
2021-07-30 04:00:58 +00:00
|
|
|
assert!(matches.is_present("check"));
|
|
|
|
assert!(matches.is_present("unique"));
|
|
|
|
}
|
|
|
|
|
2021-10-27 02:31:37 +00:00
|
|
|
#[test]
|
|
|
|
fn required_unless_all_with_any() {
|
2022-02-14 21:47:20 +00:00
|
|
|
let cmd = Command::new("prog")
|
2021-10-27 02:31:37 +00:00
|
|
|
.arg(Arg::new("foo").long("foo"))
|
|
|
|
.arg(Arg::new("bar").long("bar"))
|
|
|
|
.arg(Arg::new("baz").long("baz"))
|
|
|
|
.arg(
|
|
|
|
Arg::new("flag")
|
|
|
|
.long("flag")
|
|
|
|
.required_unless_present_any(&["foo"])
|
|
|
|
.required_unless_present_all(&["bar", "baz"]),
|
|
|
|
);
|
|
|
|
|
2022-02-14 21:47:20 +00:00
|
|
|
let result = cmd.clone().try_get_matches_from(vec!["myprog"]);
|
2021-10-27 02:31:37 +00:00
|
|
|
|
|
|
|
assert!(result.is_err(), "{:?}", result.unwrap());
|
|
|
|
|
2022-02-14 21:47:20 +00:00
|
|
|
let result = cmd.clone().try_get_matches_from(vec!["myprog", "--foo"]);
|
2021-10-27 02:31:37 +00:00
|
|
|
|
2021-10-29 17:27:33 +00:00
|
|
|
assert!(result.is_ok(), "{:?}", result.unwrap_err());
|
2021-10-27 02:31:37 +00:00
|
|
|
assert!(!result.unwrap().is_present("flag"));
|
|
|
|
|
2022-02-14 21:47:20 +00:00
|
|
|
let result = cmd
|
2021-10-27 02:31:37 +00:00
|
|
|
.clone()
|
|
|
|
.try_get_matches_from(vec!["myprog", "--bar", "--baz"]);
|
|
|
|
|
2021-10-29 17:27:33 +00:00
|
|
|
assert!(result.is_ok(), "{:?}", result.unwrap_err());
|
2021-10-27 02:31:37 +00:00
|
|
|
assert!(!result.unwrap().is_present("flag"));
|
|
|
|
|
2022-02-14 21:47:20 +00:00
|
|
|
let result = cmd.try_get_matches_from(vec!["myprog", "--bar"]);
|
2021-10-27 02:31:37 +00:00
|
|
|
|
|
|
|
assert!(result.is_err(), "{:?}", result.unwrap());
|
|
|
|
}
|
|
|
|
|
2020-04-09 12:03:52 +00:00
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
#[test]
|
2020-04-24 19:34:23 +00:00
|
|
|
#[should_panic = "Argument or group 'extra' specified in 'requires*' for 'config' does not exist"]
|
2020-04-09 12:03:52 +00:00
|
|
|
fn requires_invalid_arg() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let _ = Command::new("prog")
|
2020-05-14 20:50:56 +00:00
|
|
|
.arg(Arg::new("config").requires("extra").long("config"))
|
2020-04-09 12:03:52 +00:00
|
|
|
.try_get_matches_from(vec!["", "--config"]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
#[test]
|
2020-04-24 19:34:23 +00:00
|
|
|
#[should_panic = "Argument or group 'extra' specified in 'requires*' for 'config' does not exist"]
|
2020-04-09 12:03:52 +00:00
|
|
|
fn requires_if_invalid_arg() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let _ = Command::new("prog")
|
2020-04-09 12:03:52 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("config")
|
2020-04-09 12:03:52 +00:00
|
|
|
.requires_if("val", "extra")
|
|
|
|
.long("config"),
|
|
|
|
)
|
|
|
|
.try_get_matches_from(vec!["", "--config"]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
#[test]
|
2020-05-23 14:02:03 +00:00
|
|
|
#[should_panic = "Argument or group 'extra' specified in 'required_if_eq*' for 'config' does not exist"]
|
2020-04-09 12:03:52 +00:00
|
|
|
fn required_if_invalid_arg() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let _ = Command::new("prog")
|
2020-04-09 12:03:52 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("config")
|
2020-05-23 14:02:03 +00:00
|
|
|
.required_if_eq("extra", "val")
|
2020-04-09 12:03:52 +00:00
|
|
|
.long("config"),
|
|
|
|
)
|
|
|
|
.try_get_matches_from(vec!["", "--config"]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
#[test]
|
2020-04-24 19:34:23 +00:00
|
|
|
#[should_panic = "Argument or group 'extra' specified in 'required_unless*' for 'config' does not exist"]
|
2020-04-09 12:03:52 +00:00
|
|
|
fn required_unless_invalid_arg() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let _ = Command::new("prog")
|
2020-05-31 01:50:53 +00:00
|
|
|
.arg(
|
|
|
|
Arg::new("config")
|
|
|
|
.required_unless_present("extra")
|
|
|
|
.long("config"),
|
|
|
|
)
|
2020-04-09 12:03:52 +00:00
|
|
|
.try_get_matches_from(vec![""]);
|
|
|
|
}
|
2022-02-08 03:45:30 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn requires_with_default_value() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let result = Command::new("prog")
|
2022-02-08 03:45:30 +00:00
|
|
|
.arg(
|
|
|
|
Arg::new("opt")
|
|
|
|
.long("opt")
|
|
|
|
.default_value("default")
|
|
|
|
.requires("flag"),
|
|
|
|
)
|
|
|
|
.arg(Arg::new("flag").long("flag"))
|
|
|
|
.try_get_matches_from(vec!["myprog"]);
|
|
|
|
|
|
|
|
assert!(
|
|
|
|
result.is_ok(),
|
|
|
|
"requires should ignore default_value: {:?}",
|
|
|
|
result.unwrap_err()
|
|
|
|
);
|
|
|
|
let m = result.unwrap();
|
|
|
|
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("opt").map(|v| v.as_str()),
|
|
|
|
Some("default")
|
|
|
|
);
|
2022-02-08 03:45:30 +00:00
|
|
|
assert!(!m.is_present("flag"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn requires_if_with_default_value() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let result = Command::new("prog")
|
2022-02-08 03:45:30 +00:00
|
|
|
.arg(
|
|
|
|
Arg::new("opt")
|
|
|
|
.long("opt")
|
|
|
|
.default_value("default")
|
|
|
|
.requires_if("default", "flag"),
|
|
|
|
)
|
|
|
|
.arg(Arg::new("flag").long("flag"))
|
|
|
|
.try_get_matches_from(vec!["myprog"]);
|
|
|
|
|
|
|
|
assert!(
|
|
|
|
result.is_ok(),
|
|
|
|
"requires_if should ignore default_value: {:?}",
|
|
|
|
result.unwrap_err()
|
|
|
|
);
|
|
|
|
let m = result.unwrap();
|
|
|
|
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("opt").map(|v| v.as_str()),
|
|
|
|
Some("default")
|
|
|
|
);
|
2022-02-08 03:45:30 +00:00
|
|
|
assert!(!m.is_present("flag"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn group_requires_with_default_value() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let result = Command::new("prog")
|
2022-02-08 03:45:30 +00:00
|
|
|
.arg(Arg::new("opt").long("opt").default_value("default"))
|
|
|
|
.arg(Arg::new("flag").long("flag"))
|
|
|
|
.group(ArgGroup::new("one").arg("opt").requires("flag"))
|
|
|
|
.try_get_matches_from(vec!["myprog"]);
|
|
|
|
|
|
|
|
assert!(
|
|
|
|
result.is_ok(),
|
|
|
|
"arg group requires should ignore default_value: {:?}",
|
|
|
|
result.unwrap_err()
|
|
|
|
);
|
|
|
|
let m = result.unwrap();
|
|
|
|
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("opt").map(|v| v.as_str()),
|
|
|
|
Some("default")
|
|
|
|
);
|
2022-02-08 03:45:30 +00:00
|
|
|
assert!(!m.is_present("flag"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn required_if_eq_on_default_value() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let result = Command::new("prog")
|
2022-02-08 03:45:30 +00:00
|
|
|
.arg(Arg::new("opt").long("opt").default_value("default"))
|
|
|
|
.arg(
|
|
|
|
Arg::new("flag")
|
|
|
|
.long("flag")
|
|
|
|
.required_if_eq("opt", "default"),
|
|
|
|
)
|
|
|
|
.try_get_matches_from(vec!["myprog"]);
|
|
|
|
|
|
|
|
assert!(
|
|
|
|
result.is_ok(),
|
|
|
|
"required_if_eq should ignore default_value: {:?}",
|
|
|
|
result.unwrap_err()
|
|
|
|
);
|
|
|
|
let m = result.unwrap();
|
|
|
|
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("opt").map(|v| v.as_str()),
|
|
|
|
Some("default")
|
|
|
|
);
|
2022-02-08 03:45:30 +00:00
|
|
|
assert!(!m.is_present("flag"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn required_if_eq_all_on_default_value() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let result = Command::new("prog")
|
2022-02-08 03:45:30 +00:00
|
|
|
.arg(Arg::new("opt").long("opt").default_value("default"))
|
|
|
|
.arg(
|
|
|
|
Arg::new("flag")
|
|
|
|
.long("flag")
|
|
|
|
.required_if_eq_all(&[("opt", "default")]),
|
|
|
|
)
|
|
|
|
.try_get_matches_from(vec!["myprog"]);
|
|
|
|
|
|
|
|
assert!(
|
|
|
|
result.is_ok(),
|
|
|
|
"required_if_eq_all should ignore default_value: {:?}",
|
|
|
|
result.unwrap_err()
|
|
|
|
);
|
|
|
|
let m = result.unwrap();
|
|
|
|
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("opt").map(|v| v.as_str()),
|
|
|
|
Some("default")
|
|
|
|
);
|
2022-02-08 03:45:30 +00:00
|
|
|
assert!(!m.is_present("flag"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn required_unless_on_default_value() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let result = Command::new("prog")
|
2022-02-08 03:45:30 +00:00
|
|
|
.arg(Arg::new("opt").long("opt").default_value("default"))
|
|
|
|
.arg(Arg::new("flag").long("flag").required_unless_present("opt"))
|
|
|
|
.try_get_matches_from(vec!["myprog"]);
|
|
|
|
|
|
|
|
assert!(result.is_err(), "{:?}", result.unwrap());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn required_unless_all_on_default_value() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let result = Command::new("prog")
|
2022-02-08 03:45:30 +00:00
|
|
|
.arg(Arg::new("opt").long("opt").default_value("default"))
|
|
|
|
.arg(
|
|
|
|
Arg::new("flag")
|
|
|
|
.long("flag")
|
|
|
|
.required_unless_present_all(&["opt"]),
|
|
|
|
)
|
|
|
|
.try_get_matches_from(vec!["myprog"]);
|
|
|
|
|
|
|
|
assert!(result.is_err(), "{:?}", result.unwrap());
|
|
|
|
}
|
2022-05-04 16:05:34 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn required_error_doesnt_duplicate() {
|
|
|
|
let cmd = Command::new("Clap-created-USAGE-string-bug")
|
|
|
|
.arg(Arg::new("a").required(true))
|
|
|
|
.arg(
|
|
|
|
Arg::new("b")
|
|
|
|
.short('b')
|
|
|
|
.takes_value(true)
|
|
|
|
.conflicts_with("c"),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::new("c")
|
|
|
|
.short('c')
|
|
|
|
.takes_value(true)
|
|
|
|
.conflicts_with("b"),
|
|
|
|
);
|
|
|
|
const EXPECTED: &str = "\
|
|
|
|
error: The argument '-b <b>' cannot be used with '-c <c>'
|
|
|
|
|
|
|
|
USAGE:
|
|
|
|
clap-test -b <b> <a>
|
|
|
|
|
|
|
|
For more information try --help
|
|
|
|
";
|
|
|
|
utils::assert_output(cmd, "clap-test aaa -b bbb -c ccc", EXPECTED, true);
|
|
|
|
}
|