2020-02-04 08:10:53 +00:00
|
|
|
mod utils;
|
2015-09-28 02:48:15 +00:00
|
|
|
|
2016-01-25 20:56:37 +00:00
|
|
|
use clap::{App, Arg, ArgGroup, ErrorKind};
|
2015-09-28 02:48:15 +00:00
|
|
|
|
2019-10-02 13:27:19 +00:00
|
|
|
static REQ_GROUP_USAGE: &str = "error: The following required arguments were not provided:
|
2017-01-03 04:05:23 +00:00
|
|
|
<base|--delete>
|
|
|
|
|
|
|
|
USAGE:
|
|
|
|
clap-test <base|--delete>
|
|
|
|
|
|
|
|
For more information try --help";
|
|
|
|
|
2019-10-02 13:27:19 +00:00
|
|
|
static REQ_GROUP_CONFLICT_USAGE: &str =
|
2020-11-07 17:28:22 +00:00
|
|
|
"error: The argument '--delete' cannot be used with '<base>'
|
2020-04-23 17:56:42 +00:00
|
|
|
|
|
|
|
USAGE:
|
|
|
|
clap-test <base|--delete>
|
|
|
|
|
|
|
|
For more information try --help";
|
|
|
|
|
|
|
|
static REQ_GROUP_CONFLICT_ONLY_OPTIONS: &str =
|
2020-11-07 17:28:22 +00:00
|
|
|
"error: The argument '--delete' cannot be used with '--all'
|
2020-04-23 17:19:11 +00:00
|
|
|
|
2017-01-03 04:05:23 +00:00
|
|
|
USAGE:
|
2020-11-07 17:28:22 +00:00
|
|
|
clap-test <--all|--delete>
|
2017-01-03 04:05:23 +00:00
|
|
|
|
|
|
|
For more information try --help";
|
|
|
|
|
2015-09-28 02:48:15 +00:00
|
|
|
#[test]
|
|
|
|
fn required_group_missing_arg() {
|
2015-09-28 20:23:17 +00:00
|
|
|
let result = App::new("group")
|
2018-10-19 20:42:13 +00:00
|
|
|
.arg("-f, --flag 'some flag'")
|
|
|
|
.arg(" -c, --color 'some other flag'")
|
2020-05-14 20:50:56 +00:00
|
|
|
.group(ArgGroup::new("req").args(&["flag", "color"]).required(true))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec![""]);
|
2015-09-28 20:23:17 +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-28 02:48:15 +00:00
|
|
|
}
|
|
|
|
|
2020-04-09 14:21:45 +00:00
|
|
|
#[cfg(debug_assertions)]
|
2017-05-15 22:29:30 +00:00
|
|
|
#[test]
|
2020-04-09 22:33:16 +00:00
|
|
|
#[should_panic = "Argument group 'req' contains non-existent argument"]
|
2017-05-15 22:29:30 +00:00
|
|
|
fn non_existing_arg() {
|
|
|
|
let _ = App::new("group")
|
2018-10-19 20:42:13 +00:00
|
|
|
.arg("-f, --flag 'some flag'")
|
|
|
|
.arg("-c, --color 'some other flag'")
|
2020-05-14 20:50:56 +00:00
|
|
|
.group(ArgGroup::new("req").args(&["flg", "color"]).required(true))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec![""]);
|
2017-05-15 22:29:30 +00:00
|
|
|
}
|
|
|
|
|
2020-04-09 14:21:45 +00:00
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
#[test]
|
2020-04-09 22:33:16 +00:00
|
|
|
#[should_panic = "Argument group name must be unique\n\n\t'req' is already in use"]
|
2020-04-09 14:21:45 +00:00
|
|
|
fn unique_group_name() {
|
|
|
|
let _ = App::new("group")
|
|
|
|
.arg("-f, --flag 'some flag'")
|
|
|
|
.arg("-c, --color 'some other flag'")
|
2020-05-14 20:50:56 +00:00
|
|
|
.group(ArgGroup::new("req").args(&["flag"]).required(true))
|
|
|
|
.group(ArgGroup::new("req").args(&["color"]).required(true))
|
2020-04-09 14:21:45 +00:00
|
|
|
.try_get_matches_from(vec![""]);
|
|
|
|
}
|
|
|
|
|
2020-04-09 16:19:05 +00:00
|
|
|
#[cfg(debug_assertions)]
|
2020-04-09 14:21:45 +00:00
|
|
|
#[test]
|
2020-04-09 22:33:16 +00:00
|
|
|
#[should_panic = "Argument group name '' must not conflict with argument name"]
|
2020-05-14 20:50:56 +00:00
|
|
|
fn groups_new_of_arg_name() {
|
2020-04-09 14:21:45 +00:00
|
|
|
let _ = App::new("group")
|
2020-05-14 20:50:56 +00:00
|
|
|
.arg(Arg::new("a").long("a").group("a"))
|
2020-04-09 14:21:45 +00:00
|
|
|
.try_get_matches_from(vec!["", "--a"]);
|
|
|
|
}
|
|
|
|
|
2020-04-09 16:19:05 +00:00
|
|
|
#[cfg(debug_assertions)]
|
2020-04-09 14:21:45 +00:00
|
|
|
#[test]
|
2020-04-09 22:33:16 +00:00
|
|
|
#[should_panic = "Argument group name 'a' must not conflict with argument name"]
|
2020-05-14 20:50:56 +00:00
|
|
|
fn arg_group_new_of_arg_name() {
|
2020-04-09 14:21:45 +00:00
|
|
|
let _ = App::new("group")
|
2020-05-14 20:50:56 +00:00
|
|
|
.arg(Arg::new("a").long("a").group("a"))
|
|
|
|
.group(ArgGroup::new("a"))
|
2020-04-09 14:21:45 +00:00
|
|
|
.try_get_matches_from(vec!["", "--a"]);
|
|
|
|
}
|
|
|
|
|
2015-09-28 02:48:15 +00:00
|
|
|
#[test]
|
|
|
|
fn group_single_value() {
|
2018-01-09 15:24:24 +00:00
|
|
|
let res = App::new("group")
|
2018-10-19 20:42:13 +00:00
|
|
|
.arg("-f, --flag 'some flag'")
|
|
|
|
.arg("-c, --color [color] 'some option'")
|
2020-05-14 20:50:56 +00:00
|
|
|
.group(ArgGroup::new("grp").args(&["flag", "color"]))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "-c", "blue"]);
|
2018-01-09 15:24:24 +00:00
|
|
|
assert!(res.is_ok());
|
|
|
|
|
|
|
|
let m = res.unwrap();
|
2015-09-28 02:48:15 +00:00
|
|
|
assert!(m.is_present("grp"));
|
|
|
|
assert_eq!(m.value_of("grp").unwrap(), "blue");
|
2015-11-09 13:57:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn group_single_flag() {
|
2018-01-09 15:24:24 +00:00
|
|
|
let res = App::new("group")
|
2018-10-19 20:42:13 +00:00
|
|
|
.arg("-f, --flag 'some flag'")
|
|
|
|
.arg("-c, --color [color] 'some option'")
|
2020-05-14 20:50:56 +00:00
|
|
|
.group(ArgGroup::new("grp").args(&["flag", "color"]))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "-f"]);
|
2018-01-09 15:24:24 +00:00
|
|
|
assert!(res.is_ok());
|
|
|
|
|
|
|
|
let m = res.unwrap();
|
2015-09-28 02:48:15 +00:00
|
|
|
assert!(m.is_present("grp"));
|
|
|
|
assert!(m.value_of("grp").is_none());
|
2015-11-09 13:57:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn group_empty() {
|
2018-01-09 15:24:24 +00:00
|
|
|
let res = App::new("group")
|
2018-10-19 20:42:13 +00:00
|
|
|
.arg("-f, --flag 'some flag'")
|
|
|
|
.arg("-c, --color [color] 'some option'")
|
2020-05-14 20:50:56 +00:00
|
|
|
.group(ArgGroup::new("grp").args(&["flag", "color"]))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec![""]);
|
2018-01-09 15:24:24 +00:00
|
|
|
assert!(res.is_ok());
|
|
|
|
|
|
|
|
let m = res.unwrap();
|
2015-09-28 02:48:15 +00:00
|
|
|
assert!(!m.is_present("grp"));
|
|
|
|
assert!(m.value_of("grp").is_none());
|
|
|
|
}
|
|
|
|
|
2015-11-09 13:57:20 +00:00
|
|
|
#[test]
|
|
|
|
fn group_reqired_flags_empty() {
|
|
|
|
let result = App::new("group")
|
2018-10-19 20:42:13 +00:00
|
|
|
.arg("-f, --flag 'some flag'")
|
|
|
|
.arg("-c, --color 'some option'")
|
2020-05-14 20:50:56 +00:00
|
|
|
.group(ArgGroup::new("grp").required(true).args(&["flag", "color"]))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec![""]);
|
2015-11-09 13:57:20 +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-11-09 13:57:20 +00:00
|
|
|
}
|
|
|
|
|
2015-09-28 02:48:15 +00:00
|
|
|
#[test]
|
|
|
|
fn group_multi_value_single_arg() {
|
2018-01-09 15:24:24 +00:00
|
|
|
let res = App::new("group")
|
2018-10-19 20:42:13 +00:00
|
|
|
.arg("-f, --flag 'some flag'")
|
|
|
|
.arg("-c, --color [color]... 'some option'")
|
2020-05-14 20:50:56 +00:00
|
|
|
.group(ArgGroup::new("grp").args(&["flag", "color"]))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "-c", "blue", "red", "green"]);
|
2018-01-09 15:24:24 +00:00
|
|
|
assert!(res.is_ok(), "{:?}", res.unwrap_err().kind);
|
|
|
|
|
|
|
|
let m = res.unwrap();
|
2015-09-28 02:48:15 +00:00
|
|
|
assert!(m.is_present("grp"));
|
2018-01-25 04:05:05 +00:00
|
|
|
assert_eq!(
|
|
|
|
&*m.values_of("grp").unwrap().collect::<Vec<_>>(),
|
|
|
|
&["blue", "red", "green"]
|
|
|
|
);
|
2016-01-25 20:56:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn empty_group() {
|
2016-04-10 05:31:52 +00:00
|
|
|
let r = App::new("empty_group")
|
2018-04-21 18:59:19 +00:00
|
|
|
.arg(Arg::from("-f, --flag 'some flag'"))
|
2020-05-14 20:50:56 +00:00
|
|
|
.group(ArgGroup::new("vers").required(true))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["empty_prog"]);
|
2016-04-10 05:31:52 +00:00
|
|
|
assert!(r.is_err());
|
|
|
|
let err = r.err().unwrap();
|
|
|
|
assert_eq!(err.kind, ErrorKind::MissingRequiredArgument);
|
2015-09-28 02:48:15 +00:00
|
|
|
}
|
2016-05-09 01:33:56 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn req_group_usage_string() {
|
2016-05-09 03:20:50 +00:00
|
|
|
let app = App::new("req_group")
|
2018-10-19 20:42:13 +00:00
|
|
|
.arg("[base] 'Base commit'")
|
|
|
|
.arg("-d, --delete 'Remove the base commit information'")
|
2018-01-25 04:05:05 +00:00
|
|
|
.group(
|
2020-05-14 20:50:56 +00:00
|
|
|
ArgGroup::new("base_or_delete")
|
2018-01-25 04:05:05 +00:00
|
|
|
.args(&["base", "delete"])
|
|
|
|
.required(true),
|
|
|
|
);
|
|
|
|
|
2020-02-04 08:10:53 +00:00
|
|
|
assert!(utils::compare_output(
|
2018-01-25 04:05:05 +00:00
|
|
|
app,
|
|
|
|
"clap-test",
|
|
|
|
REQ_GROUP_USAGE,
|
|
|
|
true
|
|
|
|
));
|
2016-05-09 01:33:56 +00:00
|
|
|
}
|
2016-05-09 20:00:44 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn req_group_with_conflict_usage_string() {
|
|
|
|
let app = App::new("req_group")
|
2018-04-21 18:59:19 +00:00
|
|
|
.arg(Arg::from("[base] 'Base commit'").conflicts_with("delete"))
|
|
|
|
.arg(Arg::from(
|
2018-01-25 04:05:05 +00:00
|
|
|
"-d, --delete 'Remove the base commit information'",
|
|
|
|
))
|
|
|
|
.group(
|
2020-05-14 20:50:56 +00:00
|
|
|
ArgGroup::new("base_or_delete")
|
2018-01-25 04:05:05 +00:00
|
|
|
.args(&["base", "delete"])
|
|
|
|
.required(true),
|
|
|
|
);
|
|
|
|
|
2020-11-07 17:28:22 +00:00
|
|
|
assert!(utils::compare_output(
|
2018-01-25 04:05:05 +00:00
|
|
|
app,
|
|
|
|
"clap-test --delete base",
|
2020-04-23 17:56:42 +00:00
|
|
|
REQ_GROUP_CONFLICT_USAGE,
|
|
|
|
true
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn req_group_with_conflict_usage_string_only_options() {
|
|
|
|
let app = App::new("req_group")
|
2020-11-07 17:28:22 +00:00
|
|
|
.arg(Arg::from("<all> -a --all 'All'").conflicts_with("delete"))
|
2020-04-23 17:56:42 +00:00
|
|
|
.arg(Arg::from(
|
|
|
|
"<delete> -d, --delete 'Remove the base commit information'",
|
|
|
|
))
|
|
|
|
.group(
|
2020-05-14 20:50:56 +00:00
|
|
|
ArgGroup::new("all_or_delete")
|
2020-04-23 17:56:42 +00:00
|
|
|
.args(&["all", "delete"])
|
|
|
|
.required(true),
|
|
|
|
);
|
2020-11-07 17:28:22 +00:00
|
|
|
assert!(utils::compare_output(
|
2020-04-23 17:56:42 +00:00
|
|
|
app,
|
|
|
|
"clap-test --delete --all",
|
|
|
|
REQ_GROUP_CONFLICT_ONLY_OPTIONS,
|
2018-01-25 04:05:05 +00:00
|
|
|
true
|
|
|
|
));
|
2016-05-09 20:00:44 +00:00
|
|
|
}
|
2016-06-23 14:06:19 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn required_group_multiple_args() {
|
|
|
|
let result = App::new("group")
|
2018-10-19 20:42:13 +00:00
|
|
|
.arg("-f, --flag 'some flag'")
|
|
|
|
.arg("-c, --color 'some other flag'")
|
2018-01-25 04:05:05 +00:00
|
|
|
.group(
|
2020-05-14 20:50:56 +00:00
|
|
|
ArgGroup::new("req")
|
2018-01-25 04:05:05 +00:00
|
|
|
.args(&["flag", "color"])
|
|
|
|
.required(true)
|
|
|
|
.multiple(true),
|
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["group", "-f", "-c"]);
|
2016-06-23 14:06:19 +00:00
|
|
|
assert!(result.is_ok());
|
|
|
|
let m = result.unwrap();
|
|
|
|
assert!(m.is_present("flag"));
|
|
|
|
assert!(m.is_present("color"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn group_multiple_args_error() {
|
|
|
|
let result = App::new("group")
|
2018-10-19 20:42:13 +00:00
|
|
|
.arg("-f, --flag 'some flag'")
|
|
|
|
.arg("-c, --color 'some other flag'")
|
2020-05-14 20:50:56 +00:00
|
|
|
.group(ArgGroup::new("req").args(&["flag", "color"]))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["group", "-f", "-c"]);
|
2016-06-23 14:06:19 +00:00
|
|
|
assert!(result.is_err());
|
|
|
|
let err = result.unwrap_err();
|
|
|
|
assert_eq!(err.kind, ErrorKind::ArgumentConflict);
|
2016-06-24 04:17:04 +00:00
|
|
|
}
|
2019-04-05 02:06:23 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn group_acts_like_arg() {
|
|
|
|
let m = App::new("prog")
|
2020-05-14 20:50:56 +00:00
|
|
|
.arg(Arg::new("debug").long("debug").group("mode"))
|
|
|
|
.arg(Arg::new("verbose").long("verbose").group("mode"))
|
2019-04-05 19:51:22 +00:00
|
|
|
.get_matches_from(vec!["prog", "--debug"]);
|
2019-04-05 02:06:23 +00:00
|
|
|
assert!(m.is_present("mode"));
|
2019-04-05 19:51:22 +00:00
|
|
|
}
|
2020-04-23 17:19:11 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn issue_1794() {
|
|
|
|
let app = clap::App::new("hello")
|
|
|
|
.bin_name("deno")
|
2020-05-14 20:50:56 +00:00
|
|
|
.arg(Arg::new("option1").long("option1").takes_value(false))
|
|
|
|
.arg(Arg::new("pos1").takes_value(true))
|
|
|
|
.arg(Arg::new("pos2").takes_value(true))
|
2020-04-23 17:19:11 +00:00
|
|
|
.group(
|
2020-05-14 20:50:56 +00:00
|
|
|
ArgGroup::new("arg1")
|
2020-04-23 17:19:11 +00:00
|
|
|
.args(&["pos1", "option1"])
|
|
|
|
.required(true),
|
|
|
|
);
|
|
|
|
|
|
|
|
let m = app.clone().get_matches_from(&["app", "pos1", "pos2"]);
|
|
|
|
assert_eq!(m.value_of("pos1"), Some("pos1"));
|
|
|
|
assert_eq!(m.value_of("pos2"), Some("pos2"));
|
|
|
|
assert!(!m.is_present("option1"));
|
|
|
|
|
|
|
|
let m = app
|
|
|
|
.clone()
|
|
|
|
.get_matches_from(&["app", "--option1", "positional"]);
|
|
|
|
assert_eq!(m.value_of("pos1"), None);
|
|
|
|
assert_eq!(m.value_of("pos2"), Some("positional"));
|
|
|
|
assert!(m.is_present("option1"));
|
|
|
|
}
|