2022-02-12 03:48:29 +00:00
|
|
|
use clap::{arg, error::ErrorKind, Arg, Command};
|
2020-02-04 08:10:53 +00:00
|
|
|
|
2018-02-10 04:35:14 +00:00
|
|
|
#[test]
|
|
|
|
fn flag_overrides_itself() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let res = Command::new("posix")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(
|
|
|
|
arg!(--flag "some flag"
|
|
|
|
)
|
|
|
|
.overrides_with("flag"),
|
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "--flag", "--flag"]);
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(res.is_ok(), "{}", res.unwrap_err());
|
2018-02-10 04:35:14 +00:00
|
|
|
let m = res.unwrap();
|
|
|
|
assert!(m.is_present("flag"));
|
2022-06-06 17:35:00 +00:00
|
|
|
#[allow(deprecated)]
|
|
|
|
{
|
|
|
|
assert_eq!(m.occurrences_of("flag"), 1);
|
|
|
|
}
|
2018-02-10 04:35:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn mult_flag_overrides_itself() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let res = Command::new("posix")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(--flag ... "some flag").overrides_with("flag"))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "--flag", "--flag", "--flag", "--flag"]);
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(res.is_ok(), "{}", res.unwrap_err());
|
2018-02-10 04:35:14 +00:00
|
|
|
let m = res.unwrap();
|
|
|
|
assert!(m.is_present("flag"));
|
2022-06-06 17:35:00 +00:00
|
|
|
#[allow(deprecated)]
|
|
|
|
{
|
|
|
|
assert_eq!(m.occurrences_of("flag"), 4);
|
|
|
|
}
|
2018-02-10 04:35:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn option_overrides_itself() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let res = Command::new("posix")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(
|
|
|
|
arg!(--opt <val> "some option")
|
|
|
|
.required(false)
|
|
|
|
.overrides_with("opt"),
|
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "--opt=some", "--opt=other"]);
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(res.is_ok(), "{}", res.unwrap_err());
|
2018-02-10 04:35:14 +00:00
|
|
|
let m = res.unwrap();
|
|
|
|
assert!(m.is_present("opt"));
|
2022-06-06 17:35:00 +00:00
|
|
|
#[allow(deprecated)]
|
|
|
|
{
|
|
|
|
assert_eq!(m.occurrences_of("opt"), 1);
|
|
|
|
}
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("opt").map(|v| v.as_str()),
|
|
|
|
Some("other")
|
|
|
|
);
|
2018-02-10 04:35:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn mult_option_require_delim_overrides_itself() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let res = Command::new("posix")
|
2018-08-02 03:13:51 +00:00
|
|
|
.arg(
|
2021-11-19 20:33:11 +00:00
|
|
|
arg!(--opt <val> ... "some option")
|
|
|
|
.required(false)
|
2018-08-02 03:13:51 +00:00
|
|
|
.overrides_with("opt")
|
|
|
|
.number_of_values(1)
|
2021-02-24 15:07:57 +00:00
|
|
|
.takes_value(true)
|
2022-02-09 17:16:34 +00:00
|
|
|
.use_value_delimiter(true)
|
|
|
|
.require_value_delimiter(true),
|
2018-08-02 03:13:51 +00:00
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "--opt=some", "--opt=other", "--opt=one,two"]);
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(res.is_ok(), "{}", res.unwrap_err());
|
2018-02-10 04:35:14 +00:00
|
|
|
let m = res.unwrap();
|
|
|
|
assert!(m.is_present("opt"));
|
2022-06-06 17:35:00 +00:00
|
|
|
#[allow(deprecated)]
|
|
|
|
{
|
|
|
|
assert_eq!(m.occurrences_of("opt"), 3);
|
|
|
|
}
|
2018-08-02 03:13:51 +00:00
|
|
|
assert_eq!(
|
2022-05-24 15:16:50 +00:00
|
|
|
m.get_many::<String>("opt")
|
|
|
|
.unwrap()
|
|
|
|
.map(|v| v.as_str())
|
|
|
|
.collect::<Vec<_>>(),
|
2018-08-02 03:13:51 +00:00
|
|
|
&["some", "other", "one", "two"]
|
|
|
|
);
|
2018-02-10 04:35:14 +00:00
|
|
|
}
|
2015-08-27 21:03:45 +00:00
|
|
|
|
2018-02-10 04:35:14 +00:00
|
|
|
#[test]
|
|
|
|
fn mult_option_overrides_itself() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let res = Command::new("posix")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(
|
|
|
|
arg!(--opt <val> ... "some option")
|
|
|
|
.required(false)
|
|
|
|
.multiple_values(true)
|
|
|
|
.overrides_with("opt"),
|
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec![
|
2020-07-18 17:47:04 +00:00
|
|
|
"",
|
|
|
|
"--opt",
|
|
|
|
"first",
|
|
|
|
"overrides",
|
|
|
|
"--opt",
|
|
|
|
"some",
|
|
|
|
"other",
|
|
|
|
"val",
|
2018-08-02 03:13:51 +00:00
|
|
|
]);
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(res.is_ok(), "{}", res.unwrap_err());
|
2018-02-10 04:35:14 +00:00
|
|
|
let m = res.unwrap();
|
|
|
|
assert!(m.is_present("opt"));
|
2022-06-06 17:35:00 +00:00
|
|
|
#[allow(deprecated)]
|
|
|
|
{
|
|
|
|
assert_eq!(m.occurrences_of("opt"), 2);
|
|
|
|
}
|
2018-08-02 03:13:51 +00:00
|
|
|
assert_eq!(
|
2022-05-24 15:16:50 +00:00
|
|
|
m.get_many::<String>("opt")
|
|
|
|
.unwrap()
|
|
|
|
.map(|v| v.as_str())
|
|
|
|
.collect::<Vec<_>>(),
|
2020-07-18 17:47:04 +00:00
|
|
|
&["first", "overrides", "some", "other", "val"]
|
2018-08-02 03:13:51 +00:00
|
|
|
);
|
2018-02-10 04:35:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn option_use_delim_false_override_itself() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::new("posix")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(
|
|
|
|
arg!(--opt <val> "some option")
|
|
|
|
.required(false)
|
|
|
|
.overrides_with("opt"),
|
|
|
|
)
|
2021-12-27 18:56:12 +00:00
|
|
|
.try_get_matches_from(vec!["", "--opt=some,other", "--opt=one,two"])
|
|
|
|
.unwrap();
|
2018-02-10 04:35:14 +00:00
|
|
|
assert!(m.is_present("opt"));
|
2022-06-06 17:35:00 +00:00
|
|
|
#[allow(deprecated)]
|
|
|
|
{
|
|
|
|
assert_eq!(m.occurrences_of("opt"), 1);
|
|
|
|
}
|
2018-08-02 03:13:51 +00:00
|
|
|
assert_eq!(
|
2022-05-24 15:16:50 +00:00
|
|
|
m.get_many::<String>("opt")
|
|
|
|
.unwrap()
|
|
|
|
.map(|v| v.as_str())
|
|
|
|
.collect::<Vec<_>>(),
|
2018-08-02 03:13:51 +00:00
|
|
|
&["one,two"]
|
|
|
|
);
|
2018-02-10 04:35:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn pos_mult_overrides_itself() {
|
|
|
|
// opts with multiple
|
2022-02-12 03:48:29 +00:00
|
|
|
let res = Command::new("posix")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!([val] ... "some pos").overrides_with("val"))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "some", "other", "value"]);
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(res.is_ok(), "{}", res.unwrap_err());
|
2018-02-10 04:35:14 +00:00
|
|
|
let m = res.unwrap();
|
|
|
|
assert!(m.is_present("val"));
|
2022-06-06 17:35:00 +00:00
|
|
|
#[allow(deprecated)]
|
|
|
|
{
|
|
|
|
assert_eq!(m.occurrences_of("val"), 3);
|
|
|
|
}
|
2018-08-02 03:13:51 +00:00
|
|
|
assert_eq!(
|
2022-05-24 15:16:50 +00:00
|
|
|
m.get_many::<String>("val")
|
|
|
|
.unwrap()
|
|
|
|
.map(|v| v.as_str())
|
|
|
|
.collect::<Vec<_>>(),
|
2018-08-02 03:13:51 +00:00
|
|
|
&["some", "other", "value"]
|
|
|
|
);
|
2018-02-10 04:35:14 +00:00
|
|
|
}
|
2015-08-27 21:03:45 +00:00
|
|
|
#[test]
|
|
|
|
fn posix_compatible_flags_long() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::new("posix")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(--flag "some flag").overrides_with("color"))
|
|
|
|
.arg(arg!(--color "some other flag"))
|
2021-12-27 18:56:12 +00:00
|
|
|
.try_get_matches_from(vec!["", "--flag", "--color"])
|
|
|
|
.unwrap();
|
2015-08-27 21:03:45 +00:00
|
|
|
assert!(m.is_present("color"));
|
|
|
|
assert!(!m.is_present("flag"));
|
2018-01-25 20:08:31 +00:00
|
|
|
}
|
2015-08-27 21:03:45 +00:00
|
|
|
|
2018-01-25 20:08:31 +00:00
|
|
|
#[test]
|
|
|
|
fn posix_compatible_flags_long_rev() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::new("posix")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(--flag "some flag").overrides_with("color"))
|
|
|
|
.arg(arg!(--color "some other flag"))
|
2021-12-27 18:56:12 +00:00
|
|
|
.try_get_matches_from(vec!["", "--color", "--flag"])
|
|
|
|
.unwrap();
|
2015-08-27 21:03:45 +00:00
|
|
|
assert!(!m.is_present("color"));
|
|
|
|
assert!(m.is_present("flag"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn posix_compatible_flags_short() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::new("posix")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(-f --flag "some flag").overrides_with("color"))
|
|
|
|
.arg(arg!(-c --color "some other flag"))
|
2021-12-27 18:56:12 +00:00
|
|
|
.try_get_matches_from(vec!["", "-f", "-c"])
|
|
|
|
.unwrap();
|
2015-08-27 21:03:45 +00:00
|
|
|
assert!(m.is_present("color"));
|
|
|
|
assert!(!m.is_present("flag"));
|
2018-01-25 20:08:31 +00:00
|
|
|
}
|
2015-08-27 21:03:45 +00:00
|
|
|
|
2018-01-25 20:08:31 +00:00
|
|
|
#[test]
|
|
|
|
fn posix_compatible_flags_short_rev() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::new("posix")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(-f --flag "some flag").overrides_with("color"))
|
|
|
|
.arg(arg!(-c --color "some other flag"))
|
2021-12-27 18:56:12 +00:00
|
|
|
.try_get_matches_from(vec!["", "-c", "-f"])
|
|
|
|
.unwrap();
|
2015-08-27 21:03:45 +00:00
|
|
|
assert!(!m.is_present("color"));
|
|
|
|
assert!(m.is_present("flag"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn posix_compatible_opts_long() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::new("posix")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(
|
|
|
|
arg!(--flag <flag> "some flag")
|
|
|
|
.required(false)
|
|
|
|
.overrides_with("color"),
|
|
|
|
)
|
|
|
|
.arg(arg!(--color <color> "some other flag").required(false))
|
2021-12-27 18:56:12 +00:00
|
|
|
.try_get_matches_from(vec!["", "--flag", "some", "--color", "other"])
|
|
|
|
.unwrap();
|
2015-08-27 21:03:45 +00:00
|
|
|
assert!(m.is_present("color"));
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("color").map(|v| v.as_str()).unwrap(),
|
|
|
|
"other"
|
|
|
|
);
|
2015-08-27 21:03:45 +00:00
|
|
|
assert!(!m.is_present("flag"));
|
2018-01-25 20:08:31 +00:00
|
|
|
}
|
2015-08-27 21:03:45 +00:00
|
|
|
|
2018-01-25 20:08:31 +00:00
|
|
|
#[test]
|
|
|
|
fn posix_compatible_opts_long_rev() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::new("posix")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(
|
|
|
|
arg!(--flag <flag> "some flag")
|
|
|
|
.required(false)
|
|
|
|
.overrides_with("color"),
|
|
|
|
)
|
|
|
|
.arg(arg!(--color <color> "some other flag").required(false))
|
2021-12-27 18:56:12 +00:00
|
|
|
.try_get_matches_from(vec!["", "--color", "some", "--flag", "other"])
|
|
|
|
.unwrap();
|
2015-08-27 21:03:45 +00:00
|
|
|
assert!(!m.is_present("color"));
|
|
|
|
assert!(m.is_present("flag"));
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("flag").map(|v| v.as_str()).unwrap(),
|
|
|
|
"other"
|
|
|
|
);
|
2015-08-27 21:03:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn posix_compatible_opts_long_equals() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::new("posix")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(
|
|
|
|
arg!(--flag <flag> "some flag")
|
|
|
|
.required(false)
|
|
|
|
.overrides_with("color"),
|
|
|
|
)
|
|
|
|
.arg(arg!(--color <color> "some other flag").required(false))
|
2021-12-27 18:56:12 +00:00
|
|
|
.try_get_matches_from(vec!["", "--flag=some", "--color=other"])
|
|
|
|
.unwrap();
|
2015-08-27 21:03:45 +00:00
|
|
|
assert!(m.is_present("color"));
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("color").map(|v| v.as_str()).unwrap(),
|
|
|
|
"other"
|
|
|
|
);
|
2015-08-27 21:03:45 +00:00
|
|
|
assert!(!m.is_present("flag"));
|
2018-01-25 20:08:31 +00:00
|
|
|
}
|
2015-08-27 21:03:45 +00:00
|
|
|
|
2018-01-25 20:08:31 +00:00
|
|
|
#[test]
|
|
|
|
fn posix_compatible_opts_long_equals_rev() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::new("posix")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(
|
|
|
|
arg!(--flag <flag> "some flag")
|
|
|
|
.required(false)
|
|
|
|
.overrides_with("color"),
|
|
|
|
)
|
|
|
|
.arg(arg!(--color <color> "some other flag").required(false))
|
2021-12-27 18:56:12 +00:00
|
|
|
.try_get_matches_from(vec!["", "--color=some", "--flag=other"])
|
|
|
|
.unwrap();
|
2015-08-27 21:03:45 +00:00
|
|
|
assert!(!m.is_present("color"));
|
|
|
|
assert!(m.is_present("flag"));
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("flag").map(|v| v.as_str()).unwrap(),
|
|
|
|
"other"
|
|
|
|
);
|
2015-08-27 21:03:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn posix_compatible_opts_short() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::new("posix")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(
|
|
|
|
arg!(f: -f <flag> "some flag")
|
|
|
|
.required(false)
|
|
|
|
.overrides_with("c"),
|
|
|
|
)
|
|
|
|
.arg(arg!(c: -c <color> "some other flag").required(false))
|
2021-12-27 18:56:12 +00:00
|
|
|
.try_get_matches_from(vec!["", "-f", "some", "-c", "other"])
|
|
|
|
.unwrap();
|
2016-01-25 20:56:37 +00:00
|
|
|
assert!(m.is_present("c"));
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("c").map(|v| v.as_str()).unwrap(),
|
|
|
|
"other"
|
|
|
|
);
|
2016-01-25 20:56:37 +00:00
|
|
|
assert!(!m.is_present("f"));
|
2018-01-25 20:08:31 +00:00
|
|
|
}
|
2015-08-27 21:03:45 +00:00
|
|
|
|
2018-01-25 20:08:31 +00:00
|
|
|
#[test]
|
|
|
|
fn posix_compatible_opts_short_rev() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::new("posix")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(
|
|
|
|
arg!(f: -f <flag> "some flag")
|
|
|
|
.required(false)
|
|
|
|
.overrides_with("c"),
|
|
|
|
)
|
|
|
|
.arg(arg!(c: -c <color> "some other flag").required(false))
|
2021-12-27 18:56:12 +00:00
|
|
|
.try_get_matches_from(vec!["", "-c", "some", "-f", "other"])
|
|
|
|
.unwrap();
|
2016-01-25 20:56:37 +00:00
|
|
|
assert!(!m.is_present("c"));
|
|
|
|
assert!(m.is_present("f"));
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("f").map(|v| v.as_str()).unwrap(),
|
|
|
|
"other"
|
|
|
|
);
|
2015-09-06 19:22:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-07-18 17:47:04 +00:00
|
|
|
fn conflict_overridden() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::new("conflict_overridden")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(-f --flag "some flag").conflicts_with("debug"))
|
|
|
|
.arg(arg!(-d --debug "other flag"))
|
|
|
|
.arg(arg!(-c --color "third flag").overrides_with("flag"))
|
2021-12-27 18:56:12 +00:00
|
|
|
.try_get_matches_from(vec!["", "-f", "-c", "-d"])
|
|
|
|
.unwrap();
|
2015-09-06 19:22:37 +00:00
|
|
|
assert!(m.is_present("color"));
|
|
|
|
assert!(!m.is_present("flag"));
|
|
|
|
assert!(m.is_present("debug"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-07-18 17:47:04 +00:00
|
|
|
fn conflict_overridden_2() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let result = Command::new("conflict_overridden")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(-f --flag "some flag").conflicts_with("debug"))
|
|
|
|
.arg(arg!(-d --debug "other flag"))
|
|
|
|
.arg(arg!(-c --color "third flag").overrides_with("flag"))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "-f", "-d", "-c"]);
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(result.is_ok(), "{}", result.unwrap_err());
|
2016-01-22 17:58:56 +00:00
|
|
|
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]
|
2020-07-18 17:47:04 +00:00
|
|
|
fn conflict_overridden_3() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let result = Command::new("conflict_overridden")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(-f --flag "some flag").conflicts_with("debug"))
|
|
|
|
.arg(arg!(-d --debug "other flag"))
|
|
|
|
.arg(arg!(-c --color "third flag").overrides_with("flag"))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "-d", "-c", "-f"]);
|
2015-09-06 19:22:37 +00:00
|
|
|
assert!(result.is_err());
|
|
|
|
let err = result.err().unwrap();
|
2022-01-25 22:19:28 +00:00
|
|
|
assert_eq!(err.kind(), ErrorKind::ArgumentConflict);
|
2015-09-06 19:22:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-07-18 17:47:04 +00:00
|
|
|
fn conflict_overridden_4() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::new("conflict_overridden")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(-f --flag "some flag").conflicts_with("debug"))
|
|
|
|
.arg(arg!(-d --debug "other flag"))
|
|
|
|
.arg(arg!(-c --color "third flag").overrides_with("flag"))
|
2021-12-27 18:56:12 +00:00
|
|
|
.try_get_matches_from(vec!["", "-d", "-f", "-c"])
|
|
|
|
.unwrap();
|
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() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let result = Command::new("require_overridden")
|
2020-05-14 20:50:56 +00:00
|
|
|
.arg(Arg::new("pos").index(1).required(true))
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(-c --color "some flag").overrides_with("pos"))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(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]
|
2020-07-18 17:47:04 +00:00
|
|
|
fn require_overridden_2() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::new("require_overridden")
|
2020-05-14 20:50:56 +00:00
|
|
|
.arg(Arg::new("req_pos").required(true))
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(-c --color "other flag").overrides_with("req_pos"))
|
2021-12-27 18:56:12 +00:00
|
|
|
.try_get_matches_from(vec!["", "-c", "req_pos"])
|
|
|
|
.unwrap();
|
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]
|
2020-07-18 17:47:04 +00:00
|
|
|
fn require_overridden_3() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::new("require_overridden")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(-f --flag "some flag").requires("debug"))
|
|
|
|
.arg(arg!(-d --debug "other flag"))
|
|
|
|
.arg(arg!(-c --color "third flag").overrides_with("flag"))
|
2021-12-27 18:56:12 +00:00
|
|
|
.try_get_matches_from(vec!["", "-f", "-c"])
|
|
|
|
.unwrap();
|
2015-09-06 19:34:37 +00:00
|
|
|
assert!(m.is_present("color"));
|
|
|
|
assert!(!m.is_present("flag"));
|
|
|
|
assert!(!m.is_present("debug"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-07-18 17:47:04 +00:00
|
|
|
fn require_overridden_4() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let result = Command::new("require_overridden")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(-f --flag "some flag").requires("debug"))
|
|
|
|
.arg(arg!(-d --debug "other flag"))
|
|
|
|
.arg(arg!(-c --color "third flag").overrides_with("flag"))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "-c", "-f"]);
|
2015-09-06 19:34:37 +00:00
|
|
|
assert!(result.is_err());
|
|
|
|
let err = result.err().unwrap();
|
2022-01-25 22:19:28 +00:00
|
|
|
assert_eq!(err.kind(), ErrorKind::MissingRequiredArgument);
|
2015-11-09 13:57:20 +00:00
|
|
|
}
|
2021-08-14 07:50:39 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn issue_1374_overrides_self_with_multiple_values() {
|
2022-02-14 21:47:20 +00:00
|
|
|
let cmd = Command::new("test").arg(
|
2021-08-14 07:50:39 +00:00
|
|
|
Arg::new("input")
|
|
|
|
.long("input")
|
|
|
|
.takes_value(true)
|
|
|
|
.overrides_with("input")
|
|
|
|
.min_values(0),
|
|
|
|
);
|
2022-02-14 21:47:20 +00:00
|
|
|
let m = cmd
|
2021-08-14 07:50:39 +00:00
|
|
|
.clone()
|
2021-12-27 18:56:12 +00:00
|
|
|
.try_get_matches_from(&["test", "--input", "a", "b", "c", "--input", "d"])
|
|
|
|
.unwrap();
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_many::<String>("input")
|
|
|
|
.unwrap()
|
|
|
|
.map(|v| v.as_str())
|
|
|
|
.collect::<Vec<_>>(),
|
|
|
|
&["d"]
|
|
|
|
);
|
2022-02-14 21:47:20 +00:00
|
|
|
let m = cmd
|
2021-08-14 07:50:39 +00:00
|
|
|
.clone()
|
2021-12-27 18:56:12 +00:00
|
|
|
.try_get_matches_from(&["test", "--input", "a", "b", "--input", "c", "d"])
|
|
|
|
.unwrap();
|
2021-08-14 07:50:39 +00:00
|
|
|
assert_eq!(
|
2022-05-24 15:16:50 +00:00
|
|
|
m.get_many::<String>("input")
|
|
|
|
.unwrap()
|
|
|
|
.map(|v| v.as_str())
|
|
|
|
.collect::<Vec<_>>(),
|
2021-08-14 07:50:39 +00:00
|
|
|
&["c", "d"]
|
|
|
|
);
|
|
|
|
}
|
2021-12-27 18:46:55 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn incremental_override() {
|
2022-02-14 21:47:20 +00:00
|
|
|
let mut cmd = Command::new("test")
|
2021-12-27 18:46:55 +00:00
|
|
|
.arg(arg!(--name <NAME>).multiple_occurrences(true))
|
|
|
|
.arg(arg!(--"no-name").overrides_with("name"));
|
2022-02-14 21:47:20 +00:00
|
|
|
let m = cmd
|
2021-12-27 18:46:55 +00:00
|
|
|
.try_get_matches_from_mut(&["test", "--name=ahmed", "--no-name", "--name=ali"])
|
|
|
|
.unwrap();
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_many::<String>("name")
|
|
|
|
.unwrap()
|
|
|
|
.map(|v| v.as_str())
|
|
|
|
.collect::<Vec<_>>(),
|
|
|
|
&["ali"]
|
|
|
|
);
|
2021-12-27 18:46:55 +00:00
|
|
|
assert!(!m.is_present("no-name"));
|
|
|
|
}
|