#Mutually exclusive clap argument parsing using derive

4 messages · Page 1 of 1 (latest)

warm pier
#

I'm using clap to parse arguments to my program, and I want to make some options mutually exclusive. Currently, if I add both arguments then I get an error saying that they cannot be used together (Great!), but if I only use one of them - or none of them - then I get an error saying that I am missing a required argument.

Here is what I currently have:

use clap::{Args, Parser};

#[derive(Parser, Debug)]
pub struct Cli {
    #[command(flatten)]
    pub commands: Commands,
}

#[derive(Args, Debug)]
#[group(required = true, multiple = false)]
pub struct Commands {
    /// Name of the task you want to add
    #[arg(short, long)]
    pub add: String,

    /// Name of the task you want to remove
    #[arg(short, long)]
    pub remove: String,
}

pub fn parse_args() -> Cli {
    Cli::parse()
}

Thanks for any help 🙂
(also how do I format my code for discord 😅 )

boreal stag
#

Idk about clap, but I can tell you how to format

#

```rs
code here
```

warm pier