-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add special handling for optional boolean flags
- Loading branch information
1 parent
1a5151a
commit 09bdead
Showing
3 changed files
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -131,6 +131,18 @@ def _populate_parser(func, parser, parsers=None): | |
type_ = _get_type(func, name, doc, hints) | ||
if param.kind == param.VAR_KEYWORD: | ||
raise ValueError('**kwargs not supported') | ||
if type_.type == bool and param.default != param.empty: | ||
# Special case: just add parameterless --name and --no-name flags. | ||
parser.add_argument('--' + name, | ||
action='store_true', | ||
default=param.default, | ||
# Add help if available. | ||
**kwargs) | ||
parser.add_argument('--no-' + name, | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
evanunderscore
Author
Collaborator
|
||
action='store_false', | ||
default=param.default, | ||
dest=name) | ||
continue | ||
if type_.container: | ||
assert type_.container == list | ||
name_or_flag = '--' + name | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
"""Example showing boolean flags in defopt. | ||
Optional boolean parameters are automatically converted to | ||
``--name`` and ``--no-name`` flags which take no arguments | ||
and store `True` and `False` respectively. | ||
Code usage:: | ||
>>> main('hello!', upper=False, repeat=True) | ||
Command line usage:: | ||
$ python booleans.py 'hello!' --no-upper --repeat | ||
""" | ||
import defopt | ||
|
||
|
||
def main(message, upper=True, repeat=False): | ||
"""Example function with boolean flags. | ||
:param str message: Message to display | ||
:param bool upper: Display the message in upper case | ||
:param bool repeat: Display the message twice | ||
""" | ||
if upper: | ||
message = message.upper() | ||
for _ in range(1 + repeat): | ||
print(message) | ||
|
||
|
||
if __name__ == '__main__': | ||
defopt.run(main) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
maybe there is a way to make this show up in the docs as
--param/--no-param