TeXitoi / structopt

Parse command line arguments by defining a struct.
Other
2.7k stars 149 forks source link

How to create a global flag #503

Closed ThorstenHans closed 2 years ago

ThorstenHans commented 2 years ago

I wanted to create a global debug flag, so that all sub-commands accept this flag. However, adding global = true turns my flag into an argument which requires a value.

From documentation, I don't see another way how I can turn a flag into a global flag. Any suggestions here?

struct FooBar {
    #[structopt(long, short, help = "Prints debug information", global = true, env = "STRINGS__DEBUG")]
    debug: bool,
    input: String,
    #[structopt(subcommand)]
    cmd: SubCommand
}

when executing with this configuration, and providing just --debug, the app fails with the following error:

error: The argument '--debug <debug>' requires a value but none was supplied
TeXitoi commented 2 years ago

That's not global fault, that's env that change your flag to option. There is no really any solution for this using structopt and clapv2, but clapv3 (in alpha stage) should propose a solution for this.

epage commented 2 years ago

Don't know if we've solved every problem here but https://github.com/clap-rs/clap/issues/2539 was closed by https://github.com/clap-rs/clap/pull/2664

ThorstenHans commented 2 years ago

🙏🏼 Thanks for pointing me to env.