Skip to content

Commit 3311214

Browse files
committed
fix(parser): Don't report conflicting arguments in usage
Fixes #6236
1 parent 3dca9ae commit 3311214

File tree

3 files changed

+14
-9
lines changed

3 files changed

+14
-9
lines changed

clap_builder/src/parser/validator.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,7 @@ impl<'cmd> Validator<'cmd> {
132132
}
133133

134134
debug!("Validator::build_conflict_err: name={name:?}");
135-
let mut seen = FlatSet::new();
136-
let conflicts = conflict_ids
135+
let conflict_ids = conflict_ids
137136
.iter()
138137
.flat_map(|c_id| {
139138
if self.cmd.find_group(c_id).is_some() {
@@ -142,16 +141,18 @@ impl<'cmd> Validator<'cmd> {
142141
vec![c_id.clone()]
143142
}
144143
})
145-
.filter_map(|c_id| {
146-
seen.insert(c_id.clone()).then(|| {
147-
let c_arg = self.cmd.find(&c_id).expect(INTERNAL_ERROR_MSG);
148-
c_arg.to_string()
149-
})
144+
.collect::<FlatSet<_>>()
145+
.into_vec();
146+
let conflicts = conflict_ids
147+
.iter()
148+
.map(|c_id| {
149+
let c_arg = self.cmd.find(&c_id).expect(INTERNAL_ERROR_MSG);
150+
c_arg.to_string()
150151
})
151152
.collect();
152153

153154
let former_arg = self.cmd.find(name).expect(INTERNAL_ERROR_MSG);
154-
let usg = self.build_conflict_err_usage(matcher, conflict_ids);
155+
let usg = self.build_conflict_err_usage(matcher, &conflict_ids);
155156
Err(Error::argument_conflict(
156157
self.cmd,
157158
former_arg.to_string(),

clap_builder/src/util/flat_set.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ impl<T: PartialEq + Eq> FlatSet<T> {
6060
{
6161
self.inner.sort_by_key(f);
6262
}
63+
64+
pub(crate) fn into_vec(self) -> Vec<T> {
65+
self.inner
66+
}
6367
}
6468

6569
impl<T: PartialEq + Eq> Default for FlatSet<T> {

tests/builder/groups.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ error: the argument '--conflict' cannot be used with:
232232
--a
233233
--b
234234
235-
Usage: prog --a --conflict
235+
Usage: prog --conflict
236236
237237
For more information, try '--help'.
238238

0 commit comments

Comments
 (0)