-
-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: Don't bypass conflicts when using requires
#4523
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -381,8 +381,8 @@ impl<'cmd> Validator<'cmd> { | |
conflicts: &mut Conflicts, | ||
) -> bool { | ||
debug!("Validator::is_missing_required_ok: {}", a.get_id()); | ||
let conflicts = conflicts.gather_conflicts(self.cmd, matcher, a.get_id()); | ||
!conflicts.is_empty() | ||
let overrides = conflicts.gather_overrides(self.cmd, matcher, a.get_id()); | ||
!overrides.is_empty() | ||
} | ||
|
||
// Failing a required unless means, the arg's "unless" wasn't present, and neither were they | ||
|
@@ -530,6 +530,52 @@ impl Conflicts { | |
conf | ||
}) | ||
} | ||
|
||
fn gather_overrides(&mut self, cmd: &Command, matcher: &ArgMatcher, arg_id: &Id) -> Vec<Id> { | ||
debug!("Conflicts::gather_overrides: arg={:?}", arg_id); | ||
Comment on lines
+534
to
+535
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we have an intermediate commit that breaks out these functions, duplicating the other behavior, and then the commit after changes it to the new behavior? It'd make it a lot easier to see what the behavior difference is. |
||
let mut overrides = Vec::new(); | ||
for other_arg_id in matcher | ||
.arg_ids() | ||
.filter(|arg_id| matcher.check_explicit(arg_id, &ArgPredicate::IsPresent)) | ||
{ | ||
if arg_id == other_arg_id { | ||
continue; | ||
} | ||
|
||
if self | ||
.gather_direct_overrides(cmd, arg_id) | ||
.contains(other_arg_id) | ||
{ | ||
overrides.push(other_arg_id.clone()); | ||
} | ||
if self | ||
.gather_direct_overrides(cmd, other_arg_id) | ||
.contains(arg_id) | ||
{ | ||
overrides.push(other_arg_id.clone()); | ||
} | ||
} | ||
debug!("Conflicts::gather_overrides: overrides={:?}", overrides); | ||
overrides | ||
} | ||
|
||
fn gather_direct_overrides(&mut self, cmd: &Command, arg_id: &Id) -> &[Id] { | ||
self.potential.entry(arg_id.clone()).or_insert_with(|| { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't this using the same cache as |
||
let conf = if let Some(arg) = cmd.find(arg_id) { | ||
arg.overrides.clone() | ||
} else if let Some(group) = cmd.find_group(arg_id) { | ||
group.conflicts.clone() | ||
} else { | ||
debug_assert!(false, "id={:?} is unknown", arg_id); | ||
Vec::new() | ||
}; | ||
debug!( | ||
"Conflicts::gather_direct_overrides id={:?}, overrides={:?}", | ||
arg_id, conf | ||
); | ||
conf | ||
}) | ||
} | ||
} | ||
|
||
pub(crate) fn get_possible_values_cli(a: &Arg) -> Vec<PossibleValue> { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,6 +41,18 @@ fn option_required() { | |
assert_eq!(err.kind(), ErrorKind::MissingRequiredArgument); | ||
} | ||
|
||
#[test] | ||
fn option_required_and_conflicts_with_other_option() { | ||
let result = Command::new("cli") | ||
.arg(arg!(brick: -b "do something harmful")) | ||
.arg(arg!(fix: -f "do something good").conflicts_with("brick")) | ||
.arg(arg!(dry_run: -d "don't do it Louis").requires("fix")) | ||
.try_get_matches_from(vec!["", "-b", "-d"]); | ||
assert!(result.is_err()); | ||
let err = result.err().unwrap(); | ||
assert_eq!(err.kind(), ErrorKind::MissingRequiredArgument); | ||
} | ||
Comment on lines
+44
to
+54
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As I would prefer every commit to pass tests, please make this test pass with the bad behavior and then the followup commit would make it pass with the good behavior |
||
|
||
#[test] | ||
fn option_required_2() { | ||
let m = Command::new("option_required") | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
merge conflict with 85ecb3e and 4a34b9d