Sub topic 10
Conflicts, requirements, and groups
This chapter assumes you can derive a Parser struct with options and flags, as in the earlier chapters. Types and value_parser check each value on its own. They can't say that --quiet makes no sense next to --verbose, or that --user is useless without --password. For rules between arguments, clap provides conflicts, requirements, and groups.
Conflicts
conflicts_with names another argument that must not appear in the same command line. Refer to the other argument by its id. In the derive API, the id is the field name.
use clap::Parser;
#[derive(Parser, Debug)]
struct Cli {
/// Suppress all output
#[arg(long, conflicts_with = "verbose")]
quiet: bool,
/// Print extra output
#[arg(long)]
verbose: bool,
}
fn main() {
let cli = Cli::parse();
println!("{cli:?}");
}Running app --quiet --verbose makes clap print an error and exit with code 2. Your code never sees the invalid combination.
Requirements
requires works the other way. The argument is allowed only when another argument is also present.
use clap::Parser;
#[derive(Parser, Debug)]
struct Login {
#[arg(long, requires = "password")]
user: Option<String>,
#[arg(long, requires = "user")]
password: Option<String>,
}
fn main() {
let login = Login::parse();
println!("{login:?}");
}Each side requires the other, so you can give both or neither. Giving only one is an error.
Groups
A group is a named set of arguments with one rule applied to the whole set. Two settings matter:
| Setting | Meaning |
|---|---|
| `required = true` | At least one member must be given. |
| `multiple = false` | At most one member may be given. |
Setting both means exactly one member. In the derive API, put #[group(...)] on a struct that derives Args. Then add it to the parser with #[command(flatten)].
use clap::{Args, Parser};
#[derive(Parser, Debug)]
struct Cli {
#[command(flatten)]
format: Format,
}
#[derive(Args, Debug)]
#[group(required = true, multiple = false)]
struct Format {
/// Print JSON
#[arg(long)]
json: bool,
/// Print YAML
#[arg(long)]
yaml: bool,
}
fn main() {
let cli = Cli::parse();
if cli.format.json {
println!("json");
} else {
println!("yaml");
}
}app with neither flag fails, and so does app --json --yaml. The --help output shows the group's rule in the usage line.
Common mistakes
- Misspelled ids.
conflicts_with = "verbos"refers to an argument that doesn't exist. CallCli::command().debug_assert()in a test, as shown in the testing chapter, to catch this. - Using
Optionwhen a group decides presence. Withmultiple = false, you may still have to check which member is set, as theif cli.format.jsoncheck above does. AnArgGroupcan't give you an enum. - Rules that hide real logic. If a rule depends on argument values, such as "
--portbelow 1024 needs root", check it in your own code instead.
Exercise
You want --input FILE and --stdin to be mutually exclusive, and you want the user to give exactly one of them. Which group settings do you use, and what happens when the user gives neither?
Show answer
Put both arguments in a group with required = true and multiple = false, for example #[group(required = true, multiple = false)] on a flattened Args struct. If the user gives neither, clap prints an error and exits with code 2.