Skip to content

Commit

Permalink
Merge pull request #4397 from epage/exclusive
Browse files Browse the repository at this point in the history
fix(parser): Don't make Args `exclusive` with their ArgGroup
  • Loading branch information
epage authored Oct 18, 2022
2 parents a40c7b4 + 45dcf0e commit 466b6f8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/parser/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ impl<'cmd> Validator<'cmd> {
.arg_ids()
.filter(|arg_id| {
matcher.check_explicit(arg_id, &crate::builder::ArgPredicate::IsPresent)
// Avoid including our own groups by checking none of them. If a group is present, the
// args for the group will be.
&& self.cmd.find(arg_id).is_some()
})
.count();
if args_count <= 1 {
Expand Down
23 changes: 20 additions & 3 deletions tests/builder/conflicts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,13 @@ fn flag_conflict_with_all() {

#[test]
fn exclusive_flag() {
let result = Command::new("flag_conflict")
let cmd = Command::new("flag_conflict")
.arg(arg!(-f --flag "some flag").exclusive(true))
.arg(arg!(-o --other "some flag"))
.try_get_matches_from(vec!["myprog", "-o", "-f"]);
.arg(arg!(-o --other "some flag"));
let result = cmd.clone().try_get_matches_from(vec!["myprog", "-f"]);
assert!(result.is_ok(), "{}", result.unwrap_err());

let result = cmd.clone().try_get_matches_from(vec!["myprog", "-o", "-f"]);
assert!(result.is_err());
let err = result.err().unwrap();
assert_eq!(err.kind(), ErrorKind::ArgumentConflict);
Expand Down Expand Up @@ -71,6 +74,20 @@ fn not_exclusive_with_defaults() {
assert!(result.is_ok(), "{}", result.unwrap_err());
}

#[test]
fn not_exclusive_with_group() {
let cmd = Command::new("test")
.group(clap::ArgGroup::new("test").arg("foo"))
.arg(
clap::Arg::new("foo")
.long("foo")
.exclusive(true)
.action(clap::ArgAction::SetTrue),
);
let result = cmd.try_get_matches_from(vec!["test", "--foo"]);
assert!(result.is_ok(), "{}", result.unwrap_err());
}

#[test]
fn default_doesnt_activate_exclusive() {
let result = Command::new("flag_conflict")
Expand Down

0 comments on commit 466b6f8

Please sign in to comment.