2021-11-30 09:31:19 -06:00
|
|
|
use crate::utils;
|
2015-09-06 13:46:58 +03:00
|
|
|
|
2021-11-19 14:33:11 -06:00
|
|
|
use clap::{arg, App, Arg, ArgGroup, ErrorKind};
|
2015-09-06 13:46:58 +03:00
|
|
|
|
2020-08-16 03:47:19 -05:00
|
|
|
static CONFLICT_ERR: &str = "error: The argument '-F' cannot be used with '--flag'
|
2016-05-08 23:20:50 -04:00
|
|
|
|
|
|
|
USAGE:
|
2021-05-21 00:29:21 +05:30
|
|
|
clap-test --flag --long-option-2 <option2> <positional> <positional2>
|
2016-05-08 23:20:50 -04:00
|
|
|
|
2021-09-24 11:58:39 -04:00
|
|
|
For more information try --help
|
|
|
|
";
|
2016-05-08 23:20:50 -04:00
|
|
|
|
2020-08-16 03:47:19 -05:00
|
|
|
static CONFLICT_ERR_REV: &str = "error: The argument '--flag' cannot be used with '-F'
|
2016-05-08 23:20:50 -04:00
|
|
|
|
|
|
|
USAGE:
|
2021-05-21 00:29:21 +05:30
|
|
|
clap-test -F --long-option-2 <option2> <positional> <positional2>
|
2016-05-08 23:20:50 -04:00
|
|
|
|
2021-09-24 11:58:39 -04:00
|
|
|
For more information try --help
|
|
|
|
";
|
2016-05-08 23:20:50 -04:00
|
|
|
|
2020-08-16 03:47:19 -05:00
|
|
|
static CONFLICT_ERR_THREE: &str = "error: The argument '--two' cannot be used with '--one'
|
2020-08-15 04:43:12 -05:00
|
|
|
|
|
|
|
USAGE:
|
|
|
|
three_conflicting_arguments --one
|
|
|
|
|
2021-09-24 11:58:39 -04:00
|
|
|
For more information try --help
|
|
|
|
";
|
2020-08-15 04:43:12 -05:00
|
|
|
|
2015-09-06 13:46:58 +03:00
|
|
|
#[test]
|
|
|
|
fn flag_conflict() {
|
|
|
|
let result = App::new("flag_conflict")
|
2021-11-19 14:33:11 -06:00
|
|
|
.arg(arg!(-f --flag "some flag").conflicts_with("other"))
|
|
|
|
.arg(arg!(-o --other "some flag"))
|
2018-10-19 16:42:13 -04:00
|
|
|
.try_get_matches_from(vec!["myprog", "-f", "-o"]);
|
2015-09-06 13:46:58 +03:00
|
|
|
assert!(result.is_err());
|
2015-09-30 21:45:35 -04:00
|
|
|
let err = result.err().unwrap();
|
2016-01-21 00:18:53 -05:00
|
|
|
assert_eq!(err.kind, ErrorKind::ArgumentConflict);
|
2015-09-06 13:46:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn flag_conflict_2() {
|
|
|
|
let result = App::new("flag_conflict")
|
2021-11-19 14:33:11 -06:00
|
|
|
.arg(arg!(-f --flag "some flag").conflicts_with("other"))
|
|
|
|
.arg(arg!(-o --other "some flag"))
|
2018-10-19 16:42:13 -04:00
|
|
|
.try_get_matches_from(vec!["myprog", "-o", "-f"]);
|
2015-09-06 13:46:58 +03:00
|
|
|
assert!(result.is_err());
|
2015-09-30 21:45:35 -04:00
|
|
|
let err = result.err().unwrap();
|
2016-01-21 00:18:53 -05:00
|
|
|
assert_eq!(err.kind, ErrorKind::ArgumentConflict);
|
2020-01-05 19:46:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn flag_conflict_with_all() {
|
|
|
|
let result = App::new("flag_conflict")
|
2021-11-19 14:33:11 -06:00
|
|
|
.arg(arg!(-f --flag "some flag").conflicts_with_all(&["other"]))
|
|
|
|
.arg(arg!(-o --other "some flag"))
|
2020-01-05 19:46:28 +01:00
|
|
|
.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")
|
2021-11-19 14:33:11 -06:00
|
|
|
.arg(arg!(-f --flag "some flag").exclusive(true))
|
|
|
|
.arg(arg!(-o --other "some flag"))
|
2020-01-05 19:46:28 +01:00
|
|
|
.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 13:46:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn group_conflict() {
|
|
|
|
let result = App::new("group_conflict")
|
2021-11-19 14:33:11 -06:00
|
|
|
.arg(arg!(-f --flag "some flag").conflicts_with("gr"))
|
2020-05-14 22:50:56 +02:00
|
|
|
.group(ArgGroup::new("gr").required(true).arg("some").arg("other"))
|
2021-11-19 14:33:11 -06:00
|
|
|
.arg(arg!(--some "some arg"))
|
|
|
|
.arg(arg!(--other "other arg"))
|
2018-10-19 16:42:13 -04:00
|
|
|
.try_get_matches_from(vec!["myprog", "--other", "-f"]);
|
2015-09-06 13:46:58 +03:00
|
|
|
assert!(result.is_err());
|
|
|
|
let err = result.err().unwrap();
|
2016-01-21 00:18:53 -05:00
|
|
|
assert_eq!(err.kind, ErrorKind::ArgumentConflict);
|
2015-09-06 13:46:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn group_conflict_2() {
|
|
|
|
let result = App::new("group_conflict")
|
2021-11-19 14:33:11 -06:00
|
|
|
.arg(arg!(-f --flag "some flag").conflicts_with("gr"))
|
2020-05-14 22:50:56 +02:00
|
|
|
.group(ArgGroup::new("gr").required(true).arg("some").arg("other"))
|
2021-11-19 14:33:11 -06:00
|
|
|
.arg(arg!(--some "some arg"))
|
|
|
|
.arg(arg!(--other "other arg"))
|
2018-10-19 16:42:13 -04:00
|
|
|
.try_get_matches_from(vec!["myprog", "-f", "--some"]);
|
2015-09-06 13:46:58 +03:00
|
|
|
assert!(result.is_err());
|
|
|
|
let err = result.err().unwrap();
|
2016-01-21 00:18:53 -05:00
|
|
|
assert_eq!(err.kind, ErrorKind::ArgumentConflict);
|
|
|
|
}
|
2016-05-08 23:20:50 -04:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn conflict_output() {
|
2020-08-15 02:51:42 -05:00
|
|
|
assert!(utils::compare_output(
|
2020-02-04 09:10:53 +01:00
|
|
|
utils::complex_app(),
|
2020-08-15 03:20:48 -05:00
|
|
|
"clap-test val1 fa --flag --long-option-2 val2 -F",
|
2018-01-24 23:05:05 -05:00
|
|
|
CONFLICT_ERR,
|
|
|
|
true,
|
2020-08-15 02:51:42 -05:00
|
|
|
));
|
2016-05-08 23:20:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn conflict_output_rev() {
|
2020-08-15 02:51:42 -05:00
|
|
|
assert!(utils::compare_output(
|
2020-02-04 09:10:53 +01:00
|
|
|
utils::complex_app(),
|
2020-08-15 03:20:48 -05:00
|
|
|
"clap-test val1 fa -F --long-option-2 val2 --flag",
|
2018-01-24 23:05:05 -05:00
|
|
|
CONFLICT_ERR_REV,
|
|
|
|
true,
|
2020-08-15 02:51:42 -05:00
|
|
|
));
|
2016-05-08 23:20:50 -04:00
|
|
|
}
|
2017-10-24 12:23:29 -07:00
|
|
|
|
2020-08-15 03:20:48 -05:00
|
|
|
#[test]
|
|
|
|
fn conflict_output_with_required() {
|
2020-08-15 04:16:23 -05:00
|
|
|
assert!(utils::compare_output(
|
2020-08-15 03:20:48 -05:00
|
|
|
utils::complex_app(),
|
|
|
|
"clap-test val1 --flag --long-option-2 val2 -F",
|
|
|
|
CONFLICT_ERR,
|
|
|
|
true,
|
2020-08-15 04:16:23 -05:00
|
|
|
));
|
2020-08-15 03:20:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn conflict_output_rev_with_required() {
|
2020-08-15 04:16:23 -05:00
|
|
|
assert!(utils::compare_output(
|
2020-08-15 03:20:48 -05:00
|
|
|
utils::complex_app(),
|
|
|
|
"clap-test val1 -F --long-option-2 val2 --flag",
|
|
|
|
CONFLICT_ERR_REV,
|
|
|
|
true,
|
2020-08-15 04:16:23 -05:00
|
|
|
));
|
2020-08-15 03:20:48 -05:00
|
|
|
}
|
|
|
|
|
2020-08-15 04:43:12 -05:00
|
|
|
#[test]
|
|
|
|
fn conflict_output_three_conflicting() {
|
|
|
|
let app = App::new("three_conflicting_arguments")
|
|
|
|
.arg(
|
|
|
|
Arg::new("one")
|
|
|
|
.long("one")
|
|
|
|
.conflicts_with_all(&["two", "three"]),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::new("two")
|
|
|
|
.long("two")
|
|
|
|
.conflicts_with_all(&["one", "three"]),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::new("three")
|
|
|
|
.long("three")
|
|
|
|
.conflicts_with_all(&["one", "two"]),
|
|
|
|
);
|
|
|
|
assert!(utils::compare_output(
|
|
|
|
app,
|
|
|
|
"three_conflicting_arguments --one --two --three",
|
|
|
|
CONFLICT_ERR_THREE,
|
|
|
|
true,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2020-04-09 12:20:46 +02:00
|
|
|
#[test]
|
|
|
|
fn two_conflicting_arguments() {
|
|
|
|
let a = App::new("two_conflicting_arguments")
|
|
|
|
.arg(
|
2020-05-14 22:50:56 +02:00
|
|
|
Arg::new("develop")
|
2020-04-09 12:20:46 +02:00
|
|
|
.long("develop")
|
|
|
|
.conflicts_with("production"),
|
|
|
|
)
|
|
|
|
.arg(
|
2020-05-14 22:50:56 +02:00
|
|
|
Arg::new("production")
|
2020-04-09 12:20:46 +02:00
|
|
|
.long("production")
|
|
|
|
.conflicts_with("develop"),
|
|
|
|
)
|
|
|
|
.try_get_matches_from(vec!["", "--develop", "--production"]);
|
|
|
|
|
|
|
|
assert!(a.is_err());
|
|
|
|
let a = a.unwrap_err();
|
2020-08-22 12:43:58 +03:00
|
|
|
assert!(
|
|
|
|
a.to_string()
|
|
|
|
.contains("The argument \'--production\' cannot be used with \'--develop\'"),
|
|
|
|
"{}",
|
|
|
|
a
|
2020-04-09 12:20:46 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn three_conflicting_arguments() {
|
2020-08-15 04:43:12 -05:00
|
|
|
let a = App::new("three_conflicting_arguments")
|
2020-04-09 12:20:46 +02:00
|
|
|
.arg(
|
2020-05-14 22:50:56 +02:00
|
|
|
Arg::new("one")
|
2020-04-09 12:20:46 +02:00
|
|
|
.long("one")
|
|
|
|
.conflicts_with_all(&["two", "three"]),
|
|
|
|
)
|
|
|
|
.arg(
|
2020-05-14 22:50:56 +02:00
|
|
|
Arg::new("two")
|
2020-04-09 12:20:46 +02:00
|
|
|
.long("two")
|
|
|
|
.conflicts_with_all(&["one", "three"]),
|
|
|
|
)
|
|
|
|
.arg(
|
2020-05-14 22:50:56 +02:00
|
|
|
Arg::new("three")
|
2020-04-09 12:20:46 +02:00
|
|
|
.long("three")
|
|
|
|
.conflicts_with_all(&["one", "two"]),
|
|
|
|
)
|
|
|
|
.try_get_matches_from(vec!["", "--one", "--two", "--three"]);
|
|
|
|
|
|
|
|
assert!(a.is_err());
|
|
|
|
let a = a.unwrap_err();
|
2020-08-22 12:43:58 +03:00
|
|
|
assert!(
|
|
|
|
a.to_string()
|
|
|
|
.contains("The argument \'--two\' cannot be used with \'--one\'"),
|
|
|
|
"{}",
|
|
|
|
a
|
2020-04-09 12:20:46 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
#[test]
|
2020-04-10 00:33:16 +02:00
|
|
|
#[should_panic = "Argument 'config' cannot conflict with itself"]
|
2020-04-09 12:20:46 +02:00
|
|
|
fn self_conflicting_arg() {
|
|
|
|
let _ = App::new("prog")
|
2020-05-14 22:50:56 +02:00
|
|
|
.arg(Arg::new("config").long("config").conflicts_with("config"))
|
2020-04-09 12:20:46 +02:00
|
|
|
.try_get_matches_from(vec!["", "--config"]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
#[test]
|
2020-04-24 22:34:23 +03:00
|
|
|
#[should_panic = "Argument or group 'extra' specified in 'conflicts_with*' for 'config' does not exist"]
|
2020-04-09 12:20:46 +02:00
|
|
|
fn conflicts_with_invalid_arg() {
|
|
|
|
let _ = App::new("prog")
|
2020-05-14 22:50:56 +02:00
|
|
|
.arg(Arg::new("config").long("config").conflicts_with("extra"))
|
2020-04-09 12:20:46 +02:00
|
|
|
.try_get_matches_from(vec!["", "--config"]);
|
|
|
|
}
|
2020-04-26 20:48:08 +03:00
|
|
|
|
|
|
|
#[test]
|
2021-10-25 18:36:01 +01:00
|
|
|
fn conflict_with_unused_default() {
|
|
|
|
let result = App::new("conflict")
|
2021-11-19 14:33:11 -06:00
|
|
|
.arg(
|
|
|
|
arg!(-o --opt <opt> "some opt")
|
|
|
|
.required(false)
|
|
|
|
.default_value("default"),
|
|
|
|
)
|
|
|
|
.arg(arg!(-f --flag "some flag").conflicts_with("opt"))
|
2021-10-25 18:36:01 +01:00
|
|
|
.try_get_matches_from(vec!["myprog", "-f"]);
|
|
|
|
|
|
|
|
assert!(result.is_ok());
|
|
|
|
let m = result.unwrap();
|
|
|
|
|
|
|
|
assert_eq!(m.value_of("opt"), Some("default"));
|
|
|
|
assert!(m.is_present("flag"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn conflicts_with_alongside_default() {
|
2020-04-26 20:48:08 +03:00
|
|
|
let result = App::new("conflict")
|
|
|
|
.arg(
|
2021-11-19 14:33:11 -06:00
|
|
|
arg!(-o --opt <opt> "some opt")
|
2020-04-26 20:48:08 +03:00
|
|
|
.default_value("default")
|
2021-11-19 14:33:11 -06:00
|
|
|
.required(false)
|
2020-04-26 20:48:08 +03:00
|
|
|
.conflicts_with("flag"),
|
|
|
|
)
|
2021-11-19 14:33:11 -06:00
|
|
|
.arg(arg!(-f --flag "some flag"))
|
2020-04-26 20:48:08 +03:00
|
|
|
.try_get_matches_from(vec!["myprog", "-f"]);
|
|
|
|
|
2021-10-29 18:27:33 +01:00
|
|
|
assert!(
|
|
|
|
result.is_ok(),
|
|
|
|
"conflicts_with should ignore default_value: {:?}",
|
|
|
|
result.unwrap_err()
|
|
|
|
);
|
2020-04-26 20:48:08 +03:00
|
|
|
let m = result.unwrap();
|
|
|
|
|
|
|
|
assert_eq!(m.value_of("opt"), Some("default"));
|
|
|
|
assert!(m.is_present("flag"));
|
|
|
|
}
|