2022-08-11 14:50:58 -05:00
|
|
|
use clap::{arg, error::ErrorKind, Arg, ArgAction, ArgGroup, Command, Id};
|
2015-09-27 22:48:15 -04:00
|
|
|
|
2022-09-19 09:59:04 -05:00
|
|
|
use super::utils;
|
2017-01-02 23:05:23 -05:00
|
|
|
|
2015-09-27 22:48:15 -04:00
|
|
|
#[test]
|
|
|
|
fn required_group_missing_arg() {
|
2022-02-11 21:48:29 -06:00
|
|
|
let result = Command::new("group")
|
2021-11-19 14:33:11 -06:00
|
|
|
.arg(arg!(-f --flag "some flag"))
|
|
|
|
.arg(arg!( -c --color "some other flag"))
|
2022-08-12 12:40:58 -05:00
|
|
|
.group(ArgGroup::new("req").args(["flag", "color"]).required(true))
|
2018-10-19 16:42:13 -04:00
|
|
|
.try_get_matches_from(vec![""]);
|
2015-09-28 23:23:17 +03:00
|
|
|
assert!(result.is_err());
|
|
|
|
let err = result.err().unwrap();
|
2022-01-25 16:19:28 -06:00
|
|
|
assert_eq!(err.kind(), ErrorKind::MissingRequiredArgument);
|
2015-09-27 22:48:15 -04:00
|
|
|
}
|
|
|
|
|
2020-04-09 16:21:45 +02:00
|
|
|
#[cfg(debug_assertions)]
|
2017-05-15 18:29:30 -04:00
|
|
|
#[test]
|
2022-08-11 15:33:23 -05:00
|
|
|
#[should_panic = "Command group: Argument group 'req' contains non-existent argument"]
|
2017-05-15 18:29:30 -04:00
|
|
|
fn non_existing_arg() {
|
2022-02-11 21:48:29 -06:00
|
|
|
let _ = Command::new("group")
|
2021-11-19 14:33:11 -06:00
|
|
|
.arg(arg!(-f --flag "some flag"))
|
|
|
|
.arg(arg!(-c --color "some other flag"))
|
2022-08-12 12:40:58 -05:00
|
|
|
.group(ArgGroup::new("req").args(["flg", "color"]).required(true))
|
2018-10-19 16:42:13 -04:00
|
|
|
.try_get_matches_from(vec![""]);
|
2017-05-15 18:29:30 -04:00
|
|
|
}
|
|
|
|
|
2020-04-09 16:21:45 +02:00
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
#[test]
|
2022-08-11 15:33:23 -05:00
|
|
|
#[should_panic = "Command group: Argument group name must be unique\n\n\t'req' is already in use"]
|
2020-04-09 16:21:45 +02:00
|
|
|
fn unique_group_name() {
|
2022-02-11 21:48:29 -06:00
|
|
|
let _ = Command::new("group")
|
2021-11-19 14:33:11 -06:00
|
|
|
.arg(arg!(-f --flag "some flag"))
|
|
|
|
.arg(arg!(-c --color "some other flag"))
|
2022-08-12 12:40:58 -05:00
|
|
|
.group(ArgGroup::new("req").args(["flag"]).required(true))
|
|
|
|
.group(ArgGroup::new("req").args(["color"]).required(true))
|
2020-04-09 16:21:45 +02:00
|
|
|
.try_get_matches_from(vec![""]);
|
|
|
|
}
|
|
|
|
|
2020-04-09 18:19:05 +02:00
|
|
|
#[cfg(debug_assertions)]
|
2020-04-09 16:21:45 +02:00
|
|
|
#[test]
|
2022-08-11 15:33:23 -05:00
|
|
|
#[should_panic = "Command group: Argument group name 'a' must not conflict with argument name"]
|
2020-05-14 22:50:56 +02:00
|
|
|
fn groups_new_of_arg_name() {
|
2022-02-11 21:48:29 -06:00
|
|
|
let _ = Command::new("group")
|
2020-05-14 22:50:56 +02:00
|
|
|
.arg(Arg::new("a").long("a").group("a"))
|
2020-04-09 16:21:45 +02:00
|
|
|
.try_get_matches_from(vec!["", "--a"]);
|
|
|
|
}
|
|
|
|
|
2020-04-09 18:19:05 +02:00
|
|
|
#[cfg(debug_assertions)]
|
2020-04-09 16:21:45 +02:00
|
|
|
#[test]
|
2022-08-12 12:40:58 -05:00
|
|
|
#[should_panic = "Command group: Argument group name 'a' must not conflict with argument name"]
|
2020-05-14 22:50:56 +02:00
|
|
|
fn arg_group_new_of_arg_name() {
|
2022-02-11 21:48:29 -06:00
|
|
|
let _ = Command::new("group")
|
2020-05-14 22:50:56 +02:00
|
|
|
.arg(Arg::new("a").long("a").group("a"))
|
|
|
|
.group(ArgGroup::new("a"))
|
2020-04-09 16:21:45 +02:00
|
|
|
.try_get_matches_from(vec!["", "--a"]);
|
|
|
|
}
|
|
|
|
|
2015-09-27 22:48:15 -04:00
|
|
|
#[test]
|
|
|
|
fn group_single_value() {
|
2022-02-11 21:48:29 -06:00
|
|
|
let res = Command::new("group")
|
2021-11-19 14:33:11 -06:00
|
|
|
.arg(arg!(-c --color [color] "some option"))
|
2022-09-12 16:59:57 -05:00
|
|
|
.arg(arg!(-n --hostname <name> "another option"))
|
2022-08-12 12:40:58 -05:00
|
|
|
.group(ArgGroup::new("grp").args(["hostname", "color"]))
|
2018-10-19 16:42:13 -04:00
|
|
|
.try_get_matches_from(vec!["", "-c", "blue"]);
|
2021-12-27 13:57:38 -06:00
|
|
|
assert!(res.is_ok(), "{}", res.unwrap_err());
|
2018-01-09 10:24:24 -05:00
|
|
|
|
|
|
|
let m = res.unwrap();
|
2022-06-09 20:03:28 -05:00
|
|
|
assert!(m.contains_id("grp"));
|
2022-08-11 14:50:58 -05:00
|
|
|
assert_eq!(m.get_one::<Id>("grp").map(|v| v.as_str()).unwrap(), "color");
|
2015-11-09 08:57:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn group_empty() {
|
2022-02-11 21:48:29 -06:00
|
|
|
let res = Command::new("group")
|
2022-10-12 07:39:55 -05:00
|
|
|
.arg(arg!(-f --flag "some flag"))
|
2021-11-19 14:33:11 -06:00
|
|
|
.arg(arg!(-c --color [color] "some option"))
|
2022-09-12 16:59:57 -05:00
|
|
|
.arg(arg!(-n --hostname <name> "another option"))
|
2022-10-12 07:39:55 -05:00
|
|
|
.group(ArgGroup::new("grp").args(["hostname", "color", "flag"]))
|
2018-10-19 16:42:13 -04:00
|
|
|
.try_get_matches_from(vec![""]);
|
2021-12-27 13:57:38 -06:00
|
|
|
assert!(res.is_ok(), "{}", res.unwrap_err());
|
2018-01-09 10:24:24 -05:00
|
|
|
|
|
|
|
let m = res.unwrap();
|
2022-06-09 20:03:28 -05:00
|
|
|
assert!(!m.contains_id("grp"));
|
2022-05-24 10:16:50 -05:00
|
|
|
assert!(m.get_one::<String>("grp").map(|v| v.as_str()).is_none());
|
2015-09-27 22:48:15 -04:00
|
|
|
}
|
|
|
|
|
2015-11-09 08:57:20 -05:00
|
|
|
#[test]
|
2022-07-21 16:36:54 -05:00
|
|
|
fn group_required_flags_empty() {
|
2022-02-11 21:48:29 -06:00
|
|
|
let result = Command::new("group")
|
2022-10-12 07:39:55 -05:00
|
|
|
.arg(arg!(-f --flag "some flag"))
|
2021-11-19 14:33:11 -06:00
|
|
|
.arg(arg!(-c --color "some option"))
|
2022-09-12 16:59:57 -05:00
|
|
|
.arg(arg!(-n --hostname <name> "another option"))
|
2022-07-21 16:36:54 -05:00
|
|
|
.group(
|
|
|
|
ArgGroup::new("grp")
|
|
|
|
.required(true)
|
2022-10-12 07:39:55 -05:00
|
|
|
.args(["hostname", "color", "flag"]),
|
2022-07-21 16:36:54 -05:00
|
|
|
)
|
2018-10-19 16:42:13 -04:00
|
|
|
.try_get_matches_from(vec![""]);
|
2015-11-09 08:57:20 -05:00
|
|
|
assert!(result.is_err());
|
|
|
|
let err = result.err().unwrap();
|
2022-01-25 16:19:28 -06:00
|
|
|
assert_eq!(err.kind(), ErrorKind::MissingRequiredArgument);
|
2015-11-09 08:57:20 -05:00
|
|
|
}
|
|
|
|
|
2015-09-27 22:48:15 -04:00
|
|
|
#[test]
|
|
|
|
fn group_multi_value_single_arg() {
|
2022-02-11 21:48:29 -06:00
|
|
|
let res = Command::new("group")
|
2022-10-12 07:39:55 -05:00
|
|
|
.arg(arg!(-f --flag "some flag"))
|
2022-08-03 11:20:07 -05:00
|
|
|
.arg(arg!(-c --color <color> "some option").num_args(1..))
|
2022-09-12 16:59:57 -05:00
|
|
|
.arg(arg!(-n --hostname <name> "another option"))
|
2022-10-12 07:39:55 -05:00
|
|
|
.group(ArgGroup::new("grp").args(["hostname", "color", "flag"]))
|
2018-10-19 16:42:13 -04:00
|
|
|
.try_get_matches_from(vec!["", "-c", "blue", "red", "green"]);
|
2022-01-25 16:19:28 -06:00
|
|
|
assert!(res.is_ok(), "{:?}", res.unwrap_err().kind());
|
2018-01-09 10:24:24 -05:00
|
|
|
|
|
|
|
let m = res.unwrap();
|
2022-06-09 20:03:28 -05:00
|
|
|
assert!(m.contains_id("grp"));
|
2022-08-11 14:50:58 -05:00
|
|
|
assert_eq!(m.get_one::<Id>("grp").map(|v| v.as_str()).unwrap(), "color");
|
2016-01-25 15:56:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn empty_group() {
|
2022-02-11 21:48:29 -06:00
|
|
|
let r = Command::new("empty_group")
|
2021-11-19 14:33:11 -06:00
|
|
|
.arg(arg!(-f --flag "some flag"))
|
2020-05-14 22:50:56 +02:00
|
|
|
.group(ArgGroup::new("vers").required(true))
|
2018-10-19 16:42:13 -04:00
|
|
|
.try_get_matches_from(vec!["empty_prog"]);
|
2016-04-09 22:31:52 -07:00
|
|
|
assert!(r.is_err());
|
|
|
|
let err = r.err().unwrap();
|
2022-01-25 16:19:28 -06:00
|
|
|
assert_eq!(err.kind(), ErrorKind::MissingRequiredArgument);
|
2015-09-27 22:48:15 -04:00
|
|
|
}
|
2016-05-08 21:33:56 -04:00
|
|
|
|
|
|
|
#[test]
|
2022-09-19 09:59:04 -05:00
|
|
|
#[cfg(feature = "error-context")]
|
2016-05-08 21:33:56 -04:00
|
|
|
fn req_group_usage_string() {
|
2022-09-19 09:59:04 -05:00
|
|
|
static REQ_GROUP_USAGE: &str = "error: The following required arguments were not provided:
|
|
|
|
<base|--delete>
|
|
|
|
|
|
|
|
Usage: clap-test <base|--delete>
|
|
|
|
|
|
|
|
For more information try '--help'
|
|
|
|
";
|
|
|
|
|
2022-02-14 15:47:20 -06:00
|
|
|
let cmd = Command::new("req_group")
|
2021-11-19 14:33:11 -06:00
|
|
|
.arg(arg!([base] "Base commit"))
|
|
|
|
.arg(arg!(
|
|
|
|
-d --delete "Remove the base commit information"
|
2021-11-18 14:17:31 -06:00
|
|
|
))
|
2018-01-24 23:05:05 -05:00
|
|
|
.group(
|
2020-05-14 22:50:56 +02:00
|
|
|
ArgGroup::new("base_or_delete")
|
2022-08-12 12:40:58 -05:00
|
|
|
.args(["base", "delete"])
|
2018-01-24 23:05:05 -05:00
|
|
|
.required(true),
|
|
|
|
);
|
|
|
|
|
2022-04-29 15:32:25 -05:00
|
|
|
utils::assert_output(cmd, "clap-test", REQ_GROUP_USAGE, true);
|
2016-05-08 21:33:56 -04:00
|
|
|
}
|
2016-05-09 16:00:44 -04:00
|
|
|
|
|
|
|
#[test]
|
2022-09-19 09:59:04 -05:00
|
|
|
#[cfg(feature = "error-context")]
|
2016-05-09 16:00:44 -04:00
|
|
|
fn req_group_with_conflict_usage_string() {
|
2022-09-19 09:59:04 -05:00
|
|
|
static REQ_GROUP_CONFLICT_USAGE: &str = "\
|
|
|
|
error: The argument '--delete' cannot be used with '[base]'
|
|
|
|
|
|
|
|
Usage: clap-test <base|--delete>
|
|
|
|
|
|
|
|
For more information try '--help'
|
|
|
|
";
|
|
|
|
|
2022-02-14 15:47:20 -06:00
|
|
|
let cmd = Command::new("req_group")
|
2021-11-19 14:33:11 -06:00
|
|
|
.arg(arg!([base] "Base commit").conflicts_with("delete"))
|
|
|
|
.arg(arg!(
|
|
|
|
-d --delete "Remove the base commit information"
|
2018-01-24 23:05:05 -05:00
|
|
|
))
|
|
|
|
.group(
|
2020-05-14 22:50:56 +02:00
|
|
|
ArgGroup::new("base_or_delete")
|
2022-08-12 12:40:58 -05:00
|
|
|
.args(["base", "delete"])
|
2018-01-24 23:05:05 -05:00
|
|
|
.required(true),
|
|
|
|
);
|
|
|
|
|
2022-04-29 15:32:25 -05:00
|
|
|
utils::assert_output(
|
2022-02-14 15:47:20 -06:00
|
|
|
cmd,
|
2018-01-24 23:05:05 -05:00
|
|
|
"clap-test --delete base",
|
2020-04-23 20:56:42 +03:00
|
|
|
REQ_GROUP_CONFLICT_USAGE,
|
2022-04-29 15:32:25 -05:00
|
|
|
true,
|
|
|
|
);
|
2020-04-23 20:56:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2022-09-19 09:59:04 -05:00
|
|
|
#[cfg(feature = "error-context")]
|
2020-04-23 20:56:42 +03:00
|
|
|
fn req_group_with_conflict_usage_string_only_options() {
|
2022-09-19 09:59:04 -05:00
|
|
|
static REQ_GROUP_CONFLICT_ONLY_OPTIONS: &str = "\
|
|
|
|
error: The argument '--delete' cannot be used with '--all'
|
|
|
|
|
|
|
|
Usage: clap-test <--all|--delete>
|
|
|
|
|
|
|
|
For more information try '--help'
|
|
|
|
";
|
|
|
|
|
2022-02-14 15:47:20 -06:00
|
|
|
let cmd = Command::new("req_group")
|
2021-11-19 14:33:11 -06:00
|
|
|
.arg(arg!(-a --all "All").conflicts_with("delete"))
|
|
|
|
.arg(arg!(
|
|
|
|
-d --delete "Remove the base commit information"
|
2020-04-23 20:56:42 +03:00
|
|
|
))
|
|
|
|
.group(
|
2020-05-14 22:50:56 +02:00
|
|
|
ArgGroup::new("all_or_delete")
|
2022-08-12 12:40:58 -05:00
|
|
|
.args(["all", "delete"])
|
2020-04-23 20:56:42 +03:00
|
|
|
.required(true),
|
|
|
|
);
|
2022-04-29 15:32:25 -05:00
|
|
|
utils::assert_output(
|
2022-02-14 15:47:20 -06:00
|
|
|
cmd,
|
2020-04-23 20:56:42 +03:00
|
|
|
"clap-test --delete --all",
|
|
|
|
REQ_GROUP_CONFLICT_ONLY_OPTIONS,
|
2022-04-29 15:32:25 -05:00
|
|
|
true,
|
|
|
|
);
|
2016-05-09 16:00:44 -04:00
|
|
|
}
|
2016-06-23 10:06:19 -04:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn required_group_multiple_args() {
|
2022-02-11 21:48:29 -06:00
|
|
|
let result = Command::new("group")
|
2022-06-09 20:03:28 -05:00
|
|
|
.arg(arg!(-f --flag "some flag").action(ArgAction::SetTrue))
|
|
|
|
.arg(arg!(-c --color "some other flag").action(ArgAction::SetTrue))
|
2018-01-24 23:05:05 -05:00
|
|
|
.group(
|
2020-05-14 22:50:56 +02:00
|
|
|
ArgGroup::new("req")
|
2022-08-12 12:40:58 -05:00
|
|
|
.args(["flag", "color"])
|
2018-01-24 23:05:05 -05:00
|
|
|
.required(true)
|
|
|
|
.multiple(true),
|
|
|
|
)
|
2018-10-19 16:42:13 -04:00
|
|
|
.try_get_matches_from(vec!["group", "-f", "-c"]);
|
2021-12-27 13:57:38 -06:00
|
|
|
assert!(result.is_ok(), "{}", result.unwrap_err());
|
2016-06-23 10:06:19 -04:00
|
|
|
let m = result.unwrap();
|
2022-06-09 20:03:28 -05:00
|
|
|
assert!(*m.get_one::<bool>("flag").expect("defaulted by clap"));
|
|
|
|
assert!(*m.get_one::<bool>("color").expect("defaulted by clap"));
|
2022-08-11 14:50:58 -05:00
|
|
|
assert_eq!(
|
|
|
|
&*m.get_many::<Id>("req")
|
|
|
|
.unwrap()
|
|
|
|
.map(|v| v.as_str())
|
|
|
|
.collect::<Vec<_>>(),
|
2022-11-24 07:54:25 -06:00
|
|
|
["flag", "color"]
|
2022-08-11 14:50:58 -05:00
|
|
|
);
|
2016-06-23 10:06:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn group_multiple_args_error() {
|
2022-02-11 21:48:29 -06:00
|
|
|
let result = Command::new("group")
|
2021-11-19 14:33:11 -06:00
|
|
|
.arg(arg!(-f --flag "some flag"))
|
|
|
|
.arg(arg!(-c --color "some other flag"))
|
2022-08-12 12:40:58 -05:00
|
|
|
.group(ArgGroup::new("req").args(["flag", "color"]))
|
2018-10-19 16:42:13 -04:00
|
|
|
.try_get_matches_from(vec!["group", "-f", "-c"]);
|
2016-06-23 10:06:19 -04:00
|
|
|
assert!(result.is_err());
|
|
|
|
let err = result.unwrap_err();
|
2022-01-25 16:19:28 -06:00
|
|
|
assert_eq!(err.kind(), ErrorKind::ArgumentConflict);
|
2016-06-24 00:17:04 -04:00
|
|
|
}
|
2019-04-04 22:06:23 -04:00
|
|
|
|
2022-04-22 07:01:53 -05:00
|
|
|
#[test]
|
|
|
|
fn group_overrides_required() {
|
|
|
|
let command = Command::new("group")
|
2022-09-12 16:59:57 -05:00
|
|
|
.arg(arg!(--foo <FOO>).required(true))
|
|
|
|
.arg(arg!(--bar <BAR>).required(true))
|
2022-08-12 12:40:58 -05:00
|
|
|
.group(ArgGroup::new("group").args(["foo", "bar"]).required(true));
|
2022-04-22 07:01:53 -05:00
|
|
|
let result = command.try_get_matches_from(vec!["group", "--foo", "value"]);
|
|
|
|
assert!(result.is_ok(), "{}", result.unwrap_err());
|
|
|
|
let m = result.unwrap();
|
2022-06-09 20:03:28 -05:00
|
|
|
assert!(m.contains_id("foo"));
|
|
|
|
assert!(!m.contains_id("bar"));
|
2022-04-22 07:01:53 -05:00
|
|
|
}
|
|
|
|
|
2021-01-24 19:02:43 +00:00
|
|
|
#[test]
|
|
|
|
fn group_usage_use_val_name() {
|
2022-08-31 09:29:00 -05:00
|
|
|
static GROUP_USAGE_USE_VAL_NAME: &str = "\
|
2022-09-07 11:03:55 -05:00
|
|
|
Usage: prog <A>
|
2021-01-24 19:02:43 +00:00
|
|
|
|
2022-08-26 09:40:23 -05:00
|
|
|
Arguments:
|
2022-09-07 15:29:15 -05:00
|
|
|
[A]
|
2021-01-24 19:02:43 +00:00
|
|
|
|
2022-08-26 09:40:23 -05:00
|
|
|
Options:
|
2022-09-07 15:29:15 -05:00
|
|
|
-h, --help Print help information
|
2021-09-24 11:58:39 -04:00
|
|
|
";
|
2022-02-14 15:47:20 -06:00
|
|
|
let cmd = Command::new("prog")
|
2021-01-24 19:02:43 +00:00
|
|
|
.arg(Arg::new("a").value_name("A"))
|
|
|
|
.group(ArgGroup::new("group").arg("a").required(true));
|
2022-04-29 15:32:25 -05:00
|
|
|
utils::assert_output(cmd, "prog --help", GROUP_USAGE_USE_VAL_NAME, false);
|
2021-01-24 19:02:43 +00:00
|
|
|
}
|
|
|
|
|
2019-04-04 22:06:23 -04:00
|
|
|
#[test]
|
|
|
|
fn group_acts_like_arg() {
|
2022-02-11 21:48:29 -06:00
|
|
|
let result = Command::new("prog")
|
2022-07-25 19:17:01 -05:00
|
|
|
.arg(
|
|
|
|
Arg::new("debug")
|
|
|
|
.long("debug")
|
|
|
|
.group("mode")
|
|
|
|
.action(ArgAction::SetTrue),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::new("verbose")
|
|
|
|
.long("verbose")
|
|
|
|
.group("mode")
|
|
|
|
.action(ArgAction::SetTrue),
|
|
|
|
)
|
2021-10-25 18:36:01 +01:00
|
|
|
.try_get_matches_from(vec!["prog", "--debug"]);
|
|
|
|
|
2021-12-27 13:57:38 -06:00
|
|
|
assert!(result.is_ok(), "{}", result.unwrap_err());
|
2021-10-25 18:36:01 +01:00
|
|
|
let m = result.unwrap();
|
2022-06-09 20:03:28 -05:00
|
|
|
assert!(m.contains_id("mode"));
|
2022-09-29 16:53:52 -05:00
|
|
|
assert_eq!(m.get_one::<clap::Id>("mode").unwrap(), "debug");
|
2019-04-05 15:51:22 -04:00
|
|
|
}
|
2020-04-23 20:19:11 +03:00
|
|
|
|
2022-09-29 19:34:11 -05:00
|
|
|
#[test]
|
2022-09-30 10:38:24 -05:00
|
|
|
fn conflict_with_overlapping_group_in_error() {
|
2022-09-29 19:34:11 -05:00
|
|
|
static ERR: &str = "\
|
|
|
|
error: The argument '--major' cannot be used with '--minor'
|
|
|
|
|
2022-09-30 10:38:24 -05:00
|
|
|
Usage: prog --major
|
2022-09-29 19:34:11 -05:00
|
|
|
|
|
|
|
For more information try '--help'
|
|
|
|
";
|
|
|
|
|
|
|
|
let cmd = Command::new("prog")
|
|
|
|
.group(ArgGroup::new("all").multiple(true))
|
|
|
|
.arg(arg!(--major).group("vers").group("all"))
|
|
|
|
.arg(arg!(--minor).group("vers").group("all"))
|
|
|
|
.arg(arg!(--other).group("all"));
|
|
|
|
|
|
|
|
utils::assert_output(cmd, "prog --major --minor", ERR, true);
|
|
|
|
}
|
|
|
|
|
2022-09-30 11:02:44 -05:00
|
|
|
#[test]
|
|
|
|
fn requires_group_with_overlapping_group_in_error() {
|
|
|
|
static ERR: &str = "\
|
|
|
|
error: The following required arguments were not provided:
|
|
|
|
<--in|--spec>
|
|
|
|
|
2022-09-30 11:18:47 -05:00
|
|
|
Usage: prog --config <--in|--spec>
|
2022-09-30 11:02:44 -05:00
|
|
|
|
|
|
|
For more information try '--help'
|
|
|
|
";
|
|
|
|
|
|
|
|
let cmd = Command::new("prog")
|
|
|
|
.group(ArgGroup::new("all").multiple(true))
|
|
|
|
.group(ArgGroup::new("input").required(true))
|
|
|
|
.arg(arg!(--in).group("input").group("all"))
|
|
|
|
.arg(arg!(--spec).group("input").group("all"))
|
|
|
|
.arg(arg!(--config).requires("input").group("all"));
|
|
|
|
|
|
|
|
utils::assert_output(cmd, "prog --config", ERR, true);
|
|
|
|
}
|
|
|
|
|
2020-12-09 00:56:35 +08:00
|
|
|
/* This is used to be fixed in a hack, we need to find a better way to fix it.
|
2020-04-23 20:19:11 +03:00
|
|
|
#[test]
|
|
|
|
fn issue_1794() {
|
2022-02-14 15:47:20 -06:00
|
|
|
let cmd = clap::Command::new("hello")
|
2020-04-23 20:19:11 +03:00
|
|
|
.bin_name("deno")
|
2022-07-29 16:36:03 -05:00
|
|
|
.arg(Arg::new("option1").long("option1").action(ArgAction::SetTrue))
|
2022-07-25 19:17:01 -05:00
|
|
|
.arg(Arg::new("pos1").action(ArgAction::Set))
|
|
|
|
.arg(Arg::new("pos2").action(ArgAction::Set))
|
2020-04-23 20:19:11 +03:00
|
|
|
.group(
|
2020-05-14 22:50:56 +02:00
|
|
|
ArgGroup::new("arg1")
|
2022-08-12 12:40:58 -05:00
|
|
|
.args(["pos1", "option1"])
|
2020-04-23 20:19:11 +03:00
|
|
|
.required(true),
|
|
|
|
);
|
|
|
|
|
2022-11-24 07:54:25 -06:00
|
|
|
let m = cmd.clone().try_get_matches_from(["cmd", "pos1", "pos2"]).unwrap();
|
2022-05-24 10:16:50 -05:00
|
|
|
assert_eq!(m.get_one::<String>("pos1").map(|v| v.as_str()), Some("pos1"));
|
|
|
|
assert_eq!(m.get_one::<String>("pos2").map(|v| v.as_str()), Some("pos2"));
|
2022-06-09 20:03:28 -05:00
|
|
|
assert!(!*m.get_one::<bool>("option1").expect("defaulted by clap"));
|
2020-04-23 20:19:11 +03:00
|
|
|
|
2022-02-14 15:47:20 -06:00
|
|
|
let m = cmd
|
2020-04-23 20:19:11 +03:00
|
|
|
.clone()
|
2022-11-24 07:54:25 -06:00
|
|
|
.try_get_matches_from(["cmd", "--option1", "positional"]).unwrap();
|
2022-05-24 10:16:50 -05:00
|
|
|
assert_eq!(m.get_one::<String>("pos1").map(|v| v.as_str()), None);
|
|
|
|
assert_eq!(m.get_one::<String>("pos2").map(|v| v.as_str()), Some("positional"));
|
2022-06-09 20:03:28 -05:00
|
|
|
assert!(*m.get_one::<bool>("option1").expect("defaulted by clap"));
|
2020-04-23 20:19:11 +03:00
|
|
|
}
|
2020-12-09 00:56:35 +08:00
|
|
|
*/
|