Skip to content

How to define global options but only for a subset of subcommands? #5578

Answered by epage
dhruvmanila asked this question in Q&A
Discussion options

You must be logged in to vote

At this time, it is unsupported. Personally, I find this case odd as the argument is no longer truly "global". I'd recommend considering whether the argument is global because it is universally applicable or global for code reuse. The fact that exceptions are being made makes it seem like the latter and it seems like the best course would be to flatten in each command.

That said, you can workaround this on your side by

#[derive(Debug, Parser)]
pub struct Args {
    #[command(subcommand)]
    pub(crate) command: Command,
    #[clap(flatten)]
    pub(crate) global_options: Option<GlobalConfigArgs>,
}

You can do global_option.is_some() && !command.supports_globals() and then error.

We do wan…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@dhruvmanila
Comment options

Answer selected by dhruvmanila
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