-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add override to allow throw instead of process.exit #1040
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: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
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.exit
which 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
exitOverride
is 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.exit
as different pattern requiredToDo
feature/jest
branchMaking the code visible for discussion and review.