Skip to content

Commit

Permalink
fix(parser): Only add ArgGroup to ArgMatches for command-line
Browse files Browse the repository at this point in the history
This will fix `clap_derive`s behavior for optional-flattened groups as
it will properly detect when the group is present (#3566).

While I consider this a bug and not part of compatibility guarentees, I
still want to keep in mind user impact which could still prevent this.
Defaults will make the group always-present which has little value and
if anything is relying on this, it is probably an application bug.
  • Loading branch information
epage committed Oct 12, 2022
1 parent e98fc7f commit d0dcaac
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
16 changes: 9 additions & 7 deletions src/parser/parser.rs
Expand Up @@ -1490,13 +1490,15 @@ impl<'cmd> Parser<'cmd> {
self.remove_overrides(arg, matcher);
}
matcher.start_custom_arg(arg, source);
for group in self.cmd.groups_for_arg(arg.get_id()) {
matcher.start_custom_group(group.clone(), source);
matcher.add_val_to(
&group,
AnyValue::new(arg.get_id().clone()),
OsString::from(arg.get_id().as_str()),
);
if source.is_explicit() {
for group in self.cmd.groups_for_arg(arg.get_id()) {
matcher.start_custom_group(group.clone(), source);
matcher.add_val_to(
&group,
AnyValue::new(arg.get_id().clone()),
OsString::from(arg.get_id().as_str()),
);
}
}
}
}
Expand Down
9 changes: 6 additions & 3 deletions tests/builder/groups.rs
Expand Up @@ -73,9 +73,10 @@ fn group_single_value() {
#[test]
fn group_empty() {
let res = Command::new("group")
.arg(arg!(-f --flag "some flag"))
.arg(arg!(-c --color [color] "some option"))
.arg(arg!(-n --hostname <name> "another option"))
.group(ArgGroup::new("grp").args(["hostname", "color"]))
.group(ArgGroup::new("grp").args(["hostname", "color", "flag"]))
.try_get_matches_from(vec![""]);
assert!(res.is_ok(), "{}", res.unwrap_err());

Expand All @@ -87,12 +88,13 @@ fn group_empty() {
#[test]
fn group_required_flags_empty() {
let result = Command::new("group")
.arg(arg!(-f --flag "some flag"))
.arg(arg!(-c --color "some option"))
.arg(arg!(-n --hostname <name> "another option"))
.group(
ArgGroup::new("grp")
.required(true)
.args(["hostname", "color"]),
.args(["hostname", "color", "flag"]),
)
.try_get_matches_from(vec![""]);
assert!(result.is_err());
Expand All @@ -103,9 +105,10 @@ fn group_required_flags_empty() {
#[test]
fn group_multi_value_single_arg() {
let res = Command::new("group")
.arg(arg!(-f --flag "some flag"))
.arg(arg!(-c --color <color> "some option").num_args(1..))
.arg(arg!(-n --hostname <name> "another option"))
.group(ArgGroup::new("grp").args(["hostname", "color"]))
.group(ArgGroup::new("grp").args(["hostname", "color", "flag"]))
.try_get_matches_from(vec!["", "-c", "blue", "red", "green"]);
assert!(res.is_ok(), "{:?}", res.unwrap_err().kind());

Expand Down

0 comments on commit d0dcaac

Please sign in to comment.