`exclusive` fails to take priority over `required_unless_present` flags
obskyr opened this issue · comments
Please complete the following tasks
- I have searched the discussions
- I have searched the open and rejected issues
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:
-
required_unless_present
#[derive(clap::Parser)] struct Args { #[arg(required_unless_present("alternative_arg"))] required_arg: Option<String>, alternative_arg: Option<String>, #[arg(exclusive = true, long, default_value_t = false)] exclusive_arg: bool } fn main() { let _args: Args = clap::Parser::parse(); }
-
required_unless_present_any
#[derive(clap::Parser)] struct Args { #[arg(required_unless_present_any(["alternative_arg"]))] required_arg: Option<String>, alternative_arg: Option<String>, #[arg(exclusive = true, long, default_value_t = false)] exclusive_arg: bool } fn main() { let _args: Args = clap::Parser::parse(); }
-
required_unless_present_all
#[derive(clap::Parser)] struct Args { #[arg(required_unless_present_all(["alternative_arg"]))] required_arg: Option<String>, alternative_arg: Option<String>, #[arg(exclusive = true, long, default_value_t = false)] exclusive_arg: bool } fn main() { let _args: Args = clap::Parser::parse(); }
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