Skip to content

Commit f0fd049

Browse files
committed
Improve boolean flags handling.
When a function has a keyword argument with a boolean default, the parser now automatically creates the `store_true` or `store_false` action(if not action was provided in the `arguments` keyword definition. Fixes #5 Signed-off-by: Pedro Algarvio <[email protected]>
1 parent c2d6284 commit f0fd049

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

changelog/5.improvement.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
When a function has a keyword argument with a boolean default, the parser now automatically creates the `store_true` or `store_false` action(if not action was provided in the `arguments` keyword definition.

src/ptscripts/parser.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,8 +377,17 @@ def command(
377377
# Keyword argument
378378
kwargs["dest"] = parameter.name
379379
if "type" not in kwargs:
380-
if param_type not in (bool,):
380+
if param_type is not bool:
381381
kwargs["type"] = param_type
382+
elif "action" not in kwargs:
383+
action = None
384+
if parameter.default is True:
385+
action = "store_false"
386+
elif parameter.default is False:
387+
action = "store_true"
388+
if action is not None:
389+
kwargs["action"] = action
390+
382391
kwargs["default"] = parameter.default
383392
if "help" in kwargs:
384393
if parameter.default is not None:

0 commit comments

Comments
 (0)