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

Combining env and flag arguments doesn't work as expected #305

Closed
Kerollmops opened this issue Dec 10, 2019 · 2 comments
Closed

Combining env and flag arguments doesn't work as expected #305

Kerollmops opened this issue Dec 10, 2019 · 2 comments

Comments

@Kerollmops
Copy link
Contributor

Kerollmops commented Dec 10, 2019

When we add the env attribute on a boolean field, the command line argument, that must be considered as a flag, is considered as an attribute requiring a value.

#[derive(StructOpt)]
pub struct Opt {
    /// Do not send analytics.
    #[structopt(long, env = "NO_ANALYTICS")]
    pub no_analytics: bool,
}

Here is what StructOpt generate with the above configuration:

xxx-http 0.8.4

USAGE:
    xxx [OPTIONS]

FLAGS:
    -h, --help       Prints help information
    -V, --version    Prints version information

OPTIONS:
        --no-analytics <no-analytics>    Do not send analytics. [env: NO_ANALYTICS=]

And here is what I expected:

xxx-http 0.8.4

USAGE:
    xxx [OPTIONS]

FLAGS:
    -h, --help             Prints help information
        --no-analytics     Do not send analytics. [env: NO_ANALYTICS]
    -V, --version          Prints version information

Is this behavior expected or is it not? Thank you for you help.

@CreepySkeleton
Copy link
Collaborator

CreepySkeleton commented Dec 11, 2019

It isn't really clear what the expected behavior is from your comment. I'm going to assume it's all about

  • flags (long bool fields) do not take any value, the very fact --no-analytics is present raises the flag. This is true in structopt
  • environment fallbacks for flags don't take any value. More specifically, the very fact the fallback env is defined (i.e you have export NO_ANALYTICS="" someplace above/before) raises the flag, no matter what the env is assigned with.

Now, let's remember that structopt works on top of clap. If you use some feature with structopt - this feature must be available in clap in the first place. I believe what you're asking for is not possible with clap because Arg::env implies Arg::takes_value(true). Even if you reset takes_value(false) back it won't work, unfortunately.

I'm closing this since it's entirely about clap, structopt has nothing to do with it.

Btw, there is such a request in clap already.

@Kerollmops
Copy link
Contributor Author

Kerollmops commented Dec 11, 2019

Ho! I already checked if it there was a related issue on Clap and didn't find anything.
I fallback on the issue page of StructOpt because I thought it was done by it, which is a dumb thought as this is clearly Clap which produces this help message.

I apologize and subscribed to the clap issue at the same time :) Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants