2015-09-28 02:48:15 +00:00
|
|
|
extern crate clap;
|
2016-05-10 02:46:09 +00:00
|
|
|
extern crate regex;
|
|
|
|
|
|
|
|
include!("../clap-test.rs");
|
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
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn required_group_missing_arg() {
|
2015-09-28 20:23:17 +00:00
|
|
|
let result = App::new("group")
|
2015-09-28 02:48:15 +00:00
|
|
|
.args_from_usage("-f, --flag 'some flag'
|
|
|
|
-c, --color 'some other flag'")
|
2016-01-21 05:18:53 +00:00
|
|
|
.group(ArgGroup::with_name("req")
|
|
|
|
.args(&["flag", "color"])
|
2015-09-28 02:48:15 +00:00
|
|
|
.required(true))
|
|
|
|
.get_matches_from_safe(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
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn group_single_value() {
|
2016-01-25 20:56:37 +00:00
|
|
|
let m = App::new("group")
|
2015-09-28 02:48:15 +00:00
|
|
|
.args_from_usage("-f, --flag 'some flag'
|
|
|
|
-c, --color [color] 'some option'")
|
2016-01-21 05:18:53 +00:00
|
|
|
.group(ArgGroup::with_name("grp")
|
|
|
|
.args(&["flag", "color"]))
|
2016-01-25 20:56:37 +00:00
|
|
|
.get_matches_from(vec!["", "-c", "blue"]);
|
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() {
|
2015-09-28 02:48:15 +00:00
|
|
|
let m = App::new("group")
|
|
|
|
.args_from_usage("-f, --flag 'some flag'
|
|
|
|
-c, --color [color] 'some option'")
|
2016-01-21 05:18:53 +00:00
|
|
|
.group(ArgGroup::with_name("grp")
|
|
|
|
.args(&["flag", "color"]))
|
2016-01-25 20:56:37 +00:00
|
|
|
.get_matches_from(vec!["", "-f"]);
|
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() {
|
2015-09-28 02:48:15 +00:00
|
|
|
let m = App::new("group")
|
|
|
|
.args_from_usage("-f, --flag 'some flag'
|
|
|
|
-c, --color [color] 'some option'")
|
2016-01-21 05:18:53 +00:00
|
|
|
.group(ArgGroup::with_name("grp")
|
|
|
|
.args(&["flag", "color"]))
|
2015-09-28 02:48:15 +00:00
|
|
|
.get_matches_from(vec![""]);
|
|
|
|
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")
|
|
|
|
.args_from_usage("-f, --flag 'some flag'
|
|
|
|
-c, --color 'some option'")
|
2016-01-21 05:18:53 +00:00
|
|
|
.group(ArgGroup::with_name("grp")
|
2015-11-09 13:57:20 +00:00
|
|
|
.required(true)
|
2016-01-21 05:18:53 +00:00
|
|
|
.args(&["flag", "color"]))
|
2015-11-09 13:57:20 +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-11-09 13:57:20 +00:00
|
|
|
}
|
|
|
|
|
2015-09-28 02:48:15 +00:00
|
|
|
#[test]
|
|
|
|
fn group_multi_value_single_arg() {
|
2016-01-25 20:56:37 +00:00
|
|
|
let m = App::new("group")
|
2015-09-28 02:48:15 +00:00
|
|
|
.args_from_usage("-f, --flag 'some flag'
|
|
|
|
-c, --color [color]... 'some option'")
|
2016-01-21 05:18:53 +00:00
|
|
|
.group(ArgGroup::with_name("grp")
|
|
|
|
.args(&["flag", "color"]))
|
2016-01-25 20:56:37 +00:00
|
|
|
.get_matches_from(vec!["", "-c", "blue", "red", "green"]);
|
2015-09-28 02:48:15 +00:00
|
|
|
assert!(m.is_present("grp"));
|
2016-01-25 20:56:37 +00:00
|
|
|
assert_eq!(&*m.values_of("grp").unwrap().collect::<Vec<_>>(), &["blue", "red", "green"]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn empty_group() {
|
2016-04-10 05:31:52 +00:00
|
|
|
let r = App::new("empty_group")
|
2016-01-25 20:56:37 +00:00
|
|
|
.arg(Arg::from_usage("-f, --flag 'some flag'"))
|
|
|
|
.group(ArgGroup::with_name("vers")
|
|
|
|
.required(true))
|
2016-04-10 05:31:52 +00:00
|
|
|
.get_matches_from_safe(vec!["empty_prog"]);
|
|
|
|
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")
|
2016-05-09 01:33:56 +00:00
|
|
|
.args_from_usage("[base] 'Base commit'
|
|
|
|
-d, --delete 'Remove the base commit information'")
|
|
|
|
.group(ArgGroup::with_name("base_or_delete")
|
|
|
|
.args(&["base", "delete"])
|
2016-05-09 03:20:50 +00:00
|
|
|
.required(true));
|
2016-05-09 01:33:56 +00:00
|
|
|
|
2016-05-10 02:46:09 +00:00
|
|
|
test::check_err_output(app, "clap-test",
|
2016-05-09 01:33:56 +00:00
|
|
|
"error: The following required arguments were not provided:
|
|
|
|
<base|--delete>
|
|
|
|
|
|
|
|
USAGE:
|
2016-05-09 03:20:50 +00:00
|
|
|
clap-test [FLAGS] <base|--delete> [ARGS]
|
|
|
|
|
|
|
|
For more information try --help", 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")
|
|
|
|
.arg(Arg::from_usage("[base] 'Base commit'").conflicts_with("delete"))
|
|
|
|
.arg(Arg::from_usage("-d, --delete 'Remove the base commit information'"))
|
|
|
|
.group(ArgGroup::with_name("base_or_delete")
|
|
|
|
.args(&["base", "delete"])
|
|
|
|
.required(true));
|
|
|
|
|
2016-05-10 02:46:09 +00:00
|
|
|
test::check_err_output(app, "clap-test --delete base",
|
2016-05-09 20:00:44 +00:00
|
|
|
"error: The argument '--delete' cannot be used with 'base'
|
|
|
|
|
|
|
|
USAGE:
|
|
|
|
clap-test <base|--delete>
|
|
|
|
|
|
|
|
For more information try --help", true);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn req_group_with_conflict_rev_usage_string() {
|
|
|
|
let app = App::new("req_group")
|
|
|
|
.arg(Arg::from_usage("[base] 'Base commit'").conflicts_with("delete"))
|
|
|
|
.arg(Arg::from_usage("-d, --delete 'Remove the base commit information'"))
|
|
|
|
.group(ArgGroup::with_name("base_or_delete")
|
|
|
|
.args(&["base", "delete"])
|
|
|
|
.required(true));
|
|
|
|
|
2016-05-10 02:46:09 +00:00
|
|
|
test::check_err_output(app, "clap-test --delete base",
|
2016-05-09 20:00:44 +00:00
|
|
|
"error: The argument '--delete' cannot be used with 'base'
|
|
|
|
|
|
|
|
USAGE:
|
|
|
|
clap-test <base|--delete>
|
|
|
|
|
|
|
|
For more information try --help", true);
|
|
|
|
|
|
|
|
}
|