Skip to content

Commit af55024

Browse files
authored
Merge pull request #3334 from epage/source
fix(parser): Track the most explicit value source
2 parents afd0342 + c6664af commit af55024

File tree

3 files changed

+46
-9
lines changed

3 files changed

+46
-9
lines changed

src/parse/arg_matcher.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ impl ArgMatcher {
135135
let id = &arg.id;
136136
debug!("ArgMatcher::inc_occurrence_of_arg: id={:?}", id);
137137
let ma = self.entry(id).or_insert(MatchedArg::new());
138-
ma.set_ty(ValueType::CommandLine);
138+
ma.update_ty(ValueType::CommandLine);
139139
ma.set_ignore_case(arg.is_set(ArgSettings::IgnoreCase));
140140
ma.invalid_utf8_allowed(arg.is_set(ArgSettings::AllowInvalidUtf8));
141141
ma.occurs += 1;
@@ -144,7 +144,7 @@ impl ArgMatcher {
144144
pub(crate) fn inc_occurrence_of_group(&mut self, id: &Id) {
145145
debug!("ArgMatcher::inc_occurrence_of_group: id={:?}", id);
146146
let ma = self.entry(id).or_insert(MatchedArg::new());
147-
ma.set_ty(ValueType::CommandLine);
147+
ma.update_ty(ValueType::CommandLine);
148148
ma.occurs += 1;
149149
}
150150

@@ -161,13 +161,13 @@ impl ArgMatcher {
161161
// specific circumstances, like only add one occurrence for flag
162162
// when we met: `--flag=one,two`).
163163
let ma = self.entry(arg).or_default();
164-
ma.set_ty(ty);
164+
ma.update_ty(ty);
165165
ma.push_val(val);
166166
}
167167

168168
fn append_val_to(&mut self, arg: &Id, val: OsString, ty: ValueType) {
169169
let ma = self.entry(arg).or_default();
170-
ma.set_ty(ty);
170+
ma.update_ty(ty);
171171
ma.append_val(val);
172172
}
173173

@@ -178,7 +178,7 @@ impl ArgMatcher {
178178

179179
pub(crate) fn add_index_to(&mut self, arg: &Id, idx: usize, ty: ValueType) {
180180
let ma = self.entry(arg).or_default();
181-
ma.set_ty(ty);
181+
ma.update_ty(ty);
182182
ma.push_index(idx);
183183
}
184184

src/parse/matches/matched_arg.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,8 @@ impl MatchedArg {
119119
})
120120
}
121121

122-
pub(crate) fn set_ty(&mut self, ty: ValueType) {
123-
self.ty = ty;
122+
pub(crate) fn update_ty(&mut self, ty: ValueType) {
123+
self.ty = self.ty.max(ty);
124124
}
125125

126126
pub(crate) fn set_ignore_case(&mut self, yes: bool) {
@@ -142,13 +142,13 @@ impl Default for MatchedArg {
142142
}
143143
}
144144

145-
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
145+
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
146146
pub(crate) enum ValueType {
147147
Unknown,
148+
DefaultValue,
148149
#[cfg(feature = "env")]
149150
EnvVariable,
150151
CommandLine,
151-
DefaultValue,
152152
}
153153

154154
#[cfg(test)]

tests/builder/conflicts.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,43 @@ fn arg_conflicts_with_group() {
106106
}
107107
}
108108

109+
#[test]
110+
fn arg_conflicts_with_group_with_multiple_sources() {
111+
let mut app = clap::App::new("group_conflict")
112+
.arg(clap::arg!(-f --flag "some flag").conflicts_with("gr"))
113+
.group(clap::ArgGroup::new("gr").multiple(true))
114+
.arg(
115+
clap::arg!(--some <name> "some arg")
116+
.required(false)
117+
.group("gr"),
118+
)
119+
.arg(
120+
clap::arg!(--other <secs> "other arg")
121+
.required(false)
122+
.default_value("1000")
123+
.group("gr"),
124+
);
125+
126+
let result = app.try_get_matches_from_mut(vec!["myprog", "-f"]);
127+
if let Err(err) = result {
128+
panic!("{}", err);
129+
}
130+
131+
let result = app.try_get_matches_from_mut(vec!["myprog", "--some", "usb1"]);
132+
if let Err(err) = result {
133+
panic!("{}", err);
134+
}
135+
136+
let result = app.try_get_matches_from_mut(vec!["myprog", "--some", "usb1", "--other", "40"]);
137+
if let Err(err) = result {
138+
panic!("{}", err);
139+
}
140+
141+
let result = app.try_get_matches_from_mut(vec!["myprog", "-f", "--some", "usb1"]);
142+
let err = result.err().unwrap();
143+
assert_eq!(err.kind, ErrorKind::ArgumentConflict);
144+
}
145+
109146
#[test]
110147
fn group_conflicts_with_arg() {
111148
let mut app = App::new("group_conflict")

0 commit comments

Comments
 (0)