2015-09-28 02:48:15 +00:00
|
|
|
extern crate clap;
|
|
|
|
|
2015-09-28 20:23:17 +00:00
|
|
|
use clap::{App, ArgGroup, ClapErrorType};
|
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'")
|
|
|
|
.arg_group(ArgGroup::with_name("req")
|
|
|
|
.add_all(&["flag", "color"])
|
|
|
|
.required(true))
|
|
|
|
.get_matches_from_safe(vec![""]);
|
2015-09-28 20:23:17 +00:00
|
|
|
assert!(result.is_err());
|
|
|
|
let err = result.err().unwrap();
|
|
|
|
assert_eq!(err.error_type, ClapErrorType::MissingRequiredArgument);
|
2015-09-28 02:48:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn group_single_value() {
|
|
|
|
let m = App::new("group")
|
|
|
|
.args_from_usage("-f, --flag 'some flag'
|
|
|
|
-c, --color [color] 'some option'")
|
|
|
|
.arg_group(ArgGroup::with_name("grp")
|
|
|
|
.add_all(&["flag", "color"]))
|
|
|
|
.get_matches_from(vec!["", "-c", "blue"]);
|
|
|
|
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'")
|
|
|
|
.arg_group(ArgGroup::with_name("grp")
|
|
|
|
.add_all(&["flag", "color"]))
|
|
|
|
.get_matches_from(vec!["", "-f"]);
|
|
|
|
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'")
|
|
|
|
.arg_group(ArgGroup::with_name("grp")
|
|
|
|
.add_all(&["flag", "color"]))
|
|
|
|
.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'")
|
|
|
|
.arg_group(ArgGroup::with_name("grp")
|
|
|
|
.required(true)
|
|
|
|
.add_all(&["flag", "color"]))
|
|
|
|
.get_matches_from_safe(vec![""]);
|
|
|
|
assert!(result.is_err());
|
|
|
|
let err = result.err().unwrap();
|
|
|
|
assert_eq!(err.error_type, ClapErrorType::MissingRequiredArgument);
|
|
|
|
}
|
|
|
|
|
2015-09-28 02:48:15 +00:00
|
|
|
#[test]
|
|
|
|
fn group_multi_value_single_arg() {
|
|
|
|
let m = App::new("group")
|
|
|
|
.args_from_usage("-f, --flag 'some flag'
|
|
|
|
-c, --color [color]... 'some option'")
|
|
|
|
.arg_group(ArgGroup::with_name("grp")
|
|
|
|
.add_all(&["flag", "color"]))
|
|
|
|
.get_matches_from(vec!["", "-c", "blue", "red", "green"]);
|
|
|
|
assert!(m.is_present("grp"));
|
|
|
|
assert_eq!(m.values_of("grp").unwrap(), &["blue", "red", "green"]);
|
|
|
|
}
|