clap-rs / clap

A full featured, fast Command Line Argument Parser for Rust
docs.rs/clap
Apache License 2.0
13.64k stars 1.02k forks source link

`exclusive` fails to take priority over `required_unless_present` flags #5507

Closed obskyr closed 3 weeks ago

obskyr commented 1 month ago

Please complete the following tasks

Rust Version

rustc 1.77.2 (25ef9e3d8 2024-04-09)

Clap Version

4.5.4

Minimal reproducible code

The flags I have found to eschew exclusive are:

Steps to reproduce the bug with the above code

cargo run -- --exclusive-arg

Actual Behaviour

Clap fails to recognize that an exclusive argument has been supplied, and still requires the required arguments with an error message akin to:

error: the following required arguments were not provided:
  <REQUIRED_ARG>

Usage: clap-test.exe --exclusive-arg <REQUIRED_ARG> [ALTERNATIVE_ARG]

For more information, try '--help'.

Expected Behaviour

The exclusive argument should take priority, and allow not supplying the required arguments. (If it doesn't, it makes exclusive useless in these cases – since it's impossible to use the exclusive argument without adding the required arguments, and if you add the required arguments, you get an error due to supplying arguments along with the exclusive one.)

Additional Context

Using the exclusive flag for an argument allows it to override required arguments, as long as those arguments can be initialized without. This code that sets required = true on an argument still runs without error if --exclusive-arg is specified:

#[derive(clap::Parser)]
struct Args {
    #[arg(required = true)]
    required_arg: Option<String>,

    #[arg(exclusive = true, long, default_value_t = false)]
    exclusive_arg: bool
}

fn main() {
    let _args: Args = clap::Parser::parse();
}

To match this, the same should be true for the other, more specified ways to require an argument!

Note that the required_unless_present and required_unless_present_any (though crucially, not required_unless_present_all) cases can be worked around by adding a special case – for example, switching out required_unless_present("blah") for required_unless_present_any(["blah", "exclusive"]).

Debug Output

No response