Skip to content
Discussion options

You must be logged in to vote

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:

myapp --debug subcommand --sub-option value
myapp subcommand --debug --sub-option value
myapp subcommand --sub-option value --quiet

The value is propagated back into the top-level flattened LogLevel, so you do not need to duplicate it in every subcommand.…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by Lege19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants