mirror of
https://github.com/clap-rs/clap
synced 2024-12-04 18:19:13 +00:00
Merge pull request #5836 from omertuc/reqself
fix(assert): Prevent arguments from requiring self
This commit is contained in:
commit
18a81c4133
2 changed files with 17 additions and 3 deletions
|
@ -139,12 +139,18 @@ pub(crate) fn assert_app(cmd: &Command) {
|
|||
}
|
||||
|
||||
// requires, r_if, r_unless
|
||||
for req in &arg.requires {
|
||||
for (_predicate, req_id) in &arg.requires {
|
||||
assert!(
|
||||
cmd.id_exists(&req.1),
|
||||
&arg.id != req_id,
|
||||
"Argument {} cannot require itself",
|
||||
arg.get_id()
|
||||
);
|
||||
|
||||
assert!(
|
||||
cmd.id_exists(req_id),
|
||||
"Command {}: Argument or group '{}' specified in 'requires*' for '{}' does not exist",
|
||||
cmd.get_name(),
|
||||
req.1,
|
||||
req_id,
|
||||
arg.get_id(),
|
||||
);
|
||||
}
|
||||
|
|
|
@ -1478,3 +1478,11 @@ For more information, try '--help'.
|
|||
";
|
||||
utils::assert_output(cmd, "test --require-first --second", EXPECTED, true);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic = "Argument flag cannot require itself"]
|
||||
fn requires_self() {
|
||||
let _result = Command::new("flag_required")
|
||||
.arg(arg!(-f --flag "some flag").requires("flag"))
|
||||
.try_get_matches_from(vec![""]);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue