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

Implement and/or document flags (boolean option) with automatic sub-flags #12

Closed
flying-sheep opened this issue Apr 15, 2016 · 6 comments
Assignees
Milestone

Comments

@flying-sheep
Copy link

flying-sheep commented Apr 15, 2016

flags are CLI parameters that take no argument. they usually have an inversion (--no-<name> or +g)

i guess #2 is about customization and short flags/options in general. this is about the automatic version of flag features.

flags with an automatic --no-<name> version would be cool.

import defopt

def my_cmd(legend=True):
    print(legend)

if __name__ == '__main__':
    defopt.run(my_cmd)
$ python my_cmd.py --legend
True
$ python my_cmd.py --no-legend
False

once short options/flags (#2) are there, an automatic flag inversion using +g would also be cool:

$ python my_cmd.py +g
False
@flying-sheep flying-sheep changed the title Implement and/or document flags (with automatic sub-flags) Implement and/or document flags (boolean option) with automatic sub-flags Apr 15, 2016
@evanunderscore evanunderscore added this to the 2.0.0 milestone Apr 16, 2016
@evanunderscore
Copy link
Collaborator

I think I initially had reasons for not doing this but they all seem a bit weak now. I like the idea of doing this automatically.

I personally find negations using + to be counter-intuitive, but I'm aware that they're used in a number of places, so I'm not against giving people the option to use them if we can find a simple way of doing it. I'll keep this in mind when looking at #2.

@flying-sheep
Copy link
Author

an alternative would be negations using the uppercase short opt.

@evanunderscore
Copy link
Collaborator

evanunderscore commented Apr 23, 2016

What's the point of the inverted flags? I know they're used in some places, but they just seem confusing to me. Here's an actual example from pip install -h:

  --compile                   Compile py files to pyc
  --no-compile                Do not compile py files to pyc

Obviously one of these must be the default, but I have no way of telling. Since one of them is implied by not specifying it, why do we need the flag at all?

Here's another flag for the same command:

--no-clean                  Don't clean up build directories.

Since there is no corresponding --clean flag, I know that the default behavior must be to clean up build directories.

@flying-sheep
Copy link
Author

flying-sheep commented Apr 23, 2016

with both, the default can be changed anytime (--flag means True, --no-flag means False)

and as the later option trumps, you can override stuff in aliases, …

alias ls='ls -l'
$ ls
drwxr-xr-x 1 phil users  314  6. Mär 12:12 bin/
$ ls -x
bin/

@evanunderscore
Copy link
Collaborator

I've had a shot at an implementation of this to use as a discussion point. Take a look at 09bdead.

Some points to discuss:

  • This only applies to optional arguments; a positional boolean argument is still specified as something accepted by _parse_bool.
  • The help message only shows documentation for the --name flag; the --no-name flag is blank but comes immediately after the other flag so should be clear enough.
  • This allows for you to have None or some other value as the default, so neither --name nor --no-name are necessarily implied to match the default behavior (if you really want to do that).

@evanunderscore evanunderscore self-assigned this Apr 25, 2016
@evanunderscore
Copy link
Collaborator

This has been implemented and will be in 2.0. Discussion on how to handle short flags can continue in #2 if necessary.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants