clap-rs / clap

A full featured, fast Command Line Argument Parser for Rust
docs.rs/clap
Apache License 2.0
13.66k stars 1.02k forks source link

Argument conflict ignored on `global`s specified at different levels #5335

Closed abesto closed 5 months ago

abesto commented 5 months ago

Please complete the following tasks

Rust Version

1.75.0

Clap Version

4.4.12

Minimal reproducible code

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=8fa6a4126712c787b6170a7663e929e7

use clap::{Parser, Subcommand, Args};

#[derive(Args, Debug)]
#[group(multiple = false)]
struct Group {
    #[clap(short, global = true)]
    a: bool,
    #[clap(short, global = true)]
    b: bool,
}

#[derive(Subcommand, Debug)]
enum Sub {
    Foo
}

#[derive(Parser, Debug)]
struct Cli {
    #[clap(flatten)]
    g: Group,
    #[command(subcommand)]
    sub: Sub,
}

pub fn main() -> anyhow::Result<()> {
    dbg!(Cli::try_parse_from(["cli", "-a", "foo", "-b"])?);
    dbg!(Cli::try_parse_from(["cli", "-a", "-b", "foo"])?);
    Ok(())
}

Steps to reproduce the bug with the above code

cargo run / run the Playground snippet

Actual Behaviour

The first line of main (cli -a foo -b) doesn't explode, but the second line does.

Expected Behaviour

The first line should explode as well.

Additional Context

I think this is another manifestation of https://github.com/clap-rs/clap/issues/1546#issuecomment-1198809765

Debug Output

No response

epage commented 5 months ago

Since this is a larger issue than the original title, I've renamed #1546 so we can track all of this in one place and closing in favor of that.

abesto commented 5 months ago

Yep, that makes sense. Thanks!