feat: Add SetHelpExitCode to allow custom exit codes on help#2348
feat: Add SetHelpExitCode to allow custom exit codes on help#2348maishivamhoo123 wants to merge 1 commit intospf13:mainfrom
Conversation
ccoVeille
left a comment
There was a problem hiding this comment.
This won't work the way you think.
It won't return the expected exit code.
It will only return exit 1 and an error message.
Note: I'm not saying the feature is a bad idea (or a good one).
I'm just saying it doesn't do work
7163617 to
a04c820
Compare
|
@ccoVeille Thank you for the guidance! I've updated the logic to use a custom ExitError struct. This ensures the specific integer code can be retrieved via errors.As instead of defaulting to a generic error 1. Apologies for the oversight in the previous commit. I would appreciate your review when you have a moment. |
|
This won't work either... Take a look at CheckErr method and how code use it. I feel like your PR requires more iterations. Adding a feature like this to such a project might not be easy. let's wait for @marckhouzam and his feedback But if you have a need for this, maybe you could simply consider defining a custom helper command to your code using cobra (so without change in cobra) and then call os.Exit with the code you want. |
Description
Addresses issue #2338.
Currently,
cmd --helpreturns exit code 0. This causes scripts usingeval $(cmd)to crash because the shell tries to execute the help text as commands. This PR allows developers to set a custom exit code (e.g., 2) when help is invoked, preventing this behavior.Changes
helpExitCodefield toCommandstruct.SetHelpExitCode(int)method.ExecuteCto return a specific error ifhelpExitCodeis set and help is invoked.TestHelpExitCode.Example Usage