Optional flag argument conflicts with required subcommand
domenkozar opened this issue · comments
Domen Kožar commented
Please complete the following tasks
- I have searched the discussions
- I have searched the open and rejected issues
Rust Version
1.76
Clap Version
4.5.4
Minimal reproducible code
use clap::{Parser, Subcommand};
#[derive(Parser, Debug)]
#[command(subcommand_required = true)]
struct Cli {
#[arg(
short,
long,
value_delimiter = ',',
)]
arg: Option<Vec<String>>,
#[command(subcommand)]
command: Subcommands,
}
#[derive(Subcommand, Clone, Debug)]
enum Subcommands {
#[command()]
Subcommand { },
}
fn main() {
let cli = Cli::parse();
println!("{:?}", cli);
}
Steps to reproduce the bug with the above code
❯ cargo run -- --arg subcommand
Finished dev [unoptimized + debuginfo] target(s) in 0.03s
Running `target/debug/clap-arg-subcommand --arg subcommand`
error: 'clap-arg-subcommand' requires a subcommand but one was not provided
[subcommands: subcommand, help]
Usage: clap-arg-subcommand [OPTIONS] <COMMAND>
For more information, try '--help'.
Actual Behaviour
--arg consumed subcommand
as an arg and then complains about missing subcommand
Expected Behaviour
❯ cargo run -- --arg subcommand
Cli { arg: Some([]), command: Subcommand }
❯ cargo run -- subcommand
Cli { arg: None, command: Subcommand }
❯ cargo run -- --arg arg1 subcommand
Cli { arg: Some(["arg1"]), command: Subcommand }
❯ cargo run -- --arg arg1,arg2 subcommand
Cli { arg: Some(["arg1", "arg2"]), command: Subcommand }
Additional Context
No response
Debug Output
No response
Ed Page commented
Try enabling #[command(subcommand_precedence_over_arg = true)]
Domen Kožar commented
Thanks, I assume there's a good reason it's not on by default :)