Repository navigation
|
Working on a CLI a bit like (skeleton): #[derive(Parser)]
struct Cli {
#[command(flatten)]
log_level: LogLevel,
#[command(subcommand)]
command: Commands,
}
#[derive(Args)]
#[group(multiple = false)]
struct LogLevel {
#[arg(long)]
debug: bool,
#[arg(long)]
quiet: bool,
}My intention is to be able to specify Can I make these flags non-positional such that they can appear anywhere in the arguments and still be recognised? |
Replies: 1 comment
|
Yes—this is what clap's global arguments are for. Mark the two arguments global where they are declared: #[derive(Parser)]
struct Cli {
#[command(flatten)]
log_level: LogLevel,
#[command(subcommand)]
command: Commands,
}
#[derive(Args)]
#[group(multiple = false)]
struct LogLevel {
#[arg(long, global = true)]
debug: bool,
#[arg(long, global = true)]
quiet: bool,
}Then all of these forms are recognized: The value is propagated back into the top-level flattened The exceptions are the normal parsing boundaries: a flag after There is a derive test for the same “root flag supplied after a subcommand” behavior here: https://github.com/clap-rs/clap/blob/master/tests/derive/subcommands.rs (search for |
Yes—this is what clap's global arguments are for. Mark the two arguments global where they are declared:
Then all of these forms are recognized:
The value is propagated back into the top-level flattened
LogLevel, so you do not need to duplicate it in every subcommand.…