2020-02-04 08:10:53 +00:00
|
|
|
mod utils;
|
2015-09-06 10:46:58 +00:00
|
|
|
|
2018-01-25 04:05:05 +00:00
|
|
|
use clap::{App, Arg, ArgGroup, ErrorKind};
|
2015-09-06 10:46:58 +00:00
|
|
|
|
2019-10-02 13:27:19 +00:00
|
|
|
static CONFLICT_ERR: &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";
|
|
|
|
|
2019-10-02 13:27:19 +00:00
|
|
|
static CONFLICT_ERR_REV: &str = "error: The argument '--flag' cannot be used with '-F'
|
2016-05-09 03:20:50 +00:00
|
|
|
|
|
|
|
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")
|
2018-04-21 18:59:19 +00:00
|
|
|
.arg(Arg::from("-f, --flag 'some flag'").conflicts_with("other"))
|
|
|
|
.arg(Arg::from("-o, --other 'some flag'"))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(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")
|
2018-04-21 18:59:19 +00:00
|
|
|
.arg(Arg::from("-f, --flag 'some flag'").conflicts_with("other"))
|
|
|
|
.arg(Arg::from("-o, --other 'some flag'"))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(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);
|
2020-01-05 18:46:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn flag_conflict_with_all() {
|
|
|
|
let result = App::new("flag_conflict")
|
|
|
|
.arg(Arg::from("-f, --flag 'some flag'").conflicts_with_all(&["other"]))
|
|
|
|
.arg(Arg::from("-o, --other 'some flag'"))
|
|
|
|
.try_get_matches_from(vec!["myprog", "-o", "-f"]);
|
|
|
|
assert!(result.is_err());
|
|
|
|
let err = result.err().unwrap();
|
|
|
|
assert_eq!(err.kind, ErrorKind::ArgumentConflict);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn flag_conflict_with_everything() {
|
|
|
|
let result = App::new("flag_conflict")
|
2020-01-07 19:16:59 +00:00
|
|
|
.arg(Arg::from("-f, --flag 'some flag'").exclusive(true))
|
2020-01-05 18:46:28 +00:00
|
|
|
.arg(Arg::from("-o, --other 'some flag'"))
|
|
|
|
.try_get_matches_from(vec!["myprog", "-o", "-f"]);
|
|
|
|
assert!(result.is_err());
|
|
|
|
let err = result.err().unwrap();
|
|
|
|
assert_eq!(err.kind, ErrorKind::ArgumentConflict);
|
2015-09-06 10:46:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn group_conflict() {
|
|
|
|
let result = App::new("group_conflict")
|
2018-04-21 18:59:19 +00:00
|
|
|
.arg(Arg::from("-f, --flag 'some flag'").conflicts_with("gr"))
|
2018-01-25 04:05:05 +00:00
|
|
|
.group(
|
|
|
|
ArgGroup::with_name("gr")
|
|
|
|
.required(true)
|
|
|
|
.arg("some")
|
|
|
|
.arg("other"),
|
|
|
|
)
|
2018-04-21 18:59:19 +00:00
|
|
|
.arg(Arg::from("--some 'some arg'"))
|
|
|
|
.arg(Arg::from("--other 'other arg'"))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(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")
|
2018-04-21 18:59:19 +00:00
|
|
|
.arg(Arg::from("-f, --flag 'some flag'").conflicts_with("gr"))
|
2018-01-25 04:05:05 +00:00
|
|
|
.group(
|
|
|
|
ArgGroup::with_name("gr")
|
|
|
|
.required(true)
|
|
|
|
.arg("some")
|
|
|
|
.arg("other"),
|
|
|
|
)
|
2018-04-21 18:59:19 +00:00
|
|
|
.arg(Arg::from("--some 'some arg'"))
|
|
|
|
.arg(Arg::from("--other 'other arg'"))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(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() {
|
2020-02-04 08:10:53 +00:00
|
|
|
utils::compare_output(
|
|
|
|
utils::complex_app(),
|
2018-01-25 04:05:05 +00:00
|
|
|
"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() {
|
2020-02-04 08:10:53 +00:00
|
|
|
utils::compare_output(
|
|
|
|
utils::complex_app(),
|
2018-01-25 04:05:05 +00:00
|
|
|
"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")
|
2018-04-21 18:59:19 +00:00
|
|
|
.arg(Arg::from("-o, --opt=[opt] 'some opt'").default_value("default"))
|
|
|
|
.arg(Arg::from("-f, --flag 'some flag'").conflicts_with("opt"))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["myprog", "-f"]);
|
2017-10-24 19:23:29 +00:00
|
|
|
assert!(result.is_ok());
|
|
|
|
let m = result.unwrap();
|
|
|
|
assert_eq!(m.value_of("opt"), Some("default"));
|
|
|
|
assert!(m.is_present("flag"));
|
|
|
|
}
|
2020-04-09 10:20:46 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn two_conflicting_arguments() {
|
|
|
|
let a = App::new("two_conflicting_arguments")
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("develop")
|
|
|
|
.long("develop")
|
|
|
|
.conflicts_with("production"),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("production")
|
|
|
|
.long("production")
|
|
|
|
.conflicts_with("develop"),
|
|
|
|
)
|
|
|
|
.try_get_matches_from(vec!["", "--develop", "--production"]);
|
|
|
|
|
|
|
|
assert!(a.is_err());
|
|
|
|
let a = a.unwrap_err();
|
|
|
|
assert_eq!(
|
|
|
|
a.cause,
|
|
|
|
"The argument \'--develop\' cannot be used with \'--production\'"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn three_conflicting_arguments() {
|
|
|
|
let a = App::new("two_conflicting_arguments")
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("one")
|
|
|
|
.long("one")
|
|
|
|
.conflicts_with_all(&["two", "three"]),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("two")
|
|
|
|
.long("two")
|
|
|
|
.conflicts_with_all(&["one", "three"]),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("three")
|
|
|
|
.long("three")
|
|
|
|
.conflicts_with_all(&["one", "two"]),
|
|
|
|
)
|
|
|
|
.try_get_matches_from(vec!["", "--one", "--two", "--three"]);
|
|
|
|
|
|
|
|
assert!(a.is_err());
|
|
|
|
let a = a.unwrap_err();
|
|
|
|
assert_eq!(
|
|
|
|
a.cause,
|
|
|
|
"The argument \'--one\' cannot be used with \'--two\'"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
#[test]
|
|
|
|
#[should_panic(expected = "Argument 'config' cannot conflict with itself")]
|
|
|
|
fn self_conflicting_arg() {
|
|
|
|
let _ = App::new("prog")
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("config")
|
|
|
|
.long("config")
|
|
|
|
.conflicts_with("config"),
|
|
|
|
)
|
|
|
|
.try_get_matches_from(vec!["", "--config"]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
#[test]
|
|
|
|
#[should_panic(expected = "Argument specified in 'conflicts_with*' for 'config' does not exist")]
|
|
|
|
fn conflicts_with_invalid_arg() {
|
|
|
|
let _ = App::new("prog")
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("config")
|
|
|
|
.long("config")
|
|
|
|
.conflicts_with("extra"),
|
|
|
|
)
|
|
|
|
.try_get_matches_from(vec!["", "--config"]);
|
|
|
|
}
|