2015-09-06 10:46:58 +00:00
|
|
|
extern crate clap;
|
2016-05-10 02:46:09 +00:00
|
|
|
extern crate regex;
|
|
|
|
|
|
|
|
include!("../clap-test.rs");
|
2015-09-06 10:46:58 +00:00
|
|
|
|
2016-01-21 05:18:53 +00:00
|
|
|
use clap::{App, Arg, ErrorKind, ArgGroup};
|
2015-09-06 10:46:58 +00:00
|
|
|
|
2016-10-31 04:35:13 +00:00
|
|
|
static CONFLICT_ERR: &'static str = "error: The argument '-F' cannot be used with '--flag'
|
2016-05-09 03:20:50 +00:00
|
|
|
|
|
|
|
USAGE:
|
2016-10-31 04:35:13 +00:00
|
|
|
clap-test <positional> <positional2> --flag --long-option-2 <option2>
|
2016-05-09 03:20:50 +00:00
|
|
|
|
|
|
|
For more information try --help";
|
|
|
|
|
|
|
|
static CONFLICT_ERR_REV: &'static str = "error: The argument '--flag' cannot be used with '-F'
|
|
|
|
|
|
|
|
USAGE:
|
|
|
|
clap-test <positional> <positional2> -F --long-option-2 <option2>
|
|
|
|
|
|
|
|
For more information try --help";
|
|
|
|
|
2015-09-06 10:46:58 +00:00
|
|
|
#[test]
|
|
|
|
fn flag_conflict() {
|
|
|
|
let result = App::new("flag_conflict")
|
|
|
|
.arg(Arg::from_usage("-f, --flag 'some flag'")
|
|
|
|
.conflicts_with("other"))
|
|
|
|
.arg(Arg::from_usage("-o, --other 'some flag'"))
|
2016-01-21 05:18:53 +00:00
|
|
|
.get_matches_from_safe(vec!["myprog", "-f", "-o"]);
|
2015-09-06 10:46:58 +00:00
|
|
|
assert!(result.is_err());
|
2015-10-01 01:45:35 +00:00
|
|
|
let err = result.err().unwrap();
|
2016-01-21 05:18:53 +00:00
|
|
|
assert_eq!(err.kind, ErrorKind::ArgumentConflict);
|
2015-09-06 10:46:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn flag_conflict_2() {
|
|
|
|
let result = App::new("flag_conflict")
|
|
|
|
.arg(Arg::from_usage("-f, --flag 'some flag'")
|
|
|
|
.conflicts_with("other"))
|
|
|
|
.arg(Arg::from_usage("-o, --other 'some flag'"))
|
2016-01-21 05:18:53 +00:00
|
|
|
.get_matches_from_safe(vec!["myprog", "-o", "-f"]);
|
2015-09-06 10:46:58 +00:00
|
|
|
assert!(result.is_err());
|
2015-10-01 01:45:35 +00:00
|
|
|
let err = result.err().unwrap();
|
2016-01-21 05:18:53 +00:00
|
|
|
assert_eq!(err.kind, ErrorKind::ArgumentConflict);
|
2015-09-06 10:46:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn group_conflict() {
|
|
|
|
let result = App::new("group_conflict")
|
|
|
|
.arg(Arg::from_usage("-f, --flag 'some flag'")
|
|
|
|
.conflicts_with("gr"))
|
2016-01-21 05:18:53 +00:00
|
|
|
.group(ArgGroup::with_name("gr")
|
2015-09-06 10:46:58 +00:00
|
|
|
.required(true)
|
2016-01-21 05:18:53 +00:00
|
|
|
.arg("some")
|
|
|
|
.arg("other"))
|
2015-09-06 10:46:58 +00:00
|
|
|
.arg(Arg::from_usage("--some 'some arg'"))
|
|
|
|
.arg(Arg::from_usage("--other 'other arg'"))
|
2016-01-21 05:18:53 +00:00
|
|
|
.get_matches_from_safe(vec!["myprog", "--other", "-f"]);
|
2015-09-06 10:46:58 +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 10:46:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn group_conflict_2() {
|
|
|
|
let result = App::new("group_conflict")
|
|
|
|
.arg(Arg::from_usage("-f, --flag 'some flag'")
|
|
|
|
.conflicts_with("gr"))
|
2016-01-21 05:18:53 +00:00
|
|
|
.group(ArgGroup::with_name("gr")
|
2015-09-06 10:46:58 +00:00
|
|
|
.required(true)
|
2016-01-21 05:18:53 +00:00
|
|
|
.arg("some")
|
|
|
|
.arg("other"))
|
2015-09-06 10:46:58 +00:00
|
|
|
.arg(Arg::from_usage("--some 'some arg'"))
|
|
|
|
.arg(Arg::from_usage("--other 'other arg'"))
|
2016-01-21 05:18:53 +00:00
|
|
|
.get_matches_from_safe(vec!["myprog", "-f", "--some"]);
|
2015-09-06 10:46:58 +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);
|
|
|
|
}
|
2016-05-09 03:20:50 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn conflict_output() {
|
2017-01-03 04:05:23 +00:00
|
|
|
test::compare_output(test::complex_app(), "clap-test val1 --flag --long-option-2 val2 -F", CONFLICT_ERR, true);
|
2016-05-09 03:20:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn conflict_output_rev() {
|
2017-01-03 04:05:23 +00:00
|
|
|
test::compare_output(test::complex_app(), "clap-test val1 -F --long-option-2 val2 --flag", CONFLICT_ERR_REV, true);
|
2016-05-09 03:20:50 +00:00
|
|
|
}
|
2017-10-24 19:23:29 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn conflict_with_unused_default_value() {
|
|
|
|
let result = App::new("conflict")
|
|
|
|
.arg(Arg::from_usage("-o, --opt=[opt] 'some opt'")
|
|
|
|
.default_value("default"))
|
|
|
|
.arg(Arg::from_usage("-f, --flag 'some flag'")
|
|
|
|
.conflicts_with("opt"))
|
|
|
|
.get_matches_from_safe(vec!["myprog", "-f"]);
|
|
|
|
assert!(result.is_ok());
|
|
|
|
let m = result.unwrap();
|
|
|
|
assert_eq!(m.value_of("opt"), Some("default"));
|
|
|
|
assert!(m.is_present("flag"));
|
|
|
|
}
|