Merge pull request #3690 from epage/exclusive

fix(parser): Exclusive overrides required
This commit is contained in:
Ed Page 2022-05-04 13:26:32 -05:00 committed by GitHub
commit 4da6374727
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 1 deletions

View file

@ -223,6 +223,11 @@ impl<'help, 'cmd> Validator<'help, 'cmd> {
debug!("Validator::validate_exclusive");
// Not bothering to filter for `check_explicit` since defaults shouldn't play into this
let args_count = matcher.arg_names().count();
if args_count <= 1 {
// Nothing present to conflict with
return Ok(());
}
matcher
.arg_names()
.filter_map(|name| {
@ -474,11 +479,22 @@ impl<'help, 'cmd> Validator<'help, 'cmd> {
debug!("Validator::validate_required: required={:?}", self.required);
self.gather_requires(matcher);
let is_exclusive_present = matcher.arg_names().any(|name| {
self.cmd
.find(name)
.map(|arg| arg.is_exclusive_set())
.unwrap_or_default()
});
debug!(
"Validator::validate_required: is_exclusive_present={}",
is_exclusive_present
);
for arg_or_group in self.required.iter().filter(|r| !matcher.contains(r)) {
debug!("Validator::validate_required:iter:aog={:?}", arg_or_group);
if let Some(arg) = self.cmd.find(arg_or_group) {
debug!("Validator::validate_required:iter: This is an arg");
if !self.is_missing_required_ok(arg, matcher, conflicts) {
if !is_exclusive_present && !self.is_missing_required_ok(arg, matcher, conflicts) {
return self.missing_required_error(matcher, vec![]);
}
} else if let Some(group) = self.cmd.find_group(arg_or_group) {

View file

@ -490,3 +490,20 @@ fn group_conflicts_with_default_arg() {
assert_eq!(m.value_of("opt"), Some("default"));
assert!(m.is_present("flag"));
}
#[test]
fn exclusive_with_required() {
let cmd = Command::new("bug")
.arg(Arg::new("test").long("test").exclusive(true))
.arg(Arg::new("input").takes_value(true).required(true));
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();
}