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
|
|
|
|
2016-01-21 05:18:53 +00:00
|
|
|
use clap::{App, Arg, ErrorKind, ArgGroup};
|
2015-09-06 20:08:13 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn flag_required() {
|
|
|
|
let result = App::new("flag_required")
|
|
|
|
.arg(Arg::from_usage("-f, --flag 'some flag'")
|
|
|
|
.requires("color"))
|
|
|
|
.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")
|
|
|
|
.arg(Arg::from_usage("-f, --flag 'some flag'")
|
|
|
|
.requires("color"))
|
|
|
|
.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")
|
|
|
|
.arg(Arg::from_usage("-f [flag] 'some flag'")
|
|
|
|
.requires("color"))
|
|
|
|
.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")
|
|
|
|
.arg(Arg::from_usage("-f [flag] 'some flag'")
|
2016-01-25 20:56:37 +00:00
|
|
|
.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")
|
|
|
|
.arg(Arg::with_name("flag")
|
|
|
|
.index(1)
|
|
|
|
.required(true))
|
|
|
|
.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")
|
|
|
|
.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'"))
|
2016-01-21 05:18:53 +00:00
|
|
|
.group(ArgGroup::with_name("gr")
|
2015-09-06 20:08:13 +00:00
|
|
|
.required(true)
|
2016-01-21 05:18:53 +00:00
|
|
|
.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'"))
|
2016-01-21 05:18:53 +00:00
|
|
|
.group(ArgGroup::with_name("gr")
|
2015-09-06 20:08:13 +00:00
|
|
|
.required(true)
|
2016-01-21 05:18:53 +00:00
|
|
|
.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'"))
|
2016-01-21 05:18:53 +00:00
|
|
|
.group(ArgGroup::with_name("gr")
|
2015-09-06 20:08:13 +00:00
|
|
|
.required(true)
|
2016-01-21 05:18:53 +00:00
|
|
|
.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")
|
|
|
|
.arg(Arg::from_usage("-f, --flag 'some flag'")
|
|
|
|
.requires("gr"))
|
2016-01-21 05:18:53 +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")
|
|
|
|
.arg(Arg::from_usage("-f, --flag 'some flag'")
|
|
|
|
.requires("gr"))
|
2016-01-21 05:18:53 +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")
|
|
|
|
.arg(Arg::from_usage("-f, --flag 'some flag'")
|
|
|
|
.requires("gr"))
|
2016-01-21 05:18:53 +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")
|
|
|
|
.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"))
|
|
|
|
.get_matches_from_safe(vec!["test", "--list"]);
|
|
|
|
assert!(m.is_ok());
|
|
|
|
}
|
|
|
|
|
2016-05-02 18:12:57 +00:00
|
|
|
#[test]
|
|
|
|
fn required_unless() {
|
|
|
|
let res = App::new("unlesstest")
|
|
|
|
.arg(Arg::with_name("cfg")
|
|
|
|
.required_unless("dbg")
|
|
|
|
.takes_value(true)
|
|
|
|
.long("config"))
|
|
|
|
.arg(Arg::with_name("dbg")
|
|
|
|
.long("debug"))
|
|
|
|
.get_matches_from_safe(vec![
|
|
|
|
"unlesstest", "--debug"
|
|
|
|
]);
|
|
|
|
|
|
|
|
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")
|
|
|
|
.arg(Arg::with_name("cfg")
|
|
|
|
.required_unless("dbg")
|
|
|
|
.takes_value(true)
|
|
|
|
.long("config"))
|
|
|
|
.arg(Arg::with_name("dbg")
|
|
|
|
.long("debug"))
|
|
|
|
.get_matches_from_safe(vec![
|
|
|
|
"unlesstest"
|
|
|
|
]);
|
|
|
|
|
|
|
|
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")
|
|
|
|
.arg(Arg::with_name("cfg")
|
|
|
|
.required_unless_all(&["dbg", "infile"])
|
|
|
|
.takes_value(true)
|
|
|
|
.long("config"))
|
|
|
|
.arg(Arg::with_name("dbg")
|
|
|
|
.long("debug"))
|
|
|
|
.arg(Arg::with_name("infile")
|
|
|
|
.short("i")
|
|
|
|
.takes_value(true))
|
|
|
|
.get_matches_from_safe(vec![
|
|
|
|
"unlessall", "--debug", "-i", "file"
|
|
|
|
]);
|
|
|
|
|
|
|
|
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")
|
|
|
|
.arg(Arg::with_name("cfg")
|
|
|
|
.required_unless_all(&["dbg", "infile"])
|
|
|
|
.takes_value(true)
|
|
|
|
.long("config"))
|
|
|
|
.arg(Arg::with_name("dbg")
|
|
|
|
.long("debug"))
|
|
|
|
.arg(Arg::with_name("infile")
|
|
|
|
.short("i")
|
|
|
|
.takes_value(true))
|
|
|
|
.get_matches_from_safe(vec![
|
|
|
|
"unlessall", "--debug"
|
|
|
|
]);
|
|
|
|
|
|
|
|
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")
|
|
|
|
.arg(Arg::with_name("cfg")
|
|
|
|
.required_unless_one(&["dbg", "infile"])
|
|
|
|
.takes_value(true)
|
|
|
|
.long("config"))
|
|
|
|
.arg(Arg::with_name("dbg")
|
|
|
|
.long("debug"))
|
|
|
|
.arg(Arg::with_name("infile")
|
|
|
|
.short("i")
|
|
|
|
.takes_value(true))
|
|
|
|
.get_matches_from_safe(vec![
|
|
|
|
"unlessone", "--debug"
|
|
|
|
]);
|
|
|
|
|
|
|
|
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")
|
|
|
|
.arg(Arg::with_name("cfg")
|
|
|
|
.required_unless_one(&["dbg", "infile"])
|
|
|
|
.takes_value(true)
|
|
|
|
.long("config"))
|
|
|
|
.arg(Arg::with_name("dbg")
|
|
|
|
.long("debug"))
|
|
|
|
.arg(Arg::with_name("infile")
|
|
|
|
.short("i")
|
|
|
|
.takes_value(true))
|
|
|
|
.get_matches_from_safe(vec![
|
|
|
|
"unlessone", "-i", "file"
|
|
|
|
]);
|
|
|
|
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let m = res.unwrap();
|
|
|
|
assert!(m.is_present("infile"));
|
|
|
|
assert!(!m.is_present("cfg"));
|
|
|
|
}
|
|
|
|
|
2016-07-23 21:22:14 +00:00
|
|
|
#[test]
|
|
|
|
fn required_unless_one_1() {
|
|
|
|
let res = App::new("unlessone")
|
|
|
|
.arg(Arg::with_name("cfg")
|
|
|
|
.required_unless_one(&["dbg", "infile"])
|
|
|
|
.takes_value(true)
|
|
|
|
.long("config"))
|
|
|
|
.arg(Arg::with_name("dbg")
|
|
|
|
.long("debug"))
|
|
|
|
.arg(Arg::with_name("infile")
|
|
|
|
.short("i")
|
|
|
|
.takes_value(true))
|
|
|
|
.get_matches_from_safe(vec![
|
|
|
|
"unlessone", "--debug"
|
|
|
|
]);
|
|
|
|
|
|
|
|
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")
|
|
|
|
.arg(Arg::with_name("cfg")
|
|
|
|
.required_unless_one(&["dbg", "infile"])
|
|
|
|
.takes_value(true)
|
|
|
|
.long("config"))
|
|
|
|
.arg(Arg::with_name("dbg")
|
|
|
|
.long("debug"))
|
|
|
|
.arg(Arg::with_name("infile")
|
|
|
|
.short("i")
|
|
|
|
.takes_value(true))
|
|
|
|
.get_matches_from_safe(vec![
|
|
|
|
"unlessone"
|
|
|
|
]);
|
|
|
|
|
|
|
|
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() {
|
2016-05-10 02:46:09 +00:00
|
|
|
test::check_err_output(test::complex_app(), "clap-test -F",
|
2016-05-09 03:20:50 +00:00
|
|
|
"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", true)
|
|
|
|
}
|