Skip to content
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

Closed
dev-drprasad opened this issue May 8, 2020 · 4 comments
Closed

Comments

@dev-drprasad
Copy link

dev-drprasad commented May 8, 2020

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 ?

@jpmcb
Copy link
Collaborator

jpmcb commented May 8, 2020

The Command struct has the following field:

// SilenceErrors is an option to quiet errors down stream.
SilenceErrors bool

Setting this to true will disable the "Run ... --help' for usage" output. Hope this helps!

@dev-drprasad
Copy link
Author

@jpmcb Thanks

SilenceErrors: true completely silence errors.

errors like:

Error: invalid argument "abcd" for "hashicorp run"
Error: accepts 1 arg(s), received 2

are useful for caller/user to debug problems.
Just "Run ... --help' for usage" is unnecessary when cobra commands executed by other program.

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

@jpmcb
Copy link
Collaborator

jpmcb commented May 10, 2020

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.

@dev-drprasad
Copy link
Author

@jpmcb Thanks. Your solution works for me.

@dev-drprasad dev-drprasad changed the title Question: How to disable message Run 'xxxxx --help' for usage. ? Question: How to disable message "Run 'xxxxx --help'" for usage. ? May 11, 2020
@dev-drprasad dev-drprasad changed the title Question: How to disable message "Run 'xxxxx --help'" for usage. ? Question: How to disable message "Run 'xxxxx --help' for usage." ? May 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants