Skip to content
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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
50 changes: 48 additions & 2 deletions src/parser/validator.rs
Expand Up @@ -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());
Copy link
Member

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

!overrides.is_empty()
}

// Failing a required unless means, the arg's "unless" wasn't present, and neither were they
Expand Down Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The 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(|| {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this using the same cache as gather_direct_conflicts which would cause the data to get mixed up?

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> {
Expand Down
12 changes: 12 additions & 0 deletions tests/builder/require.rs
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The 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")
Expand Down