rust cli_error ai_generated true

error: The argument '--flag-a' cannot be used with '--flag-b'

ID: rust/rust-clap-argument-conflict

Also available as: JSON · Markdown
90%Fix Rate
92%Confidence
50Evidence
2023-01-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1 active

Root Cause

Clap's argument conflicts_with configuration prevents mutually exclusive flags from being used together.

generic

Workarounds

  1. 90% success Use ArgGroup to define mutually exclusive argument groups
    use clap::{Arg, ArgGroup, Command};
    Command::new("app")
        .group(ArgGroup::new("mode")
            .args(["verbose", "quiet"])
            .required(false))

    Sources: https://docs.rs/clap/latest/clap/struct.ArgGroup.html

  2. 88% success Use clap derive with group attribute for clean conflict declaration
    #[derive(Parser)]
    struct Cli {
        #[arg(long, group = "verbosity")]
        verbose: bool,
        #[arg(long, group = "verbosity")]
        quiet: bool,
    }

    Sources: https://docs.rs/clap/latest/clap/_derive/index.html

Dead Ends

Common approaches that don't work:

  1. Remove all conflicts_with rules to allow any combination 65% fail

    The conflicts exist for a reason; allowing incompatible flags causes logic errors

Error Chain

Leads to:
Preceded by:
Frequently confused with: