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

the default derive for booleans should also handle explicit --flag=true, --flag=false #5577

Open
2 tasks done
AaronKutch opened this issue Jul 9, 2024 · 2 comments
Open
2 tasks done
Labels
A-derive Area: #[derive]` macro API C-enhancement Category: Raise on the bar on expectations M-breaking-change Meta: Implementing or merging this will introduce a breaking change. S-waiting-on-decision Status: Waiting on a go/no-go before implementing

Comments

@AaronKutch
Copy link

AaronKutch commented Jul 9, 2024

Please complete the following tasks

Clap Version

4

Describe your use case

I searched around for related issues but only saw stuff like --flag/--no-flag which is not what this is about, or posts that were actually using this pattern in examples (I'm not the first one to need this, and have seen it elsewhere). In things such as automated scripts, sometimes I have a pattern of usage like passing format!("... --flag={val}"). However, if the value is a boolean, it does not allow things like --flag=true or --flag=false, only passing --flag by itself which requires annoying conditionals in the scripts. What I have to do is apply

#[clap(
      long,
      default_missing_value("true"),
      default_value("false"),
      num_args(0..=1),
      action = clap::ArgAction::Set,
)]

to every single boolean (note that it still allows --flag by itself and the absence of the flag to work as before). This is obviously quite annoying to use if there are a lot of booleans across many structs.

Describe the solution you'd like

Simply make this the default for booleans. I feel like this is a property of some other command argument systems I've seen before, but can't remember.

Alternatives, if applicable

No response

Additional Context

No response

@AaronKutch AaronKutch added the C-enhancement Category: Raise on the bar on expectations label Jul 9, 2024
@epage epage added M-breaking-change Meta: Implementing or merging this will introduce a breaking change. A-derive Area: #[derive]` macro API labels Jul 9, 2024
@epage
Copy link
Member

epage commented Jul 9, 2024

As proposed, this would be inappropriate due to the WARNING on num_args.

We could add require_equals(true) but I'm at least not a fan of the inconsistency of sometimes = is needed and sometimes it isn't.

This would also be a dramatic shift away from the builder API. This would also be too much policy to put in the builder API.

At this time, I am against this change but I'm ok with leaving this open to see what other feedback comes in, either way. We should note that an issue like this is more likely to collect feedback in favor of it than against because only people wanting this will be actively looking for it.

@epage epage added the S-waiting-on-decision Status: Waiting on a go/no-go before implementing label Jul 9, 2024
@AaronKutch
Copy link
Author

The way I think about it, is that the normal form would always be to give values to flags, except that since booleans have only two states, they can be used without arguments. I see however how it would cause problems with positional arguments. Maybe some kind of shorthand that works in place of #[derive(long)] but adds the functionality for booleans could be added?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-derive Area: #[derive]` macro API C-enhancement Category: Raise on the bar on expectations M-breaking-change Meta: Implementing or merging this will introduce a breaking change. S-waiting-on-decision Status: Waiting on a go/no-go before implementing
Projects
None yet
Development

No branches or pull requests

2 participants