2015-08-27 21:03:45 +00:00
|
|
|
extern crate clap;
|
|
|
|
|
2016-01-21 05:18:53 +00:00
|
|
|
use clap::{App, Arg, ErrorKind};
|
2015-08-27 21:03:45 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn posix_compatible_flags_long() {
|
|
|
|
let m = App::new("posix")
|
2016-01-26 15:14:27 +00:00
|
|
|
.arg(Arg::from_usage("--flag 'some flag'").overrides_with("color"))
|
2015-08-27 21:03:45 +00:00
|
|
|
.arg(Arg::from_usage("--color 'some other flag'"))
|
2016-01-21 06:48:30 +00:00
|
|
|
.get_matches_from(vec!["", "--flag", "--color"]);
|
2015-08-27 21:03:45 +00:00
|
|
|
assert!(m.is_present("color"));
|
|
|
|
assert!(!m.is_present("flag"));
|
|
|
|
|
|
|
|
let m = App::new("posix")
|
2016-01-26 15:14:27 +00:00
|
|
|
.arg(Arg::from_usage("--flag 'some flag'").overrides_with("color"))
|
2015-08-27 21:03:45 +00:00
|
|
|
.arg(Arg::from_usage("--color 'some other flag'"))
|
2016-01-25 20:56:37 +00:00
|
|
|
.get_matches_from(vec!["", "--color", "--flag"]);
|
2015-08-27 21:03:45 +00:00
|
|
|
assert!(!m.is_present("color"));
|
|
|
|
assert!(m.is_present("flag"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn posix_compatible_flags_short() {
|
|
|
|
let m = App::new("posix")
|
2016-01-26 15:14:27 +00:00
|
|
|
.arg(Arg::from_usage("-f, --flag 'some flag'").overrides_with("color"))
|
2015-08-27 21:03:45 +00:00
|
|
|
.arg(Arg::from_usage("-c, --color 'some other flag'"))
|
2016-01-25 20:56:37 +00:00
|
|
|
.get_matches_from(vec!["", "-f", "-c"]);
|
2015-08-27 21:03:45 +00:00
|
|
|
assert!(m.is_present("color"));
|
|
|
|
assert!(!m.is_present("flag"));
|
|
|
|
|
|
|
|
let m = App::new("posix")
|
2016-01-26 15:14:27 +00:00
|
|
|
.arg(Arg::from_usage("-f, --flag 'some flag'").overrides_with("color"))
|
2015-08-27 21:03:45 +00:00
|
|
|
.arg(Arg::from_usage("-c, --color 'some other flag'"))
|
2016-01-25 20:56:37 +00:00
|
|
|
.get_matches_from(vec!["", "-c", "-f"]);
|
2015-08-27 21:03:45 +00:00
|
|
|
assert!(!m.is_present("color"));
|
|
|
|
assert!(m.is_present("flag"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn posix_compatible_opts_long() {
|
|
|
|
let m = App::new("posix")
|
2016-01-26 15:14:27 +00:00
|
|
|
.arg(Arg::from_usage("--flag [flag] 'some flag'").overrides_with("color"))
|
2015-08-27 21:03:45 +00:00
|
|
|
.arg(Arg::from_usage("--color [color] 'some other flag'"))
|
2016-01-25 20:56:37 +00:00
|
|
|
.get_matches_from(vec!["", "--flag", "some" ,"--color", "other"]);
|
2015-08-27 21:03:45 +00:00
|
|
|
assert!(m.is_present("color"));
|
|
|
|
assert_eq!(m.value_of("color").unwrap(), "other");
|
|
|
|
assert!(!m.is_present("flag"));
|
|
|
|
|
|
|
|
let m = App::new("posix")
|
2016-01-26 15:14:27 +00:00
|
|
|
.arg(Arg::from_usage("--flag [flag] 'some flag'").overrides_with("color"))
|
2015-08-27 21:03:45 +00:00
|
|
|
.arg(Arg::from_usage("--color [color] 'some other flag'"))
|
2016-01-25 20:56:37 +00:00
|
|
|
.get_matches_from(vec!["", "--color", "some" ,"--flag", "other"]);
|
2015-08-27 21:03:45 +00:00
|
|
|
assert!(!m.is_present("color"));
|
|
|
|
assert!(m.is_present("flag"));
|
|
|
|
assert_eq!(m.value_of("flag").unwrap(), "other");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn posix_compatible_opts_long_equals() {
|
|
|
|
let m = App::new("posix")
|
2016-01-26 15:14:27 +00:00
|
|
|
.arg(Arg::from_usage("--flag [flag] 'some flag'").overrides_with("color"))
|
2015-08-27 21:03:45 +00:00
|
|
|
.arg(Arg::from_usage("--color [color] 'some other flag'"))
|
2016-01-25 20:56:37 +00:00
|
|
|
.get_matches_from(vec!["", "--flag=some" ,"--color=other"]);
|
2015-08-27 21:03:45 +00:00
|
|
|
assert!(m.is_present("color"));
|
|
|
|
assert_eq!(m.value_of("color").unwrap(), "other");
|
|
|
|
assert!(!m.is_present("flag"));
|
|
|
|
|
|
|
|
let m = App::new("posix")
|
2016-01-26 15:14:27 +00:00
|
|
|
.arg(Arg::from_usage("--flag [flag] 'some flag'").overrides_with("color"))
|
2015-08-27 21:03:45 +00:00
|
|
|
.arg(Arg::from_usage("--color [color] 'some other flag'"))
|
2016-01-25 20:56:37 +00:00
|
|
|
.get_matches_from(vec!["", "--color=some" ,"--flag=other"]);
|
2015-08-27 21:03:45 +00:00
|
|
|
assert!(!m.is_present("color"));
|
|
|
|
assert!(m.is_present("flag"));
|
|
|
|
assert_eq!(m.value_of("flag").unwrap(), "other");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn posix_compatible_opts_short() {
|
|
|
|
let m = App::new("posix")
|
2016-01-26 15:14:27 +00:00
|
|
|
.arg(Arg::from_usage("-f [flag] 'some flag'").overrides_with("c"))
|
2015-08-27 21:03:45 +00:00
|
|
|
.arg(Arg::from_usage("-c [color] 'some other flag'"))
|
2016-01-25 20:56:37 +00:00
|
|
|
.get_matches_from(vec!["", "-f", "some", "-c", "other"]);
|
|
|
|
assert!(m.is_present("c"));
|
|
|
|
assert_eq!(m.value_of("c").unwrap(), "other");
|
|
|
|
assert!(!m.is_present("f"));
|
2015-08-27 21:03:45 +00:00
|
|
|
|
|
|
|
let m = App::new("posix")
|
2016-01-26 15:14:27 +00:00
|
|
|
.arg(Arg::from_usage("-f [flag] 'some flag'").overrides_with("c"))
|
2015-08-27 21:03:45 +00:00
|
|
|
.arg(Arg::from_usage("-c [color] 'some other flag'"))
|
2016-01-25 20:56:37 +00:00
|
|
|
.get_matches_from(vec!["", "-c", "some", "-f", "other"]);
|
|
|
|
assert!(!m.is_present("c"));
|
|
|
|
assert!(m.is_present("f"));
|
|
|
|
assert_eq!(m.value_of("f").unwrap(), "other");
|
2015-09-06 19:22:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn conflict_overriden() {
|
|
|
|
let m = App::new("conflict_overriden")
|
|
|
|
.arg(Arg::from_usage("-f, --flag 'some flag'")
|
|
|
|
.conflicts_with("debug"))
|
|
|
|
.arg(Arg::from_usage("-d, --debug 'other flag'"))
|
|
|
|
.arg(Arg::from_usage("-c, --color 'third flag'")
|
2016-01-26 15:14:27 +00:00
|
|
|
.overrides_with("flag"))
|
2016-01-25 20:56:37 +00:00
|
|
|
.get_matches_from(vec!["", "-f", "-c", "-d"]);
|
2015-09-06 19:22:37 +00:00
|
|
|
assert!(m.is_present("color"));
|
|
|
|
assert!(!m.is_present("flag"));
|
|
|
|
assert!(m.is_present("debug"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn conflict_overriden_2() {
|
|
|
|
let result = App::new("conflict_overriden")
|
|
|
|
.arg(Arg::from_usage("-f, --flag 'some flag'")
|
|
|
|
.conflicts_with("debug"))
|
|
|
|
.arg(Arg::from_usage("-d, --debug 'other flag'"))
|
|
|
|
.arg(Arg::from_usage("-c, --color 'third flag'")
|
2016-01-26 15:14:27 +00:00
|
|
|
.overrides_with("flag"))
|
2016-01-25 20:56:37 +00:00
|
|
|
.get_matches_from_safe(vec!["", "-f", "-d", "-c"]);
|
2016-01-22 17:58:56 +00:00
|
|
|
assert!(result.is_ok());
|
|
|
|
let m = result.unwrap();
|
|
|
|
assert!(m.is_present("color"));
|
|
|
|
assert!(m.is_present("debug"));
|
|
|
|
assert!(!m.is_present("flag"));
|
2015-09-06 19:22:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn conflict_overriden_3() {
|
|
|
|
let result = App::new("conflict_overriden")
|
|
|
|
.arg(Arg::from_usage("-f, --flag 'some flag'")
|
|
|
|
.conflicts_with("debug"))
|
|
|
|
.arg(Arg::from_usage("-d, --debug 'other flag'"))
|
|
|
|
.arg(Arg::from_usage("-c, --color 'third flag'")
|
2016-01-26 15:14:27 +00:00
|
|
|
.overrides_with("flag"))
|
2016-01-25 20:56:37 +00:00
|
|
|
.get_matches_from_safe(vec!["", "-d", "-c", "-f"]);
|
2015-09-06 19:22:37 +00:00
|
|
|
assert!(result.is_err());
|
|
|
|
let err = result.err().unwrap();
|
2016-01-21 05:18:53 +00:00
|
|
|
assert_eq!(err.kind, ErrorKind::ArgumentConflict);
|
2015-09-06 19:22:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn conflict_overriden_4() {
|
|
|
|
let m = App::new("conflict_overriden")
|
|
|
|
.arg(Arg::from_usage("-f, --flag 'some flag'")
|
|
|
|
.conflicts_with("debug"))
|
|
|
|
.arg(Arg::from_usage("-d, --debug 'other flag'"))
|
|
|
|
.arg(Arg::from_usage("-c, --color 'third flag'")
|
2016-01-26 15:14:27 +00:00
|
|
|
.overrides_with("flag"))
|
2016-01-25 20:56:37 +00:00
|
|
|
.get_matches_from(vec!["", "-d", "-f", "-c"]);
|
2015-09-06 19:22:37 +00:00
|
|
|
assert!(m.is_present("color"));
|
|
|
|
assert!(!m.is_present("flag"));
|
|
|
|
assert!(m.is_present("debug"));
|
2015-09-06 19:34:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2015-11-09 13:57:20 +00:00
|
|
|
fn pos_required_overridden_by_flag() {
|
2015-09-06 19:34:37 +00:00
|
|
|
let result = App::new("require_overriden")
|
2015-11-09 13:57:20 +00:00
|
|
|
.arg(Arg::with_name("pos")
|
2015-09-06 19:34:37 +00:00
|
|
|
.index(1)
|
|
|
|
.required(true))
|
2015-11-09 13:57:20 +00:00
|
|
|
.arg(Arg::from_usage("-c, --color 'some flag'")
|
2016-01-26 15:14:27 +00:00
|
|
|
.overrides_with("pos"))
|
2016-01-25 20:56:37 +00:00
|
|
|
.get_matches_from_safe(vec!["", "test", "-c"]);
|
2015-11-09 13:57:20 +00:00
|
|
|
assert!(result.is_ok(), "{:?}", result.unwrap_err());
|
2015-09-06 19:34:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn require_overriden_2() {
|
|
|
|
let m = App::new("require_overriden")
|
2018-01-09 15:24:24 +00:00
|
|
|
.arg(Arg::with_name("req_pos")
|
2015-09-06 19:34:37 +00:00
|
|
|
.required(true))
|
|
|
|
.arg(Arg::from_usage("-c, --color 'other flag'")
|
2018-01-09 15:24:24 +00:00
|
|
|
.overrides_with("req_pos"))
|
|
|
|
.get_matches_from(vec!["", "-c", "req_pos"]);
|
2015-09-06 19:34:37 +00:00
|
|
|
assert!(!m.is_present("color"));
|
2018-01-09 15:24:24 +00:00
|
|
|
assert!(m.is_present("req_pos"));
|
2015-09-06 19:34:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn require_overriden_3() {
|
|
|
|
let m = App::new("require_overriden")
|
|
|
|
.arg(Arg::from_usage("-f, --flag 'some flag'")
|
|
|
|
.requires("debug"))
|
|
|
|
.arg(Arg::from_usage("-d, --debug 'other flag'"))
|
|
|
|
.arg(Arg::from_usage("-c, --color 'third flag'")
|
2016-01-26 15:14:27 +00:00
|
|
|
.overrides_with("flag"))
|
2016-01-25 20:56:37 +00:00
|
|
|
.get_matches_from(vec!["", "-f", "-c"]);
|
2015-09-06 19:34:37 +00:00
|
|
|
assert!(m.is_present("color"));
|
|
|
|
assert!(!m.is_present("flag"));
|
|
|
|
assert!(!m.is_present("debug"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn require_overriden_4() {
|
|
|
|
let result = App::new("require_overriden")
|
|
|
|
.arg(Arg::from_usage("-f, --flag 'some flag'")
|
|
|
|
.requires("debug"))
|
|
|
|
.arg(Arg::from_usage("-d, --debug 'other flag'"))
|
|
|
|
.arg(Arg::from_usage("-c, --color 'third flag'")
|
2016-01-26 15:14:27 +00:00
|
|
|
.overrides_with("flag"))
|
2016-01-25 20:56:37 +00:00
|
|
|
.get_matches_from_safe(vec!["", "-c", "-f"]);
|
2015-09-06 19:34:37 +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
|
|
|
}
|