-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Question: How to disable message "Run 'xxxxx --help' for usage." ? #1111
Comments
The
Setting this to |
@jpmcb Thanks
errors like:
are useful for caller/user to debug problems. Currently I see, there is no way to achieve this. But this is useful in some cases. I would like to raise a PR for this behaviour, if maintainers wave green flag |
Since errors are returned from within the Cobra API upon failure, I believe that this can be handled by your top level application code. When defining your command, disable both error output and command usage output: var myCommand = &cobra.Command{
SilenceErrors: true,
SilenceUsage: true,
} Then, when calling execute on this command, capture and print the error message however you like: if err := myCommand.Execute(); err != nil {
// There was an error! Print the error message and exit. Or do whatever you'd like!
// This includes errors like "invalid argument" etc.
fmt.Println(err)
os.Exit(1)
} This way, you maintain complete control of the error messaging and output. The downstream client "caller" you have can then assert on the exit status of the command or the output itself. |
@jpmcb Thanks. Your solution works for me. |
I am working on plugin system where caller calls commands on plugin binary (built using cobra). ex:
plugin run something
.Caller want to know if any error happens while running command and it doesn't make sense sending message
Run 'xxxxx --help' for usage.
to caller. Is there any way to disable this message ?The text was updated successfully, but these errors were encountered: