You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm trying to establish what the correct pattern is for exiting a command with a non zero return code.
I can use RunE, but the normal effect of that is that the "usage" is printed out, since there's no distinction between an error from validating the args (where you would want usage to result) and an error from the operation of your command. You can SilenceUsage, but then you get no usage help on invalid args either, so that doesn't feel like the right way.
The examples generated by the CLI all use Run instead of RunE. The implication would appear to be that I have to manually os.Exit(1) if I want my command to "fail" and return a non zero return code. But that feels wrong, it's going to skip all of the post run actions etc.
Looking at a couple of examples, it seems like managing an exit code outside of cobra seems to be a solution used in a few places, so letting the command run to completion and then once cobra has returned calling os.Exit() with the value that's stored in some global variable somewhere.
What's the idiomatic usage here?
Thanks
The text was updated successfully, but these errors were encountered:
The SetFlagErrorFunc allows a hook when the error is caused by bad flags, but that doesn't get fired for invalid arguments other than flags (so validation done by the "Args" field in Command). Then use RunE, so you can return an error from the command and it will cause a non zero return code.
This has always somewhat surprised me in cobra. I'd have thought that the normal desired behaviour would be "invalid flags or args or command name, print error then usage, errors from the command, just print error". I've always slightly struggled to get that behaviour.
I'm trying to establish what the correct pattern is for exiting a command with a non zero return code.
I can use RunE, but the normal effect of that is that the "usage" is printed out, since there's no distinction between an error from validating the args (where you would want usage to result) and an error from the operation of your command. You can SilenceUsage, but then you get no usage help on invalid args either, so that doesn't feel like the right way.
The examples generated by the CLI all use Run instead of RunE. The implication would appear to be that I have to manually os.Exit(1) if I want my command to "fail" and return a non zero return code. But that feels wrong, it's going to skip all of the post run actions etc.
Looking at a couple of examples, it seems like managing an exit code outside of cobra seems to be a solution used in a few places, so letting the command run to completion and then once cobra has returned calling os.Exit() with the value that's stored in some global variable somewhere.
What's the idiomatic usage here?
Thanks
The text was updated successfully, but these errors were encountered: