Skip to content

Commit

Permalink
bug: Include groups in get_arg_conflicts_with
Browse files Browse the repository at this point in the history
So that it doesn't panic if trying to get the conflicts for an Arg that
conflicts with a group.

Fixes: #3900
  • Loading branch information
tmccombs committed Jul 11, 2022
1 parent 4ecdd1a commit 66cd439
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 9 deletions.
25 changes: 16 additions & 9 deletions src/builder/command.rs
Expand Up @@ -3556,15 +3556,22 @@ 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(group.args.iter().map(|id| {
self.find(id).expect(
"Command::get_arg_conflicts_with: \
The passed arg conflicts with a group containing an unknown arg",
)
}));
} else {
panic!("Command::get_arg_conflicts_with: The passed arg conflicts with an arg unknown to the cmd");
}
}
result
}
}

Expand Down
20 changes: 20 additions & 0 deletions tests/builder/conflicts.rs
Expand Up @@ -154,6 +154,8 @@ fn arg_conflicts_with_group() {
if let Err(err) = result {
panic!("{}", err);
}

cmd.build();
}

#[test]
Expand Down Expand Up @@ -296,6 +298,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 66cd439

Please sign in to comment.