-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
fix: Require validation not bypassed by mutually exclusive groups #6050
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
Draft
12yanogden
wants to merge
3
commits into
clap-rs:master
Choose a base branch
from
12yanogden:issue-4707
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
/// Tests for GitHub issue #4707: `requires` validation should not be bypassed when | ||
/// arguments are in a mutually exclusive group. | ||
/// | ||
/// This issue appears to have been resolved in the current version of clap. | ||
/// These tests verify that the requires validation works correctly. | ||
use clap::{Arg, ArgAction, ArgGroup, Command, error::ErrorKind}; | ||
|
||
#[cfg(feature = "derive")] | ||
mod derive_tests { | ||
use clap::{ArgGroup, Parser}; | ||
|
||
#[derive(Parser, Debug)] | ||
#[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, | ||
} | ||
|
||
#[test] | ||
fn issue_4707_original_derive_example() { | ||
// This is the exact example from the GitHub issue | ||
// It should fail when --show-hex is used without --read | ||
let result = Args::try_parse_from(["test", "--show-hex"]); | ||
|
||
assert!(result.is_err(), "Should fail because --show-hex requires --read"); | ||
assert_eq!(result.unwrap_err().kind(), clap::error::ErrorKind::MissingRequiredArgument); | ||
} | ||
|
||
#[test] | ||
fn issue_4707_derive_example_with_write() { | ||
// Test the problematic case: using --write and --show-hex together | ||
// This should fail because --show-hex requires --read, but --write and --read are mutually exclusive | ||
let result = Args::try_parse_from(["test", "--write", "--show-hex"]); | ||
|
||
assert!(result.is_err(), "Should fail because --show-hex requires --read but --write and --read are mutually exclusive"); | ||
assert_eq!(result.unwrap_err().kind(), clap::error::ErrorKind::MissingRequiredArgument); | ||
} | ||
|
||
#[test] | ||
fn issue_4707_derive_example_valid_case() { | ||
// This should succeed when --read and --show-hex are used together | ||
let result = Args::try_parse_from(["test", "--read", "--show-hex"]); | ||
|
||
assert!(result.is_ok(), "Should succeed when --read and --show-hex are used together"); | ||
let args = result.unwrap(); | ||
assert!(args.read); | ||
assert!(args.show_hex); | ||
assert!(!args.write); | ||
} | ||
} | ||
|
||
#[test] | ||
fn issue_4707_requires_should_be_validated_when_args_are_in_group() { | ||
// This test ensures that `requires` validation is NOT bypassed | ||
// when arguments are in a mutually exclusive group | ||
let cmd = Command::new("test") | ||
.arg(Arg::new("one").short('1').action(ArgAction::SetTrue).requires("foo")) | ||
.arg(Arg::new("two").short('2').action(ArgAction::SetTrue).requires("foo")) | ||
.arg(Arg::new("foo").short('f').action(ArgAction::SetTrue)) | ||
.group(ArgGroup::new("group").args(["one", "two"])); | ||
|
||
// This should fail because --foo is required when either -1 or -2 is present | ||
let result = cmd.try_get_matches_from(vec!["test", "-1"]); | ||
|
||
// Verify the validation works correctly (issue is fixed) | ||
assert!(result.is_err(), "Should fail because -1 requires foo but foo is missing"); | ||
assert_eq!(result.unwrap_err().kind(), ErrorKind::MissingRequiredArgument); | ||
} | ||
|
||
#[test] | ||
fn issue_4707_mutually_exclusive_group_bypasses_requires() { | ||
// Test with explicitly mutually exclusive group | ||
let cmd = Command::new("test") | ||
.arg(Arg::new("one").short('1').action(ArgAction::SetTrue).requires("foo")) | ||
.arg(Arg::new("two").short('2').action(ArgAction::SetTrue).requires("foo")) | ||
.arg(Arg::new("foo").short('f').action(ArgAction::SetTrue)) | ||
.group(ArgGroup::new("group").args(["one", "two"]).multiple(false)); // explicit mutually exclusive | ||
|
||
// This should fail because --foo is required when either -1 or -2 is present | ||
let result = cmd.try_get_matches_from(vec!["test", "-1"]); | ||
|
||
assert!(result.is_err(), "Should fail because -1 requires foo but foo is missing"); | ||
assert_eq!(result.unwrap_err().kind(), ErrorKind::MissingRequiredArgument); | ||
} | ||
|
||
#[test] | ||
fn issue_4707_requires_should_work_when_required_arg_provided() { | ||
let cmd = Command::new("test") | ||
.arg(Arg::new("one").short('1').action(ArgAction::SetTrue).requires("foo")) | ||
.arg(Arg::new("two").short('2').action(ArgAction::SetTrue).requires("foo")) | ||
.arg(Arg::new("foo").short('f').action(ArgAction::SetTrue)) | ||
.group(ArgGroup::new("group").args(["one", "two"])); | ||
|
||
// This should succeed because --foo is provided | ||
let result = cmd.try_get_matches_from(vec!["test", "-1", "-f"]); | ||
|
||
assert!(result.is_ok(), "Should have succeeded when required argument is provided"); | ||
} | ||
|
||
#[test] | ||
fn issue_4707_group_requires_validation() { | ||
// Test with group that has 'requires' on the group itself | ||
let cmd = Command::new("test") | ||
.arg(Arg::new("one").short('1').action(ArgAction::SetTrue)) | ||
.arg(Arg::new("two").short('2').action(ArgAction::SetTrue)) | ||
.arg(Arg::new("foo").short('f').action(ArgAction::SetTrue)) | ||
.group(ArgGroup::new("group").args(["one", "two"]).requires("foo")); | ||
|
||
// This should fail because group requires 'foo' | ||
let result = cmd.try_get_matches_from(vec!["test", "-1"]); | ||
|
||
assert!(result.is_err(), "Should fail because group requires foo"); | ||
assert_eq!(result.unwrap_err().kind(), ErrorKind::MissingRequiredArgument); | ||
} | ||
|
||
#[test] | ||
fn issue_4707_complex_interaction_test() { | ||
// Test complex interactions between mutually exclusive groups and requires | ||
let cmd = Command::new("test") | ||
.arg(Arg::new("verbose").short('v').action(ArgAction::SetTrue).requires("output")) | ||
.arg(Arg::new("quiet").short('q').action(ArgAction::SetTrue).requires("output")) | ||
.arg(Arg::new("output").short('o').action(ArgAction::SetTrue)) | ||
.group(ArgGroup::new("verbosity").args(["verbose", "quiet"]).multiple(false)); | ||
|
||
// Test case 1: One argument from group without its required dependency | ||
let result1 = cmd.clone().try_get_matches_from(vec!["test", "-v"]); | ||
assert!(result1.is_err(), "Should fail because -v requires output"); | ||
assert_eq!(result1.unwrap_err().kind(), ErrorKind::MissingRequiredArgument); | ||
|
||
// Test case 2: Other argument from group without its required dependency | ||
let result2 = cmd.clone().try_get_matches_from(vec!["test", "-q"]); | ||
assert!(result2.is_err(), "Should fail because -q requires output"); | ||
assert_eq!(result2.unwrap_err().kind(), ErrorKind::MissingRequiredArgument); | ||
|
||
// Test case 3: Valid usage with dependency | ||
let result3 = cmd.clone().try_get_matches_from(vec!["test", "-v", "-o"]); | ||
assert!(result3.is_ok(), "Should succeed when dependency is provided"); | ||
} | ||
|
||
#[test] | ||
fn issue_4707_exact_github_example_builder() { | ||
// This reproduces the exact case from the GitHub issue using clap builder instead of derive | ||
let cmd = Command::new("test") | ||
.arg(Arg::new("read").long("read").action(ArgAction::SetTrue).group("command")) | ||
.arg(Arg::new("write").long("write").action(ArgAction::SetTrue).group("command")) | ||
.arg(Arg::new("show_hex").long("show-hex").action(ArgAction::SetTrue).requires("read")) | ||
.group(ArgGroup::new("command").multiple(false)); | ||
|
||
// This exact case should fail: --write --show-hex | ||
// Because --show-hex requires --read, but --read and --write are mutually exclusive | ||
let result = cmd.try_get_matches_from(["test", "--write", "--show-hex"]); | ||
|
||
assert!(result.is_err(), "Should fail because --show-hex requires --read but --write and --read are mutually exclusive"); | ||
assert_eq!(result.unwrap_err().kind(), ErrorKind::MissingRequiredArgument); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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
See also https://github.com/clap-rs/clap/blob/master/CONTRIBUTING.md
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.
So wait, is 4707 not a workable issue? I have to wait until 4520 is resolved?
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.
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.