2022-06-10 01:03:28 +00:00
|
|
|
use clap::{arg, error::ErrorKind, Arg, ArgAction, ArgGroup, Command};
|
2015-09-06 10:46:58 +00:00
|
|
|
|
2022-09-19 14:59:04 +00:00
|
|
|
#[cfg(feature = "error-context")]
|
|
|
|
use super::utils;
|
|
|
|
|
2015-09-06 10:46:58 +00:00
|
|
|
#[test]
|
|
|
|
fn flag_conflict() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let result = Command::new("flag_conflict")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(-f --flag "some flag").conflicts_with("other"))
|
|
|
|
.arg(arg!(-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();
|
2022-01-25 22:19:28 +00:00
|
|
|
assert_eq!(err.kind(), ErrorKind::ArgumentConflict);
|
2015-09-06 10:46:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn flag_conflict_2() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let result = Command::new("flag_conflict")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(-f --flag "some flag").conflicts_with("other"))
|
|
|
|
.arg(arg!(-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();
|
2022-01-25 22:19:28 +00:00
|
|
|
assert_eq!(err.kind(), ErrorKind::ArgumentConflict);
|
2020-01-05 18:46:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn flag_conflict_with_all() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let result = Command::new("flag_conflict")
|
2022-08-12 17:40:58 +00:00
|
|
|
.arg(arg!(-f --flag "some flag").conflicts_with_all(["other"]))
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(-o --other "some flag"))
|
2020-01-05 18:46:28 +00:00
|
|
|
.try_get_matches_from(vec!["myprog", "-o", "-f"]);
|
|
|
|
assert!(result.is_err());
|
|
|
|
let err = result.err().unwrap();
|
2022-01-25 22:19:28 +00:00
|
|
|
assert_eq!(err.kind(), ErrorKind::ArgumentConflict);
|
2020-01-05 18:46:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2022-06-04 13:53:10 +00:00
|
|
|
fn exclusive_flag() {
|
2022-10-18 00:02:39 +00:00
|
|
|
let cmd = Command::new("flag_conflict")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(-f --flag "some flag").exclusive(true))
|
2022-10-18 00:02:39 +00:00
|
|
|
.arg(arg!(-o --other "some flag"));
|
|
|
|
let result = cmd.clone().try_get_matches_from(vec!["myprog", "-f"]);
|
|
|
|
assert!(result.is_ok(), "{}", result.unwrap_err());
|
|
|
|
|
|
|
|
let result = cmd.clone().try_get_matches_from(vec!["myprog", "-o", "-f"]);
|
2020-01-05 18:46:28 +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 10:46:58 +00:00
|
|
|
}
|
|
|
|
|
2022-06-04 13:53:10 +00:00
|
|
|
#[test]
|
|
|
|
fn exclusive_option() {
|
|
|
|
let result = Command::new("flag_conflict")
|
2022-09-12 21:59:57 +00:00
|
|
|
.arg(arg!(-f --flag <VALUE> "some flag").exclusive(true))
|
|
|
|
.arg(arg!(-o --other <VALUE> "some flag"))
|
2022-06-04 13:53:10 +00:00
|
|
|
.try_get_matches_from(vec!["myprog", "-o=val1", "-f=val2"]);
|
|
|
|
assert!(result.is_err());
|
|
|
|
let err = result.err().unwrap();
|
|
|
|
assert_eq!(err.kind(), ErrorKind::ArgumentConflict);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn not_exclusive_with_defaults() {
|
|
|
|
let result = Command::new("flag_conflict")
|
2022-09-12 21:59:57 +00:00
|
|
|
.arg(arg!(-f --flag <VALUE> "some flag").exclusive(true))
|
2022-06-04 13:53:10 +00:00
|
|
|
.arg(
|
|
|
|
arg!(-o --other <VALUE> "some flag")
|
|
|
|
.required(false)
|
|
|
|
.default_value("val1"),
|
|
|
|
)
|
|
|
|
.try_get_matches_from(vec!["myprog", "-f=val2"]);
|
|
|
|
assert!(result.is_ok(), "{}", result.unwrap_err());
|
|
|
|
}
|
|
|
|
|
2022-10-18 00:02:39 +00:00
|
|
|
#[test]
|
|
|
|
fn not_exclusive_with_group() {
|
|
|
|
let cmd = Command::new("test")
|
|
|
|
.group(clap::ArgGroup::new("test").arg("foo"))
|
|
|
|
.arg(
|
|
|
|
clap::Arg::new("foo")
|
|
|
|
.long("foo")
|
|
|
|
.exclusive(true)
|
|
|
|
.action(clap::ArgAction::SetTrue),
|
|
|
|
);
|
|
|
|
let result = cmd.try_get_matches_from(vec!["test", "--foo"]);
|
|
|
|
assert!(result.is_ok(), "{}", result.unwrap_err());
|
|
|
|
}
|
|
|
|
|
2022-06-04 13:53:10 +00:00
|
|
|
#[test]
|
|
|
|
fn default_doesnt_activate_exclusive() {
|
|
|
|
let result = Command::new("flag_conflict")
|
|
|
|
.arg(
|
|
|
|
arg!(-f --flag <VALUE> "some flag")
|
|
|
|
.exclusive(true)
|
|
|
|
.default_value("val2"),
|
|
|
|
)
|
2022-09-12 21:59:57 +00:00
|
|
|
.arg(arg!(-o --other <VALUE> "some flag").default_value("val1"))
|
2022-06-04 13:53:10 +00:00
|
|
|
.try_get_matches_from(vec!["myprog"]);
|
|
|
|
assert!(result.is_ok(), "{}", result.unwrap_err());
|
|
|
|
}
|
|
|
|
|
2015-09-06 10:46:58 +00:00
|
|
|
#[test]
|
2021-12-22 19:13:11 +00:00
|
|
|
fn arg_conflicts_with_group() {
|
2022-02-14 21:47:20 +00:00
|
|
|
let mut cmd = Command::new("group_conflict")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(-f --flag "some flag").conflicts_with("gr"))
|
2021-12-22 19:13:11 +00:00
|
|
|
.group(ArgGroup::new("gr").arg("some").arg("other"))
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(--some "some arg"))
|
2021-12-22 19:13:11 +00:00
|
|
|
.arg(arg!(--other "other arg"));
|
|
|
|
|
2022-02-14 21:47:20 +00:00
|
|
|
let result = cmd.try_get_matches_from_mut(vec!["myprog", "--other", "-f"]);
|
2021-12-22 19:13:11 +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);
|
2021-12-22 19:13:11 +00:00
|
|
|
|
2022-02-14 21:47:20 +00:00
|
|
|
let result = cmd.try_get_matches_from_mut(vec!["myprog", "-f", "--some"]);
|
2015-09-06 10:46:58 +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);
|
2021-12-22 19:13:11 +00:00
|
|
|
|
2022-02-14 21:47:20 +00:00
|
|
|
let result = cmd.try_get_matches_from_mut(vec!["myprog", "--some"]);
|
2021-12-22 19:13:11 +00:00
|
|
|
if let Err(err) = result {
|
|
|
|
panic!("{}", err);
|
|
|
|
}
|
|
|
|
|
2022-02-14 21:47:20 +00:00
|
|
|
let result = cmd.try_get_matches_from_mut(vec!["myprog", "--other"]);
|
2021-12-22 19:13:11 +00:00
|
|
|
if let Err(err) = result {
|
|
|
|
panic!("{}", err);
|
|
|
|
}
|
|
|
|
|
2022-02-14 21:47:20 +00:00
|
|
|
let result = cmd.try_get_matches_from_mut(vec!["myprog", "--flag"]);
|
2021-12-22 19:13:11 +00:00
|
|
|
if let Err(err) = result {
|
|
|
|
panic!("{}", err);
|
|
|
|
}
|
2015-09-06 10:46:58 +00:00
|
|
|
}
|
|
|
|
|
2022-01-24 16:34:38 +00:00
|
|
|
#[test]
|
|
|
|
fn arg_conflicts_with_group_with_multiple_sources() {
|
2022-02-14 21:47:20 +00:00
|
|
|
let mut cmd = clap::Command::new("group_conflict")
|
2022-01-24 16:34:38 +00:00
|
|
|
.arg(clap::arg!(-f --flag "some flag").conflicts_with("gr"))
|
|
|
|
.group(clap::ArgGroup::new("gr").multiple(true))
|
2022-09-12 21:59:57 +00:00
|
|
|
.arg(clap::arg!(--some <name> "some arg").group("gr"))
|
2022-01-24 16:34:38 +00:00
|
|
|
.arg(
|
|
|
|
clap::arg!(--other <secs> "other arg")
|
|
|
|
.default_value("1000")
|
|
|
|
.group("gr"),
|
|
|
|
);
|
|
|
|
|
2022-02-14 21:47:20 +00:00
|
|
|
let result = cmd.try_get_matches_from_mut(vec!["myprog", "-f"]);
|
2022-01-24 16:34:38 +00:00
|
|
|
if let Err(err) = result {
|
|
|
|
panic!("{}", err);
|
|
|
|
}
|
|
|
|
|
2022-02-14 21:47:20 +00:00
|
|
|
let result = cmd.try_get_matches_from_mut(vec!["myprog", "--some", "usb1"]);
|
2022-01-24 16:34:38 +00:00
|
|
|
if let Err(err) = result {
|
|
|
|
panic!("{}", err);
|
|
|
|
}
|
|
|
|
|
2022-02-14 21:47:20 +00:00
|
|
|
let result = cmd.try_get_matches_from_mut(vec!["myprog", "--some", "usb1", "--other", "40"]);
|
2022-01-24 16:34:38 +00:00
|
|
|
if let Err(err) = result {
|
|
|
|
panic!("{}", err);
|
|
|
|
}
|
|
|
|
|
2022-02-14 21:47:20 +00:00
|
|
|
let result = cmd.try_get_matches_from_mut(vec!["myprog", "-f", "--some", "usb1"]);
|
2022-01-24 16:34:38 +00:00
|
|
|
let err = result.err().unwrap();
|
2022-01-25 22:19:28 +00:00
|
|
|
assert_eq!(err.kind(), ErrorKind::ArgumentConflict);
|
2022-01-24 16:34:38 +00:00
|
|
|
}
|
|
|
|
|
2015-09-06 10:46:58 +00:00
|
|
|
#[test]
|
2021-12-22 19:13:11 +00:00
|
|
|
fn group_conflicts_with_arg() {
|
2022-02-14 21:47:20 +00:00
|
|
|
let mut cmd = Command::new("group_conflict")
|
2021-12-22 19:13:11 +00:00
|
|
|
.arg(arg!(-f --flag "some flag"))
|
|
|
|
.group(
|
|
|
|
ArgGroup::new("gr")
|
|
|
|
.arg("some")
|
|
|
|
.arg("other")
|
|
|
|
.conflicts_with("flag"),
|
|
|
|
)
|
|
|
|
.arg(arg!(--some "some arg"))
|
|
|
|
.arg(arg!(--other "other arg"));
|
|
|
|
|
2022-02-14 21:47:20 +00:00
|
|
|
let result = cmd.try_get_matches_from_mut(vec!["myprog", "--other", "-f"]);
|
2021-12-22 19:13:11 +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);
|
2021-12-22 19:13:11 +00:00
|
|
|
|
2022-02-14 21:47:20 +00:00
|
|
|
let result = cmd.try_get_matches_from_mut(vec!["myprog", "-f", "--some"]);
|
2021-12-22 19:13:11 +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);
|
2021-12-22 19:13:11 +00:00
|
|
|
|
2022-02-14 21:47:20 +00:00
|
|
|
let result = cmd.try_get_matches_from_mut(vec!["myprog", "--some"]);
|
2021-12-22 19:13:11 +00:00
|
|
|
if let Err(err) = result {
|
|
|
|
panic!("{}", err);
|
|
|
|
}
|
|
|
|
|
2022-02-14 21:47:20 +00:00
|
|
|
let result = cmd.try_get_matches_from_mut(vec!["myprog", "--other"]);
|
2021-12-22 19:13:11 +00:00
|
|
|
if let Err(err) = result {
|
|
|
|
panic!("{}", err);
|
|
|
|
}
|
|
|
|
|
2022-02-14 21:47:20 +00:00
|
|
|
let result = cmd.try_get_matches_from_mut(vec!["myprog", "--flag"]);
|
2021-12-22 19:13:11 +00:00
|
|
|
if let Err(err) = result {
|
|
|
|
panic!("{}", err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn arg_conflicts_with_required_group() {
|
2022-02-14 21:47:20 +00:00
|
|
|
let mut cmd = Command::new("group_conflict")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(-f --flag "some flag").conflicts_with("gr"))
|
2020-05-14 20:50:56 +00:00
|
|
|
.group(ArgGroup::new("gr").required(true).arg("some").arg("other"))
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(--some "some arg"))
|
2021-12-22 19:13:11 +00:00
|
|
|
.arg(arg!(--other "other arg"));
|
|
|
|
|
2022-02-14 21:47:20 +00:00
|
|
|
let result = cmd.try_get_matches_from_mut(vec!["myprog", "--other", "-f"]);
|
2021-12-22 19:13:11 +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);
|
2021-12-22 19:13:11 +00:00
|
|
|
|
2022-02-14 21:47:20 +00:00
|
|
|
let result = cmd.try_get_matches_from_mut(vec!["myprog", "-f", "--some"]);
|
2015-09-06 10:46:58 +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);
|
2021-12-22 19:13:11 +00:00
|
|
|
|
2022-02-14 21:47:20 +00:00
|
|
|
let result = cmd.try_get_matches_from_mut(vec!["myprog", "--some"]);
|
2021-12-22 19:13:11 +00:00
|
|
|
if let Err(err) = result {
|
|
|
|
panic!("{}", err);
|
|
|
|
}
|
|
|
|
|
2022-02-14 21:47:20 +00:00
|
|
|
let result = cmd.try_get_matches_from_mut(vec!["myprog", "--other"]);
|
2021-12-22 19:13:11 +00:00
|
|
|
if let Err(err) = result {
|
|
|
|
panic!("{}", err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-22 19:08:02 +00:00
|
|
|
#[test]
|
|
|
|
fn arg_conflicts_with_group_with_required_memeber() {
|
|
|
|
let mut cmd = Command::new("group_conflict")
|
|
|
|
.arg(arg!(-f --flag "some flag").conflicts_with("gr"))
|
|
|
|
.group(ArgGroup::new("gr").arg("some").arg("other"))
|
|
|
|
.arg(arg!(--some "some arg").required(true))
|
|
|
|
.arg(arg!(--other "other arg"));
|
|
|
|
|
|
|
|
let result = cmd.try_get_matches_from_mut(vec!["myprog", "--other", "-f"]);
|
|
|
|
assert!(result.is_err());
|
|
|
|
let err = result.err().unwrap();
|
|
|
|
assert_eq!(err.kind(), ErrorKind::ArgumentConflict);
|
|
|
|
|
|
|
|
let result = cmd.try_get_matches_from_mut(vec!["myprog", "-f", "--some"]);
|
|
|
|
assert!(result.is_err());
|
|
|
|
let err = result.err().unwrap();
|
|
|
|
assert_eq!(err.kind(), ErrorKind::ArgumentConflict);
|
|
|
|
|
|
|
|
let result = cmd.try_get_matches_from_mut(vec!["myprog", "--some"]);
|
|
|
|
if let Err(err) = result {
|
|
|
|
panic!("{}", err);
|
|
|
|
}
|
|
|
|
|
|
|
|
let result = cmd.try_get_matches_from_mut(vec!["myprog", "--flag"]);
|
2022-12-22 19:09:04 +00:00
|
|
|
if let Err(err) = result {
|
|
|
|
panic!("{}", err);
|
|
|
|
}
|
2022-12-22 19:08:02 +00:00
|
|
|
}
|
|
|
|
|
2021-12-22 19:13:11 +00:00
|
|
|
#[test]
|
|
|
|
fn required_group_conflicts_with_arg() {
|
2022-02-14 21:47:20 +00:00
|
|
|
let mut cmd = Command::new("group_conflict")
|
2021-12-22 19:13:11 +00:00
|
|
|
.arg(arg!(-f --flag "some flag"))
|
|
|
|
.group(
|
|
|
|
ArgGroup::new("gr")
|
|
|
|
.required(true)
|
|
|
|
.arg("some")
|
|
|
|
.arg("other")
|
|
|
|
.conflicts_with("flag"),
|
|
|
|
)
|
|
|
|
.arg(arg!(--some "some arg"))
|
|
|
|
.arg(arg!(--other "other arg"));
|
|
|
|
|
2022-02-14 21:47:20 +00:00
|
|
|
let result = cmd.try_get_matches_from_mut(vec!["myprog", "--other", "-f"]);
|
2021-12-22 19:13:11 +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);
|
2021-12-22 19:13:11 +00:00
|
|
|
|
2022-02-14 21:47:20 +00:00
|
|
|
let result = cmd.try_get_matches_from_mut(vec!["myprog", "-f", "--some"]);
|
2021-12-22 19:13:11 +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);
|
2021-12-22 19:13:11 +00:00
|
|
|
|
2022-02-14 21:47:20 +00:00
|
|
|
let result = cmd.try_get_matches_from_mut(vec!["myprog", "--some"]);
|
2021-12-22 19:13:11 +00:00
|
|
|
if let Err(err) = result {
|
|
|
|
panic!("{}", err);
|
|
|
|
}
|
|
|
|
|
2022-02-14 21:47:20 +00:00
|
|
|
let result = cmd.try_get_matches_from_mut(vec!["myprog", "--other"]);
|
2021-12-22 19:13:11 +00:00
|
|
|
if let Err(err) = result {
|
|
|
|
panic!("{}", err);
|
|
|
|
}
|
2016-01-21 05:18:53 +00:00
|
|
|
}
|
2016-05-09 03:20:50 +00:00
|
|
|
|
2022-07-09 06:36:06 +00:00
|
|
|
#[test]
|
|
|
|
fn get_arg_conflicts_with_group() {
|
|
|
|
let flag = arg!(--flag).conflicts_with("gr");
|
|
|
|
let mut cmd = Command::new("group_conflict")
|
|
|
|
.arg(&flag)
|
|
|
|
.group(ArgGroup::new("gr").arg("some").arg("other"))
|
|
|
|
.arg(arg!(--some))
|
|
|
|
.arg(arg!(--other));
|
|
|
|
|
|
|
|
cmd.build();
|
|
|
|
|
|
|
|
let result = cmd.get_arg_conflicts_with(&flag);
|
|
|
|
|
|
|
|
assert_eq!(result.len(), 2);
|
|
|
|
assert_eq!(result[0].get_id(), "some");
|
|
|
|
assert_eq!(result[1].get_id(), "other");
|
|
|
|
}
|
|
|
|
|
2016-05-09 03:20:50 +00:00
|
|
|
#[test]
|
2022-09-19 14:59:04 +00:00
|
|
|
#[cfg(feature = "error-context")]
|
2016-05-09 03:20:50 +00:00
|
|
|
fn conflict_output() {
|
2022-09-07 20:29:15 +00:00
|
|
|
static CONFLICT_ERR: &str = "\
|
|
|
|
error: The argument '--flag...' cannot be used with '-F'
|
2022-07-29 14:51:28 +00:00
|
|
|
|
2022-09-07 16:03:55 +00:00
|
|
|
Usage: clap-test --flag... --long-option-2 <option2> <positional> <positional2> [positional3]...
|
2022-07-29 14:51:28 +00:00
|
|
|
|
2022-09-15 19:22:28 +00:00
|
|
|
For more information try '--help'
|
2022-07-29 14:51:28 +00:00
|
|
|
";
|
|
|
|
|
2022-04-29 20:32:25 +00:00
|
|
|
utils::assert_output(
|
2020-02-04 08:10:53 +00:00
|
|
|
utils::complex_app(),
|
2020-08-15 08:20:48 +00:00
|
|
|
"clap-test val1 fa --flag --long-option-2 val2 -F",
|
2018-01-25 04:05:05 +00:00
|
|
|
CONFLICT_ERR,
|
|
|
|
true,
|
2022-04-29 20:32:25 +00:00
|
|
|
);
|
2016-05-09 03:20:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2022-09-19 14:59:04 +00:00
|
|
|
#[cfg(feature = "error-context")]
|
2016-05-09 03:20:50 +00:00
|
|
|
fn conflict_output_rev() {
|
2022-09-07 20:29:15 +00:00
|
|
|
static CONFLICT_ERR_REV: &str = "\
|
|
|
|
error: The argument '-F' cannot be used with '--flag...'
|
2022-07-29 14:51:28 +00:00
|
|
|
|
2022-09-07 16:03:55 +00:00
|
|
|
Usage: clap-test -F --long-option-2 <option2> <positional> <positional2> [positional3]...
|
2022-07-29 14:51:28 +00:00
|
|
|
|
2022-09-15 19:22:28 +00:00
|
|
|
For more information try '--help'
|
2022-07-29 14:51:28 +00:00
|
|
|
";
|
|
|
|
|
2022-04-29 20:32:25 +00:00
|
|
|
utils::assert_output(
|
2020-02-04 08:10:53 +00:00
|
|
|
utils::complex_app(),
|
2020-08-15 08:20:48 +00:00
|
|
|
"clap-test val1 fa -F --long-option-2 val2 --flag",
|
2018-01-25 04:05:05 +00:00
|
|
|
CONFLICT_ERR_REV,
|
|
|
|
true,
|
2022-04-29 20:32:25 +00:00
|
|
|
);
|
2016-05-09 03:20:50 +00:00
|
|
|
}
|
2017-10-24 19:23:29 +00:00
|
|
|
|
2022-09-29 14:43:33 +00:00
|
|
|
#[test]
|
|
|
|
#[cfg(feature = "error-context")]
|
|
|
|
fn conflict_output_repeat() {
|
|
|
|
static ERR: &str = "\
|
2022-09-29 14:53:28 +00:00
|
|
|
error: The argument '-F' was provided more than once, but cannot be used multiple times
|
2022-09-29 14:43:33 +00:00
|
|
|
|
|
|
|
Usage: clap-test [OPTIONS] [positional] [positional2] [positional3]... [COMMAND]
|
|
|
|
|
|
|
|
For more information try '--help'
|
|
|
|
";
|
|
|
|
|
|
|
|
utils::assert_output(utils::complex_app(), "clap-test -F -F", ERR, true);
|
|
|
|
}
|
|
|
|
|
2020-08-15 08:20:48 +00:00
|
|
|
#[test]
|
2022-09-19 14:59:04 +00:00
|
|
|
#[cfg(feature = "error-context")]
|
2020-08-15 08:20:48 +00:00
|
|
|
fn conflict_output_with_required() {
|
2022-09-07 20:29:15 +00:00
|
|
|
static CONFLICT_ERR: &str = "\
|
|
|
|
error: The argument '--flag...' cannot be used with '-F'
|
2022-07-29 14:51:28 +00:00
|
|
|
|
2022-09-07 16:03:55 +00:00
|
|
|
Usage: clap-test --flag... --long-option-2 <option2> <positional> <positional2> [positional3]...
|
2022-07-29 14:51:28 +00:00
|
|
|
|
2022-09-15 19:22:28 +00:00
|
|
|
For more information try '--help'
|
2022-07-29 14:51:28 +00:00
|
|
|
";
|
|
|
|
|
2022-04-29 20:32:25 +00:00
|
|
|
utils::assert_output(
|
2020-08-15 08:20:48 +00:00
|
|
|
utils::complex_app(),
|
|
|
|
"clap-test val1 --flag --long-option-2 val2 -F",
|
|
|
|
CONFLICT_ERR,
|
|
|
|
true,
|
2022-04-29 20:32:25 +00:00
|
|
|
);
|
2020-08-15 08:20:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2022-09-19 14:59:04 +00:00
|
|
|
#[cfg(feature = "error-context")]
|
2020-08-15 08:20:48 +00:00
|
|
|
fn conflict_output_rev_with_required() {
|
2022-09-07 20:29:15 +00:00
|
|
|
static CONFLICT_ERR_REV: &str = "\
|
|
|
|
error: The argument '-F' cannot be used with '--flag...'
|
2022-07-29 14:51:28 +00:00
|
|
|
|
2022-09-07 16:03:55 +00:00
|
|
|
Usage: clap-test -F --long-option-2 <option2> <positional> <positional2> [positional3]...
|
2022-07-29 14:51:28 +00:00
|
|
|
|
2022-09-15 19:22:28 +00:00
|
|
|
For more information try '--help'
|
2022-07-29 14:51:28 +00:00
|
|
|
";
|
|
|
|
|
2022-04-29 20:32:25 +00:00
|
|
|
utils::assert_output(
|
2020-08-15 08:20:48 +00:00
|
|
|
utils::complex_app(),
|
|
|
|
"clap-test val1 -F --long-option-2 val2 --flag",
|
|
|
|
CONFLICT_ERR_REV,
|
|
|
|
true,
|
2022-04-29 20:32:25 +00:00
|
|
|
);
|
2020-08-15 08:20:48 +00:00
|
|
|
}
|
|
|
|
|
2020-08-15 09:43:12 +00:00
|
|
|
#[test]
|
2022-09-19 14:59:04 +00:00
|
|
|
#[cfg(feature = "error-context")]
|
2020-08-15 09:43:12 +00:00
|
|
|
fn conflict_output_three_conflicting() {
|
2022-09-07 20:29:15 +00:00
|
|
|
static CONFLICT_ERR_THREE: &str = "\
|
|
|
|
error: The argument '--one' cannot be used with:
|
|
|
|
--two
|
|
|
|
--three
|
2022-07-29 14:51:28 +00:00
|
|
|
|
2022-09-07 16:03:55 +00:00
|
|
|
Usage: three_conflicting_arguments --one
|
2022-07-29 14:51:28 +00:00
|
|
|
|
2022-09-15 19:22:28 +00:00
|
|
|
For more information try '--help'
|
2022-07-29 14:51:28 +00:00
|
|
|
";
|
|
|
|
|
2022-02-14 21:47:20 +00:00
|
|
|
let cmd = Command::new("three_conflicting_arguments")
|
2020-08-15 09:43:12 +00:00
|
|
|
.arg(
|
|
|
|
Arg::new("one")
|
|
|
|
.long("one")
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(ArgAction::SetTrue)
|
2022-08-12 17:40:58 +00:00
|
|
|
.conflicts_with_all(["two", "three"]),
|
2020-08-15 09:43:12 +00:00
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::new("two")
|
|
|
|
.long("two")
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(ArgAction::SetTrue)
|
2022-08-12 17:40:58 +00:00
|
|
|
.conflicts_with_all(["one", "three"]),
|
2020-08-15 09:43:12 +00:00
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::new("three")
|
|
|
|
.long("three")
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(ArgAction::SetTrue)
|
2022-08-12 17:40:58 +00:00
|
|
|
.conflicts_with_all(["one", "two"]),
|
2020-08-15 09:43:12 +00:00
|
|
|
);
|
2022-04-29 20:32:25 +00:00
|
|
|
utils::assert_output(
|
2022-02-14 21:47:20 +00:00
|
|
|
cmd,
|
2020-08-15 09:43:12 +00:00
|
|
|
"three_conflicting_arguments --one --two --three",
|
|
|
|
CONFLICT_ERR_THREE,
|
|
|
|
true,
|
2022-04-29 20:32:25 +00:00
|
|
|
);
|
2020-08-15 09:43:12 +00:00
|
|
|
}
|
|
|
|
|
2020-04-09 10:20:46 +00:00
|
|
|
#[test]
|
2022-09-19 14:59:04 +00:00
|
|
|
#[cfg(feature = "error-context")]
|
2020-04-09 10:20:46 +00:00
|
|
|
fn two_conflicting_arguments() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let a = Command::new("two_conflicting_arguments")
|
2020-04-09 10:20:46 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("develop")
|
2020-04-09 10:20:46 +00:00
|
|
|
.long("develop")
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(ArgAction::SetTrue)
|
2020-04-09 10:20:46 +00:00
|
|
|
.conflicts_with("production"),
|
|
|
|
)
|
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("production")
|
2020-04-09 10:20:46 +00:00
|
|
|
.long("production")
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(ArgAction::SetTrue)
|
2020-04-09 10:20:46 +00:00
|
|
|
.conflicts_with("develop"),
|
|
|
|
)
|
|
|
|
.try_get_matches_from(vec!["", "--develop", "--production"]);
|
|
|
|
|
|
|
|
assert!(a.is_err());
|
|
|
|
let a = a.unwrap_err();
|
2020-08-22 09:43:58 +00:00
|
|
|
assert!(
|
|
|
|
a.to_string()
|
2021-12-22 19:13:11 +00:00
|
|
|
.contains("The argument \'--develop\' cannot be used with \'--production\'"),
|
2020-08-22 09:43:58 +00:00
|
|
|
"{}",
|
|
|
|
a
|
2020-04-09 10:20:46 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2022-09-19 14:59:04 +00:00
|
|
|
#[cfg(feature = "error-context")]
|
2020-04-09 10:20:46 +00:00
|
|
|
fn three_conflicting_arguments() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let a = Command::new("three_conflicting_arguments")
|
2020-04-09 10:20:46 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("one")
|
2020-04-09 10:20:46 +00:00
|
|
|
.long("one")
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(ArgAction::SetTrue)
|
2022-08-12 17:40:58 +00:00
|
|
|
.conflicts_with_all(["two", "three"]),
|
2020-04-09 10:20:46 +00:00
|
|
|
)
|
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("two")
|
2020-04-09 10:20:46 +00:00
|
|
|
.long("two")
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(ArgAction::SetTrue)
|
2022-08-12 17:40:58 +00:00
|
|
|
.conflicts_with_all(["one", "three"]),
|
2020-04-09 10:20:46 +00:00
|
|
|
)
|
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("three")
|
2020-04-09 10:20:46 +00:00
|
|
|
.long("three")
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(ArgAction::SetTrue)
|
2022-08-12 17:40:58 +00:00
|
|
|
.conflicts_with_all(["one", "two"]),
|
2020-04-09 10:20:46 +00:00
|
|
|
)
|
|
|
|
.try_get_matches_from(vec!["", "--one", "--two", "--three"]);
|
|
|
|
|
|
|
|
assert!(a.is_err());
|
|
|
|
let a = a.unwrap_err();
|
2020-08-22 09:43:58 +00:00
|
|
|
assert!(
|
|
|
|
a.to_string()
|
2021-12-22 19:13:11 +00:00
|
|
|
.contains("The argument \'--one\' cannot be used with:"),
|
2020-08-22 09:43:58 +00:00
|
|
|
"{}",
|
|
|
|
a
|
2020-04-09 10:20:46 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
#[test]
|
2020-04-09 22:33:16 +00:00
|
|
|
#[should_panic = "Argument 'config' cannot conflict with itself"]
|
2020-04-09 10:20:46 +00:00
|
|
|
fn self_conflicting_arg() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let _ = Command::new("prog")
|
2020-05-14 20:50:56 +00:00
|
|
|
.arg(Arg::new("config").long("config").conflicts_with("config"))
|
2020-04-09 10:20:46 +00:00
|
|
|
.try_get_matches_from(vec!["", "--config"]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
#[test]
|
2020-04-24 19:34:23 +00:00
|
|
|
#[should_panic = "Argument or group 'extra' specified in 'conflicts_with*' for 'config' does not exist"]
|
2020-04-09 10:20:46 +00:00
|
|
|
fn conflicts_with_invalid_arg() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let _ = Command::new("prog")
|
2020-05-14 20:50:56 +00:00
|
|
|
.arg(Arg::new("config").long("config").conflicts_with("extra"))
|
2020-04-09 10:20:46 +00:00
|
|
|
.try_get_matches_from(vec!["", "--config"]);
|
|
|
|
}
|
2020-04-26 17:48:08 +00:00
|
|
|
|
|
|
|
#[test]
|
2021-10-25 17:36:01 +00:00
|
|
|
fn conflict_with_unused_default() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let result = Command::new("conflict")
|
2022-09-12 21:59:57 +00:00
|
|
|
.arg(arg!(-o --opt <opt> "some opt").default_value("default"))
|
2022-06-10 01:03:28 +00:00
|
|
|
.arg(
|
|
|
|
arg!(-f --flag "some flag")
|
|
|
|
.conflicts_with("opt")
|
|
|
|
.action(ArgAction::SetTrue),
|
|
|
|
)
|
2021-10-25 17:36:01 +00:00
|
|
|
.try_get_matches_from(vec!["myprog", "-f"]);
|
|
|
|
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(result.is_ok(), "{}", result.unwrap_err());
|
2021-10-25 17:36:01 +00:00
|
|
|
let m = result.unwrap();
|
|
|
|
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("opt").map(|v| v.as_str()),
|
|
|
|
Some("default")
|
|
|
|
);
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(*m.get_one::<bool>("flag").expect("defaulted by clap"));
|
2021-10-25 17:36:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn conflicts_with_alongside_default() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let result = Command::new("conflict")
|
2020-04-26 17:48:08 +00:00
|
|
|
.arg(
|
2021-11-19 20:33:11 +00:00
|
|
|
arg!(-o --opt <opt> "some opt")
|
2020-04-26 17:48:08 +00:00
|
|
|
.default_value("default")
|
|
|
|
.conflicts_with("flag"),
|
|
|
|
)
|
2022-06-10 01:03:28 +00:00
|
|
|
.arg(arg!(-f --flag "some flag").action(ArgAction::SetTrue))
|
2020-04-26 17:48:08 +00:00
|
|
|
.try_get_matches_from(vec!["myprog", "-f"]);
|
|
|
|
|
2021-10-29 17:27:33 +00:00
|
|
|
assert!(
|
|
|
|
result.is_ok(),
|
|
|
|
"conflicts_with should ignore default_value: {:?}",
|
|
|
|
result.unwrap_err()
|
|
|
|
);
|
2020-04-26 17:48:08 +00:00
|
|
|
let m = result.unwrap();
|
|
|
|
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("opt").map(|v| v.as_str()),
|
|
|
|
Some("default")
|
|
|
|
);
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(*m.get_one::<bool>("flag").expect("defaulted by clap"));
|
2020-04-26 17:48:08 +00:00
|
|
|
}
|
2022-02-08 03:45:30 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn group_in_conflicts_with() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let result = Command::new("conflict")
|
2022-02-08 03:45:30 +00:00
|
|
|
.arg(
|
|
|
|
Arg::new("opt")
|
|
|
|
.long("opt")
|
|
|
|
.default_value("default")
|
|
|
|
.group("one"),
|
|
|
|
)
|
2022-06-10 01:03:28 +00:00
|
|
|
.arg(
|
|
|
|
Arg::new("flag")
|
|
|
|
.long("flag")
|
|
|
|
.conflicts_with("one")
|
|
|
|
.action(ArgAction::SetTrue),
|
|
|
|
)
|
2022-02-08 03:45:30 +00:00
|
|
|
.try_get_matches_from(vec!["myprog", "--flag"]);
|
|
|
|
|
|
|
|
assert!(
|
|
|
|
result.is_ok(),
|
|
|
|
"conflicts_with on an arg group should ignore default_value: {:?}",
|
|
|
|
result.unwrap_err()
|
|
|
|
);
|
|
|
|
let m = result.unwrap();
|
|
|
|
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("opt").map(|v| v.as_str()),
|
|
|
|
Some("default")
|
|
|
|
);
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(*m.get_one::<bool>("flag").expect("defaulted by clap"));
|
2022-02-08 03:45:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn group_conflicts_with_default_value() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let result = Command::new("conflict")
|
2022-02-08 03:45:30 +00:00
|
|
|
.arg(
|
|
|
|
Arg::new("opt")
|
|
|
|
.long("opt")
|
|
|
|
.default_value("default")
|
|
|
|
.group("one"),
|
|
|
|
)
|
2022-06-10 01:03:28 +00:00
|
|
|
.arg(
|
|
|
|
Arg::new("flag")
|
|
|
|
.long("flag")
|
|
|
|
.group("one")
|
|
|
|
.action(ArgAction::SetTrue),
|
|
|
|
)
|
2022-02-08 03:45:30 +00:00
|
|
|
.try_get_matches_from(vec!["myprog", "--flag"]);
|
|
|
|
|
|
|
|
assert!(
|
|
|
|
result.is_ok(),
|
|
|
|
"arg group count should ignore default_value: {:?}",
|
|
|
|
result.unwrap_err()
|
|
|
|
);
|
|
|
|
let m = result.unwrap();
|
|
|
|
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("opt").map(|v| v.as_str()),
|
|
|
|
Some("default")
|
|
|
|
);
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(*m.get_one::<bool>("flag").expect("defaulted by clap"));
|
2022-02-08 03:45:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn group_conflicts_with_default_arg() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let result = Command::new("conflict")
|
2022-02-08 03:45:30 +00:00
|
|
|
.arg(Arg::new("opt").long("opt").default_value("default"))
|
2022-06-10 01:03:28 +00:00
|
|
|
.arg(
|
|
|
|
Arg::new("flag")
|
|
|
|
.long("flag")
|
|
|
|
.group("one")
|
|
|
|
.action(ArgAction::SetTrue),
|
|
|
|
)
|
2022-02-08 03:45:30 +00:00
|
|
|
.group(ArgGroup::new("one").conflicts_with("opt"))
|
|
|
|
.try_get_matches_from(vec!["myprog", "--flag"]);
|
|
|
|
|
|
|
|
assert!(
|
|
|
|
result.is_ok(),
|
|
|
|
"arg group conflicts_with should ignore default_value: {:?}",
|
|
|
|
result.unwrap_err()
|
|
|
|
);
|
|
|
|
let m = result.unwrap();
|
|
|
|
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("opt").map(|v| v.as_str()),
|
|
|
|
Some("default")
|
|
|
|
);
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(*m.get_one::<bool>("flag").expect("defaulted by clap"));
|
2022-02-08 03:45:30 +00:00
|
|
|
}
|
2022-05-04 17:23:35 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn exclusive_with_required() {
|
|
|
|
let cmd = Command::new("bug")
|
2022-07-26 00:17:01 +00:00
|
|
|
.arg(
|
|
|
|
Arg::new("test")
|
|
|
|
.long("test")
|
|
|
|
.action(ArgAction::SetTrue)
|
|
|
|
.exclusive(true),
|
|
|
|
)
|
|
|
|
.arg(Arg::new("input").action(ArgAction::Set).required(true));
|
2022-05-04 17:23:35 +00:00
|
|
|
|
|
|
|
cmd.clone()
|
|
|
|
.try_get_matches_from(["bug", "--test", "required"])
|
|
|
|
.unwrap_err();
|
|
|
|
|
|
|
|
cmd.clone()
|
|
|
|
.try_get_matches_from(["bug", "required"])
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
cmd.clone().try_get_matches_from(["bug", "--test"]).unwrap();
|
|
|
|
}
|
2022-07-22 19:10:27 +00:00
|
|
|
|
2022-12-21 02:16:15 +00:00
|
|
|
#[test]
|
|
|
|
fn args_negate_subcommands_one_level() {
|
|
|
|
let res = Command::new("disablehelp")
|
|
|
|
.args_conflicts_with_subcommands(true)
|
|
|
|
.subcommand_negates_reqs(true)
|
|
|
|
.arg(arg!(<arg1> "some arg"))
|
|
|
|
.arg(arg!(<arg2> "some arg"))
|
|
|
|
.subcommand(
|
|
|
|
Command::new("sub1").subcommand(Command::new("sub2").subcommand(Command::new("sub3"))),
|
|
|
|
)
|
|
|
|
.try_get_matches_from(vec!["", "pickles", "sub1"]);
|
|
|
|
assert!(res.is_ok(), "error: {:?}", res.unwrap_err().kind());
|
|
|
|
let m = res.unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("arg2").map(|v| v.as_str()),
|
|
|
|
Some("sub1")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn args_negate_subcommands_two_levels() {
|
|
|
|
let res = Command::new("disablehelp")
|
|
|
|
.args_conflicts_with_subcommands(true)
|
|
|
|
.subcommand_negates_reqs(true)
|
|
|
|
.arg(arg!(<arg1> "some arg"))
|
|
|
|
.arg(arg!(<arg2> "some arg"))
|
|
|
|
.subcommand(
|
|
|
|
Command::new("sub1")
|
|
|
|
.args_conflicts_with_subcommands(true)
|
|
|
|
.subcommand_negates_reqs(true)
|
|
|
|
.arg(arg!(<arg> "some"))
|
|
|
|
.arg(arg!(<arg2> "some"))
|
|
|
|
.subcommand(Command::new("sub2").subcommand(Command::new("sub3"))),
|
|
|
|
)
|
|
|
|
.try_get_matches_from(vec!["", "sub1", "arg", "sub2"]);
|
|
|
|
assert!(res.is_ok(), "error: {:?}", res.unwrap_err().kind());
|
|
|
|
let m = res.unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
m.subcommand_matches("sub1")
|
|
|
|
.unwrap()
|
|
|
|
.get_one::<String>("arg2")
|
|
|
|
.map(|v| v.as_str()),
|
|
|
|
Some("sub2")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-12-21 02:23:35 +00:00
|
|
|
#[test]
|
|
|
|
#[cfg(feature = "error-context")]
|
|
|
|
fn subcommand_conflict_error_message() {
|
|
|
|
static CONFLICT_ERR: &str = "\
|
2022-12-21 02:27:55 +00:00
|
|
|
error: Found argument 'sub1' which wasn't expected, or isn't valid in this context
|
2022-12-21 02:23:35 +00:00
|
|
|
|
|
|
|
Usage: test [OPTIONS]
|
|
|
|
test <COMMAND>
|
|
|
|
|
|
|
|
For more information try '--help'
|
|
|
|
";
|
|
|
|
|
|
|
|
let cmd = Command::new("test")
|
|
|
|
.args_conflicts_with_subcommands(true)
|
|
|
|
.arg(arg!(-p --place <"place id"> "Place ID to open"))
|
|
|
|
.subcommand(
|
|
|
|
Command::new("sub1").subcommand(Command::new("sub2").subcommand(Command::new("sub3"))),
|
|
|
|
);
|
|
|
|
|
|
|
|
utils::assert_output(cmd, "test --place id sub1", CONFLICT_ERR, true);
|
|
|
|
}
|
|
|
|
|
2022-07-22 19:10:27 +00:00
|
|
|
#[test]
|
|
|
|
fn subcommand_conflict_negates_required() {
|
|
|
|
let cmd = Command::new("test")
|
|
|
|
.args_conflicts_with_subcommands(true)
|
|
|
|
.subcommand(Command::new("config"))
|
2022-09-12 21:59:57 +00:00
|
|
|
.arg(arg!(-p --place <"place id"> "Place ID to open").required(true));
|
2022-07-22 19:10:27 +00:00
|
|
|
|
|
|
|
let result = cmd.try_get_matches_from(["test", "config"]);
|
|
|
|
assert!(
|
|
|
|
result.is_ok(),
|
|
|
|
"args_conflicts_with_subcommands should ignore required: {}",
|
|
|
|
result.unwrap_err()
|
|
|
|
);
|
|
|
|
let m = result.unwrap();
|
|
|
|
assert_eq!(m.subcommand_name().unwrap(), "config");
|
|
|
|
}
|