Skip to content

Commit f0223a4

Browse files
authored
Merge pull request #4375 from epage/group2
fix(parser): Only add ArgGroup to ArgMatches for command-line
2 parents e5a7a65 + d0dcaac commit f0223a4

5 files changed

Lines changed: 23 additions & 45 deletions

File tree

src/parser/arg_matcher.rs

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -152,23 +152,6 @@ impl ArgMatcher {
152152
ma.new_val_group();
153153
}
154154

155-
pub(crate) fn start_occurrence_of_arg(&mut self, arg: &Arg) {
156-
let id = arg.get_id().clone();
157-
debug!("ArgMatcher::start_occurrence_of_arg: id={:?}", id);
158-
let ma = self.entry(id).or_insert(MatchedArg::new_arg(arg));
159-
debug_assert_eq!(ma.type_id(), Some(arg.get_value_parser().type_id()));
160-
ma.set_source(ValueSource::CommandLine);
161-
ma.new_val_group();
162-
}
163-
164-
pub(crate) fn start_occurrence_of_group(&mut self, id: Id) {
165-
debug!("ArgMatcher::start_occurrence_of_group: id={:?}", id);
166-
let ma = self.entry(id).or_insert(MatchedArg::new_group());
167-
debug_assert_eq!(ma.type_id(), None);
168-
ma.set_source(ValueSource::CommandLine);
169-
ma.new_val_group();
170-
}
171-
172155
pub(crate) fn start_occurrence_of_external(&mut self, cmd: &crate::Command) {
173156
let id = Id::from_static_ref(Id::EXTERNAL);
174157
debug!("ArgMatcher::start_occurrence_of_external: id={:?}", id,);

src/parser/matches/matched_arg.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ impl MatchedArg {
130130
}
131131

132132
pub(crate) fn check_explicit(&self, predicate: &ArgPredicate) -> bool {
133-
if self.source == Some(ValueSource::DefaultValue) {
133+
if self.source.map(|s| !s.is_explicit()).unwrap_or(false) {
134134
return false;
135135
}
136136

src/parser/matches/value_source.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,9 @@ pub enum ValueSource {
99
/// Value was passed in on the command-line
1010
CommandLine,
1111
}
12+
13+
impl ValueSource {
14+
pub(crate) fn is_explicit(self) -> bool {
15+
self != Self::DefaultValue
16+
}
17+
}

src/parser/parser.rs

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,15 +1078,6 @@ impl<'cmd> Parser<'cmd> {
10781078
matcher.add_index_to(arg.get_id(), self.cur_idx.get());
10791079
}
10801080

1081-
// Increment or create the group "args"
1082-
for group in self.cmd.groups_for_arg(arg.get_id()) {
1083-
matcher.add_val_to(
1084-
&group,
1085-
AnyValue::new(arg.get_id().clone()),
1086-
OsString::from(arg.get_id().as_str()),
1087-
);
1088-
}
1089-
10901081
Ok(())
10911082
}
10921083

@@ -1499,20 +1490,15 @@ impl<'cmd> Parser<'cmd> {
14991490
self.remove_overrides(arg, matcher);
15001491
}
15011492
matcher.start_custom_arg(arg, source);
1502-
for group in self.cmd.groups_for_arg(arg.get_id()) {
1503-
matcher.start_custom_group(group, source);
1504-
}
1505-
}
1506-
1507-
/// Increase occurrence of specific argument and the grouped arg it's in.
1508-
fn start_occurrence_of_arg(&self, matcher: &mut ArgMatcher, arg: &Arg) {
1509-
// With each new occurrence, remove overrides from prior occurrences
1510-
self.remove_overrides(arg, matcher);
1511-
1512-
matcher.start_occurrence_of_arg(arg);
1513-
// Increment or create the group "args"
1514-
for group in self.cmd.groups_for_arg(arg.get_id()) {
1515-
matcher.start_occurrence_of_group(group);
1493+
if source.is_explicit() {
1494+
for group in self.cmd.groups_for_arg(arg.get_id()) {
1495+
matcher.start_custom_group(group.clone(), source);
1496+
matcher.add_val_to(
1497+
&group,
1498+
AnyValue::new(arg.get_id().clone()),
1499+
OsString::from(arg.get_id().as_str()),
1500+
);
1501+
}
15161502
}
15171503
}
15181504
}
@@ -1549,7 +1535,7 @@ impl<'cmd> Parser<'cmd> {
15491535
// Add the arg to the matches to build a proper usage string
15501536
if let Some((name, _)) = did_you_mean.as_ref() {
15511537
if let Some(arg) = self.cmd.get_keymap().get(&name.as_ref()) {
1552-
self.start_occurrence_of_arg(matcher, arg);
1538+
self.start_custom_arg(matcher, arg, ValueSource::CommandLine);
15531539
}
15541540
}
15551541

tests/builder/groups.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,10 @@ fn group_single_value() {
7373
#[test]
7474
fn group_empty() {
7575
let res = Command::new("group")
76+
.arg(arg!(-f --flag "some flag"))
7677
.arg(arg!(-c --color [color] "some option"))
7778
.arg(arg!(-n --hostname <name> "another option"))
78-
.group(ArgGroup::new("grp").args(["hostname", "color"]))
79+
.group(ArgGroup::new("grp").args(["hostname", "color", "flag"]))
7980
.try_get_matches_from(vec![""]);
8081
assert!(res.is_ok(), "{}", res.unwrap_err());
8182

@@ -87,12 +88,13 @@ fn group_empty() {
8788
#[test]
8889
fn group_required_flags_empty() {
8990
let result = Command::new("group")
91+
.arg(arg!(-f --flag "some flag"))
9092
.arg(arg!(-c --color "some option"))
9193
.arg(arg!(-n --hostname <name> "another option"))
9294
.group(
9395
ArgGroup::new("grp")
9496
.required(true)
95-
.args(["hostname", "color"]),
97+
.args(["hostname", "color", "flag"]),
9698
)
9799
.try_get_matches_from(vec![""]);
98100
assert!(result.is_err());
@@ -103,9 +105,10 @@ fn group_required_flags_empty() {
103105
#[test]
104106
fn group_multi_value_single_arg() {
105107
let res = Command::new("group")
108+
.arg(arg!(-f --flag "some flag"))
106109
.arg(arg!(-c --color <color> "some option").num_args(1..))
107110
.arg(arg!(-n --hostname <name> "another option"))
108-
.group(ArgGroup::new("grp").args(["hostname", "color"]))
111+
.group(ArgGroup::new("grp").args(["hostname", "color", "flag"]))
109112
.try_get_matches_from(vec!["", "-c", "blue", "red", "green"]);
110113
assert!(res.is_ok(), "{:?}", res.unwrap_err().kind());
111114

0 commit comments

Comments
 (0)