pyinvoke / invoke

Pythonic task management & command execution.
http://pyinvoke.org
BSD 2-Clause "Simplified" License
4.38k stars 367 forks source link

FR: Exception that when raised prints the task usage (print_task_help) #857

Open albertvaka opened 2 years ago

albertvaka commented 2 years ago

It's a common pattern in CLI tools to print the help/usage if the arguments given are inconsistent or not valid.

This is something that can only be detected during the execution when the arguments are parsed and validated.

It would be useful to have a way to trigger the print_task_help function from the task itself (eg: by raising a specific exception) when that happens.

neozenith commented 2 years ago

So if I am understanding this correctly, there are two parts to this...

  1. Out of the box if a task is invoked, but uses incorrect arguments and flags then print the usage help.
  2. Also if a task is invoked, all arguments are passed in correctly but inside the task function, some other validation means it is being used incorrectly you want to throw an InvalidUsageException(...) that renders the message and also displays the usage help?

Interesting idea.

It would be most helpful if yourself or anyone reading this could open a PR and link this issue, but also provide appropriate test coverage.

albertvaka commented 2 years ago

I assumed (1) was already the case, but I just saw it is not. So you are right, there are two parts to this FR.

To expand a bit more on (1), I do think it would be an improvement to error out if an unexpected argument is passed in (eg: because of a typo) instead of running and potentially doing the wrong thing. I even think it should be the default behavior, but I'm not sure if you would like to make that breaking change.

I can't promise I will be able to open a PR myself anytime soon, though, in case anyone else wants to give it a shoot :)

daltonv commented 3 months ago

The pull request seems like a good implementation of this feature to me. Anything we can do to move forward with this issue?