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

How to define mutually competing flags #320

Closed
tailhook opened this issue Jan 6, 2020 · 4 comments
Closed

How to define mutually competing flags #320

tailhook opened this issue Jan 6, 2020 · 4 comments

Comments

@tailhook
Copy link

tailhook commented Jan 6, 2020

There is a common pattern with flags which is like:

 --debug / --no-debug    enable or disable debugging

or similarly -v and -q could stand as verbose and quiet.

The only way I can find to implement it is using two different flags, but there are two problems with that:

  1. The last option should win. With two options I have to define which one has a higher priority over the other.
  2. Docs are a bit long (especially if you have many such flags)

Is there a way to implement this now? Will such a feature be accepted?

@CreepySkeleton
Copy link
Collaborator

Is there a way to implement this now?

AFAIK, no, sorry. Also, related #126

Will such a feature be accepted?

This depends a good deal on the precise design. If you're willing to try implementing this variant, which both @TeXitoi and I agree with, it will be accepted. If you have any other thoughts/improvements - we may need to discuss them first.

@tailhook
Copy link
Author

tailhook commented Jan 6, 2020

Well this one looks promising but I'm not sure it's good enough:

#[derive(StructOpt)]
struct DebugOpt {
    #[structopt(long = "debug", parse(from_occurences)]
    on: u8,

    #[structopt(long = "no-debug", parse(from_occurences)]
    off: u8,
}

Then the following:

command --no-debug --no-debug --debug

Would disable debug instead of enabling. I.e. I'm not sure how to implement last argument wins semantics with collapse. (It's also a lot more verbose than I'd like for such common functionality, and can't be abstracted away, as far as I understand, because the inner structure have exact argument names).

@tailhook
Copy link
Author

tailhook commented Jan 6, 2020

Oh well, actually:

    #[structopt(long, help="enabled debug (disable with --no-debug)")]
    pub debug: bool,
    #[structopt(long, overrides_with="debug", hidden=true)]
    pub _no_debug: bool,

Works pretty well (the key here is overrides_with, and _no_debug field can be ignored).

The --no-debug is hidden because otherwise it is put in a different place because of sorting.

I would still probably want better help, but this is not that critical and should probably be done after clap 3.0.

@tailhook tailhook closed this as completed Jan 6, 2020
@CreepySkeleton
Copy link
Collaborator

Yeah, I was mostly answering to -q -v question. I believe the --debug/--no-debug thing must be done in clap, there's an issue for that already.

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