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

Support 'no' prefix for negatable flags (e.g. --verbose or --noverbose) #16

Open
ewmailing opened this issue Mar 25, 2018 · 1 comment

Comments

@ewmailing
Copy link

This is a feature request for the "no" prefix modifier where you can prepend 'no' to a flag, and it inverts its value.

For example, imagine a --verbose flag that enables debugging chatter. A user can explicitly use --noverbose to do the opposite action that disables the debugging chatter.

I've used this feature in a some other argument parsing libraries for other languages, so I know the feature has been in the wild. (I'm currently trying to port a Perl utility that uses the standard GetOpt::Long which has the feature.)

I've tried emulating this with 2 flags in a mutex, but it is clumsy. The first problem is you end up with two separate flags with separate variables that still don't know that they need to be inversions of one another. So they can easily be out of sync. In the base case, where neither switch is set, then both are nil. If I set defaults to each, then when the user sets the non-default way, then both flags result in being the same value (since they don't have a direct relationship). In either case, this requires me to write extra setup code to check all the permutations and explicitly set both counterpart values in case I'm inconsistent about which variable I use to compare logic later in my app.

The second problem is you need to write two separate description entries, which can be redundant and clutter things. It seems like there should be a way to combine the entries in some ways. (The switch itself can be abbreviated as --[no]verbose.)

Thank you

@mpeterv
Copy link
Owner

mpeterv commented Mar 25, 2018

Something like this should work currently:

parser:flag "--verbose"
parser:flag "--noverbose"
   :target "verbose"
   :action "store_false"

It's a little, uh, verbose this way though, and there are still two descriptions, so it would be nice to have an easy built-in solution anyway.

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