Skip to content

Conversation

12yanogden
Copy link

Problem

Fixes #4707

Previously, arguments required via requires could be bypassed when they conflicted with other present arguments in mutually exclusive groups.

Example Issue:

#[derive(Parser)]
#[clap(group = ArgGroup::new("command").multiple(false))]
struct Args {
    #[clap(long, group = "command")]
    read: bool,
    
    #[clap(long, group = "command")]  
    write: bool,
    
    #[clap(long, requires = "read")]
    show_hex: bool,
}

@12yanogden 12yanogden force-pushed the issue-4707 branch 3 times, most recently from 8bf8afa to 4df478c Compare June 27, 2025 13:41
@pksunkara pksunkara requested a review from epage July 3, 2025 09:40
Comment on lines 347 to 354
// If this argument is conditionally required (i.e., required by other present arguments
// through the 'requires' relationship), it's NOT OK for it to be missing, even if it
// conflicts with other arguments. However, directly required arguments (marked as
// required(true)) can still be bypassed by conflicts.
if self.required.contains(a.get_id()) && !a.is_required_set() {
debug!("Validator::is_missing_required_ok: false (conditionally required)");
return false;
}
Copy link
Member

Choose a reason for hiding this comment

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

While this might fix the immediate problem, I'm worried that this is too brittle and we can change things in other code and not realize this needs to be update as well.

Copy link
Member

Choose a reason for hiding this comment

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

Note that #4520 is the core issue and it hasn't been accepted yet, requiring further analysis for correctness and whether this would constitute a breaking change.

For future contributions, please keep in mind

  • We prefer to resolve issues before moving on to PRs
  • Commits should be atomic, including tests passing
  • We do ask for tests to be added in commits before the behavior changes but that is to show the existing behavior
  • derive tests should go in the derive testsuite, not the builder one
  • tests should be grouped by and named for their functionality, not issue numbers

See also https://github.com/clap-rs/clap/blob/master/CONTRIBUTING.md

Copy link
Author

Choose a reason for hiding this comment

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

So wait, is 4707 not a workable issue? I have to wait until 4520 is resolved?

Copy link
Member

Choose a reason for hiding this comment

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

They are the same concept (how should requires interact with conflicts) and that question needs to be addressed before either moves forward. Rather than split that conversation between two related issues, I recommend we centralize it on #4520. That doesn't mean that #4520 needs to be implemented first. In fact, its likely that a fix for one will fix the other or be just a one or two line change.

@epage epage marked this pull request as draft July 3, 2025 16:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants