Add override to allow throw instead of process.exit#1040
Add override to allow throw instead of process.exit#1040shadowspawn merged 9 commits intotj:developfrom
Conversation
|
One of the prompts for adding this feature was to simplify writing unit tests. The test file for exitOverride itself (on a different branch) is: |
|
Thanks @abetomo 😄 |
|
Added support for async calls in executableSubcommand. |
|
The implementation already landed in the Jest PR. This PR now effectively promotes it from private Added description to README. Suggested line for CHANGELOG:
|
|
Hello, I was looking for a solution to prevent "--help" from exiting the CLI. I tried using |
|
@lxsmnsyc |
I have found a work around to my problem. I have to take a look at commander's index js to know how it executes commands, and to understand the behavior in general :) |
Pull Request
Problem
Commander has multiple calls to
process.exitwhich is hard to override to use as a library and parse multiple strings, as parsing errors or --help terminate the process. Doing a throw instead would allow appropriate handling and carry on.Issues: #945 #575 #934
Solution
Add
.exitOverride(callback)routine to do custom handling of code that would otherwise callprocess.exit(). The callback is optional and the default behaviour is to throw an instance of CommanderError which includes the exitCode, a string code, and a message.This allows writing code like:
(The routine name
exitOverrideis a bit implementation specific rather than friendly, but this is an optional feature and not sure how much or how it will be used.)Limitations
process.exitas different pattern requiredToDo
feature/jestbranchMaking the code visible for discussion and review.