Skip to content

Commit

Permalink
Merge pull request #3902 from tmccombs/get-conflicts-with-group
Browse files Browse the repository at this point in the history
Include groups in `get_arg_conflicts_with`
  • Loading branch information
epage committed Jul 14, 2022
2 parents 2df0732 + f27f1f5 commit afc5401
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
24 changes: 15 additions & 9 deletions src/builder/command.rs
Expand Up @@ -3556,15 +3556,21 @@ impl<'help> App<'help> {
if arg.is_global_set() {
self.get_global_arg_conflicts_with(arg)
} else {
arg.blacklist
.iter()
.map(|id| {
self.args.args().find(|arg| arg.id == *id).expect(
"Command::get_arg_conflicts_with: \
The passed arg conflicts with an arg unknown to the cmd",
)
})
.collect()
let mut result = Vec::new();
for id in arg.blacklist.iter() {
if let Some(arg) = self.find(id) {
result.push(arg);
} else if let Some(group) = self.find_group(id) {
result.extend(
self.unroll_args_in_group(&group.id)
.iter()
.map(|id| self.find(id).expect(INTERNAL_ERROR_MSG)),
);
} else {
panic!("Command::get_arg_conflicts_with: The passed arg conflicts with an arg unknown to the cmd");
}
}
result
}
}

Expand Down
18 changes: 18 additions & 0 deletions tests/builder/conflicts.rs
Expand Up @@ -296,6 +296,24 @@ fn required_group_conflicts_with_arg() {
}
}

#[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");
}

#[test]
fn conflict_output() {
utils::assert_output(
Expand Down

0 comments on commit afc5401

Please sign in to comment.