Skip to content

Commit ead8d93

Browse files
committed
fix: program: print task-help if ParseError occurs
This will f.e. print the help of a task when it is not given its mandatory args: ```bash > # Prints help if my_task expects mandatory positional arg > inv my_task ``` If no name is available for the context, f.e. the core/None context, we skip the specific task print since this means it was not a 'user' task which failed. Signed-off-by: laurensmiers <[email protected]>
1 parent 26d243c commit ead8d93

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

invoke/program.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,8 @@ def run(self, argv: Optional[List[str]] = None, exit: bool = True) -> None:
403403
# problems.
404404
if isinstance(e, ParseError):
405405
print(e, file=sys.stderr)
406+
if e.context and e.context.name:
407+
self.print_task_help(e.context.name)
406408
if isinstance(e, Exit) and e.message:
407409
print(e.message, file=sys.stderr)
408410
if isinstance(e, UnexpectedExit) and e.result.hide:

tests/program.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,23 @@ def prints_help_for_task_only(self):
653653
for flag in ["-h", "--help"]:
654654
expect("-c decorators {} punch".format(flag), out=expected)
655655

656+
def prints_help_if_no_mandatory_arg(self):
657+
expected = """
658+
Usage: invoke [--core-opts] punch [--options] [other tasks here ...]
659+
660+
Docstring:
661+
none
662+
663+
Options:
664+
-h STRING, --why=STRING Motive
665+
-w STRING, --who=STRING Who to punch
666+
667+
""".lstrip()
668+
expected_error= """
669+
'punch' did not receive required positional arguments: 'who', 'why'
670+
""".lstrip()
671+
expect("-c decorators punch", out=expected, err=expected_error)
672+
656673
def works_for_unparameterized_tasks(self):
657674
expected = """
658675
Usage: invoke [--core-opts] biz [other tasks here ...]

0 commit comments

Comments
 (0)