Skip to content

Commit

Permalink
Merge pull request #5836 from omertuc/reqself
Browse files Browse the repository at this point in the history
fix(assert): Prevent arguments from requiring self
  • Loading branch information
epage authored Dec 3, 2024
2 parents b4ea2d4 + 29d9e88 commit 18a81c4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
12 changes: 9 additions & 3 deletions clap_builder/src/builder/debug_asserts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
);
}
Expand Down
8 changes: 8 additions & 0 deletions tests/builder/require.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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![""]);
}

0 comments on commit 18a81c4

Please sign in to comment.