Skip to content

Commit

Permalink
Merge pull request #4573 from epage/conflict
Browse files Browse the repository at this point in the history
fix(parser): Override required when parent group has conflict
  • Loading branch information
epage committed Dec 22, 2022
2 parents 0eccd55 + 85ecb3e commit b877345
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/parser/validator.rs
Expand Up @@ -371,8 +371,17 @@ impl<'cmd> Validator<'cmd> {

fn is_missing_required_ok(&self, a: &Arg, conflicts: &Conflicts) -> bool {
debug!("Validator::is_missing_required_ok: {}", a.get_id());
let conflicts = conflicts.gather_conflicts(self.cmd, a.get_id());
!conflicts.is_empty()
if !conflicts.gather_conflicts(self.cmd, a.get_id()).is_empty() {
debug!("Validator::is_missing_required_ok: true (self)");
return true;
}
for group_id in self.cmd.groups_for_arg(a.get_id()) {
if !conflicts.gather_conflicts(self.cmd, &group_id).is_empty() {
debug!("Validator::is_missing_required_ok: true ({})", group_id);
return true;
}
}
false
}

// Failing a required unless means, the arg's "unless" wasn't present, and neither were they
Expand Down
29 changes: 29 additions & 0 deletions tests/builder/conflicts.rs
Expand Up @@ -235,6 +235,35 @@ fn arg_conflicts_with_required_group() {
}
}

#[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"]);
if let Err(err) = result {
panic!("{}", err);
}
}

#[test]
fn required_group_conflicts_with_arg() {
let mut cmd = Command::new("group_conflict")
Expand Down

0 comments on commit b877345

Please sign in to comment.