fix(assert): Prevent arguments from requiring self

It's non-sensical for an argument to require itself, so it must be a
mistake, and should be prevented.

This is arguably a breaking change, but of the spacebar heating kind.

Signed-off-by: Omer Tuchfeld <omer@tuchfeld.dev>
This commit is contained in:
Omer Tuchfeld 2024-12-03 19:58:37 +01:00
parent 52aad0ea1a
commit 29d9e8844f
2 changed files with 8 additions and 3 deletions

View file

@ -140,6 +140,12 @@ pub(crate) fn assert_app(cmd: &Command) {
// requires, r_if, r_unless
for (_predicate, req_id) in &arg.requires {
assert!(
&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",

View file

@ -1480,10 +1480,9 @@ For more information, try '--help'.
}
#[test]
/// This test demonstrates existing broken behavior, ideally it should panic
#[should_panic = "Argument flag cannot require itself"]
fn requires_self() {
let result = Command::new("flag_required")
let _result = Command::new("flag_required")
.arg(arg!(-f --flag "some flag").requires("flag"))
.try_get_matches_from(vec![""]);
assert!(result.is_ok());
}