Merge pull request #5836 from omertuc/reqself

fix(assert): Prevent arguments from requiring self
This commit is contained in:
Ed Page 2024-12-03 14:31:51 -06:00 committed by GitHub
commit 18a81c4133
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 17 additions and 3 deletions

View file

@ -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(),
);
}

View file

@ -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![""]);
}