Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Global arguments are silently ignored with nested subcommands #3428

Closed
2 tasks done
kuecks opened this issue Feb 9, 2022 · 3 comments · Fixed by #3429
Closed
2 tasks done

Global arguments are silently ignored with nested subcommands #3428

kuecks opened this issue Feb 9, 2022 · 3 comments · Fixed by #3429
Labels
A-parsing Area: Parser's logic and needs it changed somehow. C-bug Category: Updating dependencies E-easy Call for participation: Experience needed to fix: Easy / not much

Comments

@kuecks
Copy link

kuecks commented Feb 9, 2022

Please complete the following tasks

Rust Version

rustc 1.58.0-nightly (65f3f8b22 2021-11-21)

Clap Version

3.0.14

Minimal reproducible code

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

#[derive(Parser, Debug)]
struct Options {
    #[clap(subcommand)]
    command: Command1,
}

#[derive(Subcommand, Debug)]
enum Command1 {
    A(Command1AOptions),
}

#[derive(Args, Debug)]
struct Command1AOptions {
    #[clap(subcommand)]
    command: Command2,
    #[clap(long, global = true)]
    flag1: bool,
}

#[derive(Subcommand, Debug)]
enum Command2 {
    B {
        #[clap(subcommand)]
        command: Command3,
        #[clap(long, global = true)]
        flag2: bool,
    },
}

#[derive(Subcommand, Debug)]
enum Command3 {
    C(Command3COptions),
}

#[derive(Args, Debug)]
struct Command3COptions {}

fn main() {
    let options = Options::parse();
    dbg!(options);
}

Steps to reproduce the bug with the above code

cargo run -- a b c --flag2 --flag1

This passes both global flags at the lowest level (flag1 is declared with a, flag2 is declared with b)

Actual Behaviour

flag1 is correctly set to true, but flag2 is false!

    Finished dev [unoptimized + debuginfo] target(s) in 0.04s
     Running `target/debug/clap_test a b c --flag2 --flag1`
[src/main.rs:42] options = Options {
    command: A(
        Command1AOptions {
            command: B {
                command: C(
                    Command3COptions,
                ),
                flag2: false,
            },
            flag1: true,
        },
    ),
}

Expected Behaviour

Both flag1 and flag2 should be true

Additional Context

No response

Debug Output

debug.txt

@kuecks kuecks added the C-bug Category: Updating dependencies label Feb 9, 2022
@epage epage added A-parsing Area: Parser's logic and needs it changed somehow. E-easy Call for participation: Experience needed to fix: Easy / not much labels Feb 9, 2022
@epage
Copy link
Member

epage commented Feb 9, 2022

I think I've identified root cause and am working on the fix. Our recursive discovery of global flags appears to be broken,

epage added a commit to epage/clap that referenced this issue Feb 9, 2022
epage added a commit to epage/clap that referenced this issue Feb 9, 2022
@kuecks
Copy link
Author

kuecks commented Feb 9, 2022

@epage Wow that was very fast! Thank you very much!

@epage
Copy link
Member

epage commented Feb 9, 2022

The fix was fast but the part that might be slow is the release. I generally aim to release as soon as a user-facing change is in but I'm batching up some deprecations, so focusing on finish that first. Sorry for the delay.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-parsing Area: Parser's logic and needs it changed somehow. C-bug Category: Updating dependencies E-easy Call for participation: Experience needed to fix: Easy / not much
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants