2015-09-06 20:08:13 +00:00
|
|
|
extern crate clap;
|
2016-05-10 02:46:09 +00:00
|
|
|
extern crate regex;
|
|
|
|
|
|
|
|
include!("../clap-test.rs");
|
2015-09-06 20:08:13 +00:00
|
|
|
|
2018-01-25 04:05:05 +00:00
|
|
|
use clap::{App, Arg, ArgGroup, ErrorKind};
|
2015-09-06 20:08:13 +00:00
|
|
|
|
2017-03-17 01:45:45 +00:00
|
|
|
static REQUIRE_EQUALS: &'static str = "error: The following required arguments were not provided:
|
|
|
|
--opt=<FILE>
|
|
|
|
|
|
|
|
USAGE:
|
|
|
|
clap-test --opt=<FILE>
|
|
|
|
|
|
|
|
For more information try --help";
|
|
|
|
|
2017-01-03 04:05:23 +00:00
|
|
|
static MISSING_REQ: &'static str = "error: The following required arguments were not provided:
|
|
|
|
<positional2>
|
|
|
|
--long-option-2 <option2>
|
|
|
|
|
|
|
|
USAGE:
|
|
|
|
clap-test <positional2> -F --long-option-2 <option2>
|
|
|
|
|
|
|
|
For more information try --help";
|
|
|
|
|
2018-01-25 04:05:05 +00:00
|
|
|
static COND_REQ_IN_USAGE: &'static str =
|
|
|
|
"error: The following required arguments were not provided:
|
2017-02-03 22:43:49 +00:00
|
|
|
--output <output>
|
|
|
|
|
|
|
|
USAGE:
|
2017-02-21 04:51:20 +00:00
|
|
|
test --input <input> --output <output> --target <target>
|
2017-02-03 22:43:49 +00:00
|
|
|
|
|
|
|
For more information try --help";
|
|
|
|
|
2015-09-06 20:08:13 +00:00
|
|
|
#[test]
|
|
|
|
fn flag_required() {
|
|
|
|
let result = App::new("flag_required")
|
2017-02-03 22:43:49 +00:00
|
|
|
.arg(Arg::from_usage("-f, --flag 'some flag'").requires("color"))
|
2015-09-06 20:08:13 +00:00
|
|
|
.arg(Arg::from_usage("-c, --color 'third flag'"))
|
2016-01-25 20:56:37 +00:00
|
|
|
.get_matches_from_safe(vec!["", "-f"]);
|
2015-09-06 20:08:13 +00:00
|
|
|
assert!(result.is_err());
|
|
|
|
let err = result.err().unwrap();
|
2016-01-21 05:18:53 +00:00
|
|
|
assert_eq!(err.kind, ErrorKind::MissingRequiredArgument);
|
2015-09-06 20:08:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn flag_required_2() {
|
|
|
|
let m = App::new("flag_required")
|
2017-02-03 22:43:49 +00:00
|
|
|
.arg(Arg::from_usage("-f, --flag 'some flag'").requires("color"))
|
2015-09-06 20:08:13 +00:00
|
|
|
.arg(Arg::from_usage("-c, --color 'third flag'"))
|
2016-01-25 20:56:37 +00:00
|
|
|
.get_matches_from(vec!["", "-f", "-c"]);
|
2015-09-06 20:08:13 +00:00
|
|
|
assert!(m.is_present("color"));
|
|
|
|
assert!(m.is_present("flag"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn option_required() {
|
|
|
|
let result = App::new("option_required")
|
2017-02-21 04:51:20 +00:00
|
|
|
.arg(Arg::from_usage("-f [flag] 'some flag'").requires("c"))
|
2015-09-06 20:08:13 +00:00
|
|
|
.arg(Arg::from_usage("-c [color] 'third flag'"))
|
2016-01-25 20:56:37 +00:00
|
|
|
.get_matches_from_safe(vec!["", "-f", "val"]);
|
2015-09-06 20:08:13 +00:00
|
|
|
assert!(result.is_err());
|
|
|
|
let err = result.err().unwrap();
|
2016-01-21 05:18:53 +00:00
|
|
|
assert_eq!(err.kind, ErrorKind::MissingRequiredArgument);
|
2015-09-06 20:08:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn option_required_2() {
|
|
|
|
let m = App::new("option_required")
|
2017-02-03 22:43:49 +00:00
|
|
|
.arg(Arg::from_usage("-f [flag] 'some flag'").requires("c"))
|
2015-09-06 20:08:13 +00:00
|
|
|
.arg(Arg::from_usage("-c [color] 'third flag'"))
|
2016-01-25 20:56:37 +00:00
|
|
|
.get_matches_from(vec!["", "-f", "val", "-c", "other_val"]);
|
|
|
|
assert!(m.is_present("c"));
|
|
|
|
assert_eq!(m.value_of("c").unwrap(), "other_val");
|
|
|
|
assert!(m.is_present("f"));
|
|
|
|
assert_eq!(m.value_of("f").unwrap(), "val");
|
2015-09-06 20:08:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn positional_required() {
|
|
|
|
let result = App::new("positional_required")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(Arg::with_name("flag").index(1).required(true))
|
2015-09-06 20:08:13 +00:00
|
|
|
.get_matches_from_safe(vec![""]);
|
|
|
|
assert!(result.is_err());
|
|
|
|
let err = result.err().unwrap();
|
2016-01-21 05:18:53 +00:00
|
|
|
assert_eq!(err.kind, ErrorKind::MissingRequiredArgument);
|
2015-09-06 20:08:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn positional_required_2() {
|
|
|
|
let m = App::new("positional_required")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(Arg::with_name("flag").index(1).required(true))
|
2016-01-25 20:56:37 +00:00
|
|
|
.get_matches_from(vec!["", "someval"]);
|
2015-09-06 20:08:13 +00:00
|
|
|
assert!(m.is_present("flag"));
|
|
|
|
assert_eq!(m.value_of("flag").unwrap(), "someval");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn group_required() {
|
|
|
|
let result = App::new("group_required")
|
|
|
|
.arg(Arg::from_usage("-f, --flag 'some flag'"))
|
2018-01-25 04:05:05 +00:00
|
|
|
.group(
|
|
|
|
ArgGroup::with_name("gr")
|
|
|
|
.required(true)
|
|
|
|
.arg("some")
|
|
|
|
.arg("other"),
|
|
|
|
)
|
2015-09-06 20:08:13 +00:00
|
|
|
.arg(Arg::from_usage("--some 'some arg'"))
|
|
|
|
.arg(Arg::from_usage("--other 'other arg'"))
|
2016-01-25 20:56:37 +00:00
|
|
|
.get_matches_from_safe(vec!["", "-f"]);
|
2015-09-06 20:08:13 +00:00
|
|
|
assert!(result.is_err());
|
|
|
|
let err = result.err().unwrap();
|
2016-01-21 05:18:53 +00:00
|
|
|
assert_eq!(err.kind, ErrorKind::MissingRequiredArgument);
|
2015-09-06 20:08:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn group_required_2() {
|
|
|
|
let m = App::new("group_required")
|
|
|
|
.arg(Arg::from_usage("-f, --flag 'some flag'"))
|
2018-01-25 04:05:05 +00:00
|
|
|
.group(
|
|
|
|
ArgGroup::with_name("gr")
|
|
|
|
.required(true)
|
|
|
|
.arg("some")
|
|
|
|
.arg("other"),
|
|
|
|
)
|
2015-09-06 20:08:13 +00:00
|
|
|
.arg(Arg::from_usage("--some 'some arg'"))
|
|
|
|
.arg(Arg::from_usage("--other 'other arg'"))
|
2016-01-25 20:56:37 +00:00
|
|
|
.get_matches_from(vec!["", "-f", "--some"]);
|
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() {
|
|
|
|
let m = App::new("group_required")
|
|
|
|
.arg(Arg::from_usage("-f, --flag 'some flag'"))
|
2018-01-25 04:05:05 +00:00
|
|
|
.group(
|
|
|
|
ArgGroup::with_name("gr")
|
|
|
|
.required(true)
|
|
|
|
.arg("some")
|
|
|
|
.arg("other"),
|
|
|
|
)
|
2015-09-06 20:08:13 +00:00
|
|
|
.arg(Arg::from_usage("--some 'some arg'"))
|
|
|
|
.arg(Arg::from_usage("--other 'other arg'"))
|
2016-01-25 20:56:37 +00:00
|
|
|
.get_matches_from(vec!["", "-f", "--other"]);
|
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() {
|
|
|
|
let result = App::new("arg_require_group")
|
2017-02-03 22:43:49 +00:00
|
|
|
.arg(Arg::from_usage("-f, --flag 'some flag'").requires("gr"))
|
2018-01-25 04:05:05 +00:00
|
|
|
.group(ArgGroup::with_name("gr").arg("some").arg("other"))
|
2015-09-06 20:08:13 +00:00
|
|
|
.arg(Arg::from_usage("--some 'some arg'"))
|
|
|
|
.arg(Arg::from_usage("--other 'other arg'"))
|
2016-01-25 20:56:37 +00:00
|
|
|
.get_matches_from_safe(vec!["", "-f"]);
|
2015-09-06 20:08:13 +00:00
|
|
|
assert!(result.is_err());
|
|
|
|
let err = result.err().unwrap();
|
2016-01-21 05:18:53 +00:00
|
|
|
assert_eq!(err.kind, ErrorKind::MissingRequiredArgument);
|
2015-09-06 20:08:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn arg_require_group_2() {
|
|
|
|
let m = App::new("arg_require_group")
|
2017-02-03 22:43:49 +00:00
|
|
|
.arg(Arg::from_usage("-f, --flag 'some flag'").requires("gr"))
|
2018-01-25 04:05:05 +00:00
|
|
|
.group(ArgGroup::with_name("gr").arg("some").arg("other"))
|
2015-09-06 20:08:13 +00:00
|
|
|
.arg(Arg::from_usage("--some 'some arg'"))
|
|
|
|
.arg(Arg::from_usage("--other 'other arg'"))
|
2016-01-25 20:56:37 +00:00
|
|
|
.get_matches_from(vec!["", "-f", "--some"]);
|
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() {
|
|
|
|
let m = App::new("arg_require_group")
|
2017-02-03 22:43:49 +00:00
|
|
|
.arg(Arg::from_usage("-f, --flag 'some flag'").requires("gr"))
|
2018-01-25 04:05:05 +00:00
|
|
|
.group(ArgGroup::with_name("gr").arg("some").arg("other"))
|
2015-09-06 20:08:13 +00:00
|
|
|
.arg(Arg::from_usage("--some 'some arg'"))
|
|
|
|
.arg(Arg::from_usage("--other 'other arg'"))
|
2016-01-25 20:56:37 +00:00
|
|
|
.get_matches_from(vec!["", "-f", "--other"]);
|
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() {
|
|
|
|
let m = App::new("test")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(Arg::from_usage(
|
|
|
|
"-l, --list 'List available interfaces (and stop there)'",
|
|
|
|
))
|
|
|
|
.arg(
|
|
|
|
Arg::from_usage(
|
|
|
|
"-i, --iface=[INTERFACE] 'Ethernet interface for fetching NTP packets'",
|
|
|
|
).required_unless("list"),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::from_usage("-f, --file=[TESTFILE] 'Fetch NTP packets from pcap file'")
|
|
|
|
.conflicts_with("iface")
|
|
|
|
.required_unless("list"),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::from_usage("-s, --server=[SERVER_IP] 'NTP server IP address'")
|
|
|
|
.required_unless("list"),
|
|
|
|
)
|
|
|
|
.arg(Arg::from_usage("-p, --port=[SERVER_PORT] 'NTP server port'").default_value("123"))
|
2016-11-20 14:48:16 +00:00
|
|
|
.get_matches_from_safe(vec!["test", "--list"]);
|
2017-02-03 22:43:49 +00:00
|
|
|
assert!(m.is_ok());
|
2016-11-20 14:48:16 +00:00
|
|
|
}
|
|
|
|
|
2016-05-02 18:12:57 +00:00
|
|
|
#[test]
|
|
|
|
fn required_unless() {
|
|
|
|
let res = App::new("unlesstest")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
|
|
|
Arg::with_name("cfg")
|
|
|
|
.required_unless("dbg")
|
|
|
|
.takes_value(true)
|
|
|
|
.long("config"),
|
|
|
|
)
|
2017-02-03 22:43:49 +00:00
|
|
|
.arg(Arg::with_name("dbg").long("debug"))
|
|
|
|
.get_matches_from_safe(vec!["unlesstest", "--debug"]);
|
2016-05-02 18:12:57 +00:00
|
|
|
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let m = res.unwrap();
|
|
|
|
assert!(m.is_present("dbg"));
|
|
|
|
assert!(!m.is_present("cfg"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn required_unless_err() {
|
|
|
|
let res = App::new("unlesstest")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
|
|
|
Arg::with_name("cfg")
|
|
|
|
.required_unless("dbg")
|
|
|
|
.takes_value(true)
|
|
|
|
.long("config"),
|
|
|
|
)
|
2017-02-03 22:43:49 +00:00
|
|
|
.arg(Arg::with_name("dbg").long("debug"))
|
|
|
|
.get_matches_from_safe(vec!["unlesstest"]);
|
2016-05-02 18:12:57 +00:00
|
|
|
|
|
|
|
assert!(res.is_err());
|
|
|
|
assert_eq!(res.unwrap_err().kind, ErrorKind::MissingRequiredArgument);
|
|
|
|
}
|
|
|
|
|
|
|
|
// REQUIRED_UNLESS_ALL
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn required_unless_all() {
|
|
|
|
let res = App::new("unlessall")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
|
|
|
Arg::with_name("cfg")
|
|
|
|
.required_unless_all(&["dbg", "infile"])
|
|
|
|
.takes_value(true)
|
|
|
|
.long("config"),
|
|
|
|
)
|
2017-02-03 22:43:49 +00:00
|
|
|
.arg(Arg::with_name("dbg").long("debug"))
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(Arg::with_name("infile").short("i").takes_value(true))
|
2017-02-03 22:43:49 +00:00
|
|
|
.get_matches_from_safe(vec!["unlessall", "--debug", "-i", "file"]);
|
2016-05-02 18:12:57 +00:00
|
|
|
|
|
|
|
assert!(res.is_ok());
|
|
|
|
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() {
|
|
|
|
let res = App::new("unlessall")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
|
|
|
Arg::with_name("cfg")
|
|
|
|
.required_unless_all(&["dbg", "infile"])
|
|
|
|
.takes_value(true)
|
|
|
|
.long("config"),
|
|
|
|
)
|
2017-02-03 22:43:49 +00:00
|
|
|
.arg(Arg::with_name("dbg").long("debug"))
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(Arg::with_name("infile").short("i").takes_value(true))
|
2017-02-03 22:43:49 +00:00
|
|
|
.get_matches_from_safe(vec!["unlessall", "--debug"]);
|
2016-05-02 18:12:57 +00:00
|
|
|
|
|
|
|
assert!(res.is_err());
|
|
|
|
assert_eq!(res.unwrap_err().kind, ErrorKind::MissingRequiredArgument);
|
|
|
|
}
|
|
|
|
|
|
|
|
// REQUIRED_UNLESS_ONE
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn required_unless_one() {
|
|
|
|
let res = App::new("unlessone")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
|
|
|
Arg::with_name("cfg")
|
|
|
|
.required_unless_one(&["dbg", "infile"])
|
|
|
|
.takes_value(true)
|
|
|
|
.long("config"),
|
|
|
|
)
|
2017-02-03 22:43:49 +00:00
|
|
|
.arg(Arg::with_name("dbg").long("debug"))
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(Arg::with_name("infile").short("i").takes_value(true))
|
2017-02-03 22:43:49 +00:00
|
|
|
.get_matches_from_safe(vec!["unlessone", "--debug"]);
|
2016-05-02 18:12:57 +00:00
|
|
|
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let m = res.unwrap();
|
|
|
|
assert!(m.is_present("dbg"));
|
|
|
|
assert!(!m.is_present("cfg"));
|
|
|
|
}
|
|
|
|
|
2016-07-23 17:43:22 +00:00
|
|
|
#[test]
|
|
|
|
fn required_unless_one_2() {
|
|
|
|
// This tests that the required_unless_one works when the second arg in the array is used
|
|
|
|
// instead of the first.
|
|
|
|
let res = App::new("unlessone")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
|
|
|
Arg::with_name("cfg")
|
|
|
|
.required_unless_one(&["dbg", "infile"])
|
|
|
|
.takes_value(true)
|
|
|
|
.long("config"),
|
|
|
|
)
|
2017-02-03 22:43:49 +00:00
|
|
|
.arg(Arg::with_name("dbg").long("debug"))
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(Arg::with_name("infile").short("i").takes_value(true))
|
2017-02-03 22:43:49 +00:00
|
|
|
.get_matches_from_safe(vec!["unlessone", "-i", "file"]);
|
2016-07-23 17:43:22 +00:00
|
|
|
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let m = res.unwrap();
|
|
|
|
assert!(m.is_present("infile"));
|
|
|
|
assert!(!m.is_present("cfg"));
|
|
|
|
}
|
|
|
|
|
2017-12-26 16:16:18 +00:00
|
|
|
#[test]
|
|
|
|
fn required_unless_one_works_with_short() {
|
|
|
|
// GitHub issue: https://github.com/kbknapp/clap-rs/issues/1135
|
|
|
|
let res = App::new("unlessone")
|
|
|
|
.arg(Arg::with_name("a").conflicts_with("b").short("a"))
|
|
|
|
.arg(Arg::with_name("b").short("b"))
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("x")
|
|
|
|
.short("x")
|
2018-01-25 04:05:05 +00:00
|
|
|
.required_unless_one(&["a", "b"]),
|
|
|
|
)
|
|
|
|
.get_matches_from_safe(vec!["unlessone", "-a"]);
|
2017-12-26 16:16:18 +00:00
|
|
|
|
|
|
|
assert!(res.is_ok());
|
|
|
|
}
|
|
|
|
|
2018-01-19 01:41:23 +00:00
|
|
|
#[test]
|
|
|
|
fn required_unless_one_works_with_short_err() {
|
|
|
|
let res = App::new("unlessone")
|
|
|
|
.arg(Arg::with_name("a").conflicts_with("b").short("a"))
|
|
|
|
.arg(Arg::with_name("b").short("b"))
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("x")
|
|
|
|
.short("x")
|
2018-01-25 04:05:05 +00:00
|
|
|
.required_unless_one(&["a", "b"]),
|
|
|
|
)
|
|
|
|
.get_matches_from_safe(vec!["unlessone"]);
|
2018-01-19 01:41:23 +00:00
|
|
|
|
|
|
|
assert!(!res.is_ok());
|
|
|
|
}
|
|
|
|
|
2018-01-19 01:30:12 +00:00
|
|
|
#[test]
|
|
|
|
fn required_unless_one_works_without() {
|
|
|
|
let res = App::new("unlessone")
|
|
|
|
.arg(Arg::with_name("a").conflicts_with("b").short("a"))
|
|
|
|
.arg(Arg::with_name("b").short("b"))
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(Arg::with_name("x").required_unless_one(&["a", "b"]))
|
|
|
|
.get_matches_from_safe(vec!["unlessone", "-a"]);
|
2018-01-19 01:30:12 +00:00
|
|
|
|
|
|
|
assert!(res.is_ok());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn required_unless_one_works_with_long() {
|
|
|
|
let res = App::new("unlessone")
|
|
|
|
.arg(Arg::with_name("a").conflicts_with("b").short("a"))
|
|
|
|
.arg(Arg::with_name("b").short("b"))
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("x")
|
|
|
|
.long("x_is_the_option")
|
2018-01-25 04:05:05 +00:00
|
|
|
.required_unless_one(&["a", "b"]),
|
|
|
|
)
|
|
|
|
.get_matches_from_safe(vec!["unlessone", "-a"]);
|
2018-01-19 01:30:12 +00:00
|
|
|
|
|
|
|
assert!(res.is_ok());
|
|
|
|
}
|
|
|
|
|
2016-07-23 21:22:14 +00:00
|
|
|
#[test]
|
|
|
|
fn required_unless_one_1() {
|
|
|
|
let res = App::new("unlessone")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
|
|
|
Arg::with_name("cfg")
|
|
|
|
.required_unless_one(&["dbg", "infile"])
|
|
|
|
.takes_value(true)
|
|
|
|
.long("config"),
|
|
|
|
)
|
2017-02-03 22:43:49 +00:00
|
|
|
.arg(Arg::with_name("dbg").long("debug"))
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(Arg::with_name("infile").short("i").takes_value(true))
|
2017-02-03 22:43:49 +00:00
|
|
|
.get_matches_from_safe(vec!["unlessone", "--debug"]);
|
2016-07-23 21:22:14 +00:00
|
|
|
|
|
|
|
assert!(res.is_ok());
|
|
|
|
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]
|
|
|
|
fn required_unless_one_err() {
|
|
|
|
let res = App::new("unlessone")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
|
|
|
Arg::with_name("cfg")
|
|
|
|
.required_unless_one(&["dbg", "infile"])
|
|
|
|
.takes_value(true)
|
|
|
|
.long("config"),
|
|
|
|
)
|
2017-02-03 22:43:49 +00:00
|
|
|
.arg(Arg::with_name("dbg").long("debug"))
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(Arg::with_name("infile").short("i").takes_value(true))
|
2017-02-03 22:43:49 +00:00
|
|
|
.get_matches_from_safe(vec!["unlessone"]);
|
2016-05-02 18:12:57 +00:00
|
|
|
|
|
|
|
assert!(res.is_err());
|
|
|
|
assert_eq!(res.unwrap_err().kind, ErrorKind::MissingRequiredArgument);
|
|
|
|
}
|
2016-05-09 03:20:50 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn missing_required_output() {
|
2018-01-25 04:05:05 +00:00
|
|
|
assert!(test::compare_output(
|
|
|
|
test::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() {
|
|
|
|
let res = App::new("unlessone")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
|
|
|
Arg::with_name("cfg")
|
|
|
|
.requires_if("my.cfg", "extra")
|
|
|
|
.takes_value(true)
|
|
|
|
.long("config"),
|
|
|
|
)
|
2017-02-03 22:43:49 +00:00
|
|
|
.arg(Arg::with_name("extra").long("extra"))
|
|
|
|
.get_matches_from_safe(vec!["unlessone", "--config=my.cfg"]);
|
2016-12-14 16:41:21 +00:00
|
|
|
|
|
|
|
assert!(res.is_err());
|
|
|
|
assert_eq!(res.unwrap_err().kind, ErrorKind::MissingRequiredArgument);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn requires_if_present_mult() {
|
|
|
|
let res = App::new("unlessone")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
|
|
|
Arg::with_name("cfg")
|
|
|
|
.requires_ifs(&[("my.cfg", "extra"), ("other.cfg", "other")])
|
|
|
|
.takes_value(true)
|
|
|
|
.long("config"),
|
|
|
|
)
|
2017-02-03 22:43:49 +00:00
|
|
|
.arg(Arg::with_name("extra").long("extra"))
|
|
|
|
.arg(Arg::with_name("other").long("other"))
|
|
|
|
.get_matches_from_safe(vec!["unlessone", "--config=other.cfg"]);
|
2016-12-14 16:41:21 +00:00
|
|
|
|
|
|
|
assert!(res.is_err());
|
|
|
|
assert_eq!(res.unwrap_err().kind, ErrorKind::MissingRequiredArgument);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn requires_if_present_mult_pass() {
|
|
|
|
let res = App::new("unlessone")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
|
|
|
Arg::with_name("cfg")
|
|
|
|
.requires_ifs(&[("my.cfg", "extra"), ("other.cfg", "other")])
|
|
|
|
.takes_value(true)
|
|
|
|
.long("config"),
|
|
|
|
)
|
2017-02-03 22:43:49 +00:00
|
|
|
.arg(Arg::with_name("extra").long("extra"))
|
|
|
|
.arg(Arg::with_name("other").long("other"))
|
|
|
|
.get_matches_from_safe(vec!["unlessone", "--config=some.cfg"]);
|
2016-12-14 16:41:21 +00:00
|
|
|
|
|
|
|
assert!(res.is_ok());
|
|
|
|
// assert_eq!(res.unwrap_err().kind, ErrorKind::MissingRequiredArgument);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn requires_if_present_val_no_present_pass() {
|
|
|
|
let res = App::new("unlessone")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
|
|
|
Arg::with_name("cfg")
|
|
|
|
.requires_if("my.cfg", "extra")
|
|
|
|
.takes_value(true)
|
|
|
|
.long("config"),
|
|
|
|
)
|
2017-02-03 22:43:49 +00:00
|
|
|
.arg(Arg::with_name("extra").long("extra"))
|
|
|
|
.get_matches_from_safe(vec!["unlessone"]);
|
2016-12-14 16:41:21 +00:00
|
|
|
|
|
|
|
assert!(res.is_ok());
|
2016-12-29 01:35:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Conditionally required
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn required_if_val_present_pass() {
|
|
|
|
let res = App::new("ri")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
|
|
|
Arg::with_name("cfg")
|
|
|
|
.required_if("extra", "val")
|
|
|
|
.takes_value(true)
|
|
|
|
.long("config"),
|
|
|
|
)
|
|
|
|
.arg(Arg::with_name("extra").takes_value(true).long("extra"))
|
2017-02-03 22:43:49 +00:00
|
|
|
.get_matches_from_safe(vec!["ri", "--extra", "val", "--config", "my.cfg"]);
|
2016-12-29 01:35:34 +00:00
|
|
|
|
|
|
|
assert!(res.is_ok());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn required_if_val_present_fail() {
|
|
|
|
let res = App::new("ri")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
|
|
|
Arg::with_name("cfg")
|
|
|
|
.required_if("extra", "val")
|
|
|
|
.takes_value(true)
|
|
|
|
.long("config"),
|
|
|
|
)
|
|
|
|
.arg(Arg::with_name("extra").takes_value(true).long("extra"))
|
2017-02-03 22:43:49 +00:00
|
|
|
.get_matches_from_safe(vec!["ri", "--extra", "val"]);
|
2016-12-29 01:35:34 +00:00
|
|
|
|
|
|
|
assert!(res.is_err());
|
|
|
|
assert_eq!(res.unwrap_err().kind, ErrorKind::MissingRequiredArgument);
|
|
|
|
}
|
|
|
|
|
2017-02-03 22:43:49 +00:00
|
|
|
#[test]
|
|
|
|
fn required_if_val_present_fail_error_output() {
|
|
|
|
let app = App::new("Test app")
|
|
|
|
.version("1.0")
|
|
|
|
.author("F0x06")
|
|
|
|
.about("Arg test")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
|
|
|
Arg::with_name("target")
|
|
|
|
.takes_value(true)
|
|
|
|
.required(true)
|
|
|
|
.possible_values(&["file", "stdout"])
|
|
|
|
.long("target"),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("input")
|
|
|
|
.takes_value(true)
|
|
|
|
.required(true)
|
|
|
|
.long("input"),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("output")
|
|
|
|
.takes_value(true)
|
|
|
|
.required_if("target", "file")
|
|
|
|
.long("output"),
|
|
|
|
);
|
2017-02-03 22:43:49 +00:00
|
|
|
|
2018-01-25 04:05:05 +00:00
|
|
|
assert!(test::compare_output(
|
|
|
|
app,
|
|
|
|
"test --input somepath --target file",
|
|
|
|
COND_REQ_IN_USAGE,
|
|
|
|
true
|
|
|
|
));
|
2017-02-03 22:43:49 +00:00
|
|
|
}
|
|
|
|
|
2016-12-29 01:35:34 +00:00
|
|
|
#[test]
|
|
|
|
fn required_if_wrong_val() {
|
|
|
|
let res = App::new("ri")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
|
|
|
Arg::with_name("cfg")
|
|
|
|
.required_if("extra", "val")
|
|
|
|
.takes_value(true)
|
|
|
|
.long("config"),
|
|
|
|
)
|
|
|
|
.arg(Arg::with_name("extra").takes_value(true).long("extra"))
|
2017-02-03 22:43:49 +00:00
|
|
|
.get_matches_from_safe(vec!["ri", "--extra", "other"]);
|
2016-12-29 01:35:34 +00:00
|
|
|
|
|
|
|
assert!(res.is_ok());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn required_ifs_val_present_pass() {
|
|
|
|
let res = App::new("ri")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
|
|
|
Arg::with_name("cfg")
|
|
|
|
.required_ifs(&[("extra", "val"), ("option", "spec")])
|
|
|
|
.takes_value(true)
|
|
|
|
.long("config"),
|
|
|
|
)
|
|
|
|
.arg(Arg::with_name("option").takes_value(true).long("option"))
|
|
|
|
.arg(Arg::with_name("extra").takes_value(true).long("extra"))
|
2017-02-03 22:43:49 +00:00
|
|
|
.get_matches_from_safe(vec!["ri", "--option", "spec", "--config", "my.cfg"]);
|
2016-12-29 01:35:34 +00:00
|
|
|
|
|
|
|
assert!(res.is_ok());
|
|
|
|
// assert_eq!(res.unwrap_err().kind, ErrorKind::MissingRequiredArgument);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn required_ifs_val_present_fail() {
|
|
|
|
let res = App::new("ri")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
|
|
|
Arg::with_name("cfg")
|
|
|
|
.required_ifs(&[("extra", "val"), ("option", "spec")])
|
|
|
|
.takes_value(true)
|
|
|
|
.long("config"),
|
|
|
|
)
|
|
|
|
.arg(Arg::with_name("extra").takes_value(true).long("extra"))
|
|
|
|
.arg(Arg::with_name("option").takes_value(true).long("option"))
|
2017-02-03 22:43:49 +00:00
|
|
|
.get_matches_from_safe(vec!["ri", "--option", "spec"]);
|
2016-12-29 01:35:34 +00:00
|
|
|
|
|
|
|
assert!(res.is_err());
|
|
|
|
assert_eq!(res.unwrap_err().kind, ErrorKind::MissingRequiredArgument);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn required_ifs_wrong_val() {
|
|
|
|
let res = App::new("ri")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
|
|
|
Arg::with_name("cfg")
|
|
|
|
.required_ifs(&[("extra", "val"), ("option", "spec")])
|
|
|
|
.takes_value(true)
|
|
|
|
.long("config"),
|
|
|
|
)
|
|
|
|
.arg(Arg::with_name("extra").takes_value(true).long("extra"))
|
|
|
|
.arg(Arg::with_name("option").takes_value(true).long("option"))
|
2017-02-03 22:43:49 +00:00
|
|
|
.get_matches_from_safe(vec!["ri", "--option", "other"]);
|
2016-12-29 01:35:34 +00:00
|
|
|
|
|
|
|
assert!(res.is_ok());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn required_ifs_wrong_val_mult_fail() {
|
|
|
|
let res = App::new("ri")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
|
|
|
Arg::with_name("cfg")
|
|
|
|
.required_ifs(&[("extra", "val"), ("option", "spec")])
|
|
|
|
.takes_value(true)
|
|
|
|
.long("config"),
|
|
|
|
)
|
|
|
|
.arg(Arg::with_name("extra").takes_value(true).long("extra"))
|
|
|
|
.arg(Arg::with_name("option").takes_value(true).long("option"))
|
2017-02-03 22:43:49 +00:00
|
|
|
.get_matches_from_safe(vec!["ri", "--extra", "other", "--option", "spec"]);
|
2016-12-29 01:35:34 +00:00
|
|
|
|
|
|
|
assert!(res.is_err());
|
|
|
|
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() {
|
2018-01-25 04:05:05 +00:00
|
|
|
let app = App::new("clap-test").version("v1.4.8").arg(
|
|
|
|
Arg::with_name("opt")
|
2017-03-17 01:45:45 +00:00
|
|
|
.long("opt")
|
|
|
|
.short("o")
|
|
|
|
.required(true)
|
|
|
|
.require_equals(true)
|
|
|
|
.value_name("FILE")
|
2018-01-25 04:05:05 +00:00
|
|
|
.help("some"),
|
|
|
|
);
|
2017-03-17 01:45:45 +00:00
|
|
|
assert!(test::compare_output(app, "clap-test", REQUIRE_EQUALS, true));
|
2018-01-25 04:05:05 +00:00
|
|
|
}
|