should global options be allowed when args_conflicts_with_subcommands? #6182
-
I want to support this usage:That is by this code:use clap::{Parser, Subcommand};
use std::path::PathBuf; // not important
#[derive(Parser)]
#[command(version, about, long_about = None)]
#[command(args_conflicts_with_subcommands = true)]
struct Cli {
// say the Git directory
#[arg(long, short='g')]
#[arg(global=true)]
directory: Option<PathBuf>,
start: Option<String>, // this is the original way of using this tool
#[command(subcommand)] // this tool is expanded by alternative CLI, command being triggered by options: -d | --delete etc.
command: Option<Commands>,
}
#[derive(Subcommand, Debug)]
enum Commands {
#[command(short_flag('d'))]
Delete,
}
fn main() {
let cli = Cli::parse();
if let Some(argument) = cli.start {
println!("old processing {}", argument);
} else if let Some(argument) = cli.command {
println!("cmd {:?}", argument);
} else {
println!("no action");
}
}Use cases
The following should not be valid:
args_conflicts_with_subcommands indeed makes it fail. But this (args_conflicts_with_subcommands_set) also prevents using the -g option before the sub-command:
unfortunately fails, with a panic. But I would want it to pass, not error. Patch to enable itI claim that there is even an undeserved panic .... I tried to exclude global options as exception to the "conflict", in this patch: Not having read the whole parse() yet, I am not sure if it's correct -- not breaking other features/use-cases. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
#6184 fixes the panic Unsure if we should relax the validation around globals. Feel free to create an issue and we can discuss it further. |
Beta Was this translation helpful? Give feedback.
#6184 fixes the panic
Unsure if we should relax the validation around globals. Feel free to create an issue and we can discuss it further.