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

Short-hands boolean #468

Closed
Milo123459 opened this issue Feb 15, 2021 · 5 comments
Closed

Short-hands boolean #468

Milo123459 opened this issue Feb 15, 2021 · 5 comments

Comments

@Milo123459
Copy link

I want to make it so I can do -d or --dry, i know how to do alias' but I don't want to make it so you have to specify true. For example:
--dry would mean it is true
--dry true would mean it is true
without the flag it'd be false.

@TeXitoi
Copy link
Owner

TeXitoi commented Feb 15, 2021

use structopt::StructOpt;

#[derive(Debug, StructOpt)]
struct Opt {
    #[structopt(long, short, min_values(0), max_values(1), possible_value("true"))]
    dry: bool,
}

fn main() {
    println!("{:?}", Opt::from_args());
}

But I suspect you also want to support -d false, and I don't have the tricky idea just now.

@Milo123459
Copy link
Author

Yes, I do want -d false, and do you mean that's not possible in the lib right now? or, you haven't got it off the top of your head

@TeXitoi
Copy link
Owner

TeXitoi commented Feb 15, 2021

That's not really made for that, but a workaround might exists.

@TeXitoi
Copy link
Owner

TeXitoi commented Feb 15, 2021

use structopt::StructOpt;

#[derive(Debug, StructOpt)]
struct Opt {
    #[structopt(long, short)]
    dry: Option<Option<bool>>,
}
impl Opt {
    fn dry(&self) -> bool {
        match self.dry {
            None => false,
            Some(None) => true,
            Some(Some(a)) => a,
        }
    }
}

fn main() {
    println!("{:?}", Opt::from_args().dry());
}

Not so elegant, but works.

@Milo123459
Copy link
Author

Thanks for the help 😉

bors bot added a commit to meilisearch/meilisearch that referenced this issue Dec 8, 2021
1984: Support boolean for the no-analytics flag r=Kerollmops a=Kerollmops

This PR fixes an issue with the `no-analytics` flag that was ignoring the value passed to it, therefore a `no-analytics false` was just understood as a `no-analytics` and was effectively disabling the analytics instead of enabling them. I found [a closed issue about this exact behavior on the structopt repository](TeXitoi/structopt#468) and applied it here.

I don't think we should update the documentation as it must have worked like this from the start of this project. I tested it on my machine and it is working great now. Thank you `@nicolasvienot` for this issue report.

Fixes #1983.

Co-authored-by: bors[bot] <26634292+bors[bot]@users.noreply.github.com>
Co-authored-by: Clément Renault <clement@meilisearch.com>
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