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

Please allow booleans to be set false with no- prefix #958

Closed
Nuru opened this issue Sep 11, 2019 · 2 comments
Closed

Please allow booleans to be set false with no- prefix #958

Nuru opened this issue Sep 11, 2019 · 2 comments
Labels
area/flags-args Changes to functionality around command line flags and args

Comments

@Nuru
Copy link

Nuru commented Sep 11, 2019

@eparis wrote

Basically, booleans can only take arguments via --bool=[true|false] or for short names -b=[true|false]. You cannot use --bool [true|false] nor can you use the shorthand -b[true|false]

This behavior is unlike any other tool I have encountered. The behavior I am used to and consider "normal" for Unix commands, for boolean options, is:

  • For short flags, like -b, the option toggles the default value when present. There is generally no way to affirmatively assert the default value with a short flag, though some commands allow it with the same option in the other case, e.g. -B
  • For long flags, like --bool, the named option sets the flag true, and the option with a no- prefix sets the flag false, e.g. --no-bool. The "usage" for such a flag would be something like
-b, --[no-]bool 

See, for just one example, git-merge. (Truthfully, most commands are written so there is just a default value and a flag which toggles the default, with the flag being prefixed with no- when it makes for better style, such as --no-undefined Do not allow undefined values. However, when configuration can come from many sources, a command-line option re-asserting default behavior and overriding the other configuration sources is a valuable feature.)

The current state where a flag with an argument is written -f file but a boolean cannot be written -b true is painfully inconsistent as well as awkward, to the point where other options parsers simply do not allow boolean short flags to take arguments. I suggest that instead you automatically create --bool and --no-bool options for all booleans with long forms and if possible provide some kind of warning against creating a short option that has no effect.

Note: I have no problem allowing --bool=false and I support continuing to allow that as it can make scripting easier. On the other hand, --no-bool=true is just plain confusing and should not be allowed.

@umarcor
Copy link
Contributor

umarcor commented Sep 11, 2019

Hi @Nuru! I think that this isssue belongs to spf13/pflag, which is the repo used by cobra to define flags. See, for example spf13/pflag#214, spf13/pflag#213, spf13/pflag#139, spf13/pflag#171...

@jharshman jharshman added the area/flags-args Changes to functionality around command line flags and args label Oct 3, 2019
@jharshman
Copy link
Collaborator

@Nuru thanks for submitting your issue. It looks like this has already been brought up in spf13/pflag#214. Going to close this as the spf13/pflag is the appropriate place to track it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/flags-args Changes to functionality around command line flags and args
Projects
None yet
Development

No branches or pull requests

3 participants