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
Generate errors through Commander from client code #1632
Comments
Interesting question. Before Commander v4 there was not much extra that Commander did, but now as you noted there is The
The life-cycle hook for (I will think about convenient calling signatures to make use of the Commander built-in behaviours. Haven't got a discussion suggestion for that yet.) |
Experimenting with simple
A potential downside with simple "error" is the name may clash with option name used in legacy programs still using |
Can there be an optional |
Yes. Is that something you would use straight away? For unit tests, or for try/catch in calling code?
I was thinking just exit/throw depending on exitOverride. |
Yes, I'm using the error code in unit tests. I call exitOverride and configureOutput (passing in a function that writes to a small stream.Transform class to simulates stdio). I check the error code is the one I expect, and that some chunk of the text is what I expect. Duplicating or renaming the current behavior of _displayError would work very well for me, which means also having an optional exitCode, and perhaps more direct control over whether the help gets output. |
Experiment: shadowspawn@c223fae |
I like your experiment. |
Did this make it into v9.0.0? |
No, or at least not yet. This issue has not generated any likes or comments from other people, and I was not working on it. I'll take another look. |
Thanks! I'm doing ok using the undocumented internal function, and I think I've got a test that will catch when it changes so I can fix it up later. It would of course be better if I didn't have to release like that. :) |
Opened a draft PR: #1675 |
|
Commander v9 has been released. |
As mentioned on the Implied Options bug, I would like to perform inter-option checking after the main checks are done in
parse
. In these extra checks, I would like to throwInvalidArgumentError
, and have the same behavior as if I had done so in a coercion routine. In particular, I would like the exception to be caught, and depending on what I've done withexitOverride
andconfigureOutput
, either re-raised or printed appropriately andprocess.exit()
called.I'm open to lots of different designs. Here's a starting point for discussion:
The
check
routine gets called right beforeparse
returns, perhaps inside the existing catch block for options. Ifcheck
returns undefined, the existing opts object (which might have been modified by thecheck
method) is returned byopts()
, otherwise whatevercheck
returns will be used foropts()
.The text was updated successfully, but these errors were encountered: