Skip to content

Silence Cobra usage on errors#41

Merged
mislav merged 3 commits intomasterfrom
silence-usage
Oct 31, 2019
Merged

Silence Cobra usage on errors#41
mislav merged 3 commits intomasterfrom
silence-usage

Conversation

@mislav
Copy link
Copy Markdown
Contributor

@mislav mislav commented Oct 31, 2019

When an error occurs anywhere in a command, Cobra used to print the error itself and command usage help.

We already print error in main(), and we don't want to use command usage string on anything other than flag-parsing errors.

This also fixes the double output of each error.

When an error occurs anywhere in a command, Cobra used to print the
error itself and command usage help.

We already print error in `main()`, and we don't want to use command
usage string on anything other than flag-parsing errors.

This also fixes the double output of each error.
Copy link
Copy Markdown
Contributor

@probablycorey probablycorey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for looking into this. It has been really bugging me! In the situation below, it might be helpful to show the usage instead of just the exit message.

gh not-real-command
[not-real-command] is not a valid PR command
exit status 1

But because your solution is so compact and universal we might want to accept this limitation. In order to still output the usage when the command formatting is wrong I had to add cmd.SilenceUsage = true to the top of each method run. This works, but it is kind of annoying that we need to remember to add it.

So we can either add cmd.SilenceUsage = true to every new command we write or go with your solution and never think about this again. I'm cool with either way!

@vilmibm
Copy link
Copy Markdown
Contributor

vilmibm commented Oct 31, 2019

I think it's best to still show usage when an unknown command is provided; that's pretty standard behavior.

I don't mind the verbosity of adding cmd.SilenceUsage = true to commands as a trade off, personally.

@mislav
Copy link
Copy Markdown
Contributor Author

mislav commented Oct 31, 2019

@probablycorey Nice catch! Now fixed:

$ make && gh not-real-command
go build -o "bin/gh"
unknown command "not-real-command" for "gh"
Usage:
  gh [command]

Available Commands:
  help        Help about any command
  pr          Work with pull requests

What's not fixed is showing the usage help on gh pr not-real-command, but solving that is a tad more complex and I would rather explore that in a separate step.

@mislav mislav merged commit d84a8f2 into master Oct 31, 2019
@mislav mislav deleted the silence-usage branch October 31, 2019 21:56
vilmibm pushed a commit that referenced this pull request Jun 29, 2021
Made test for parse files args, fixed parse file args, fixed repo flag
mislav added a commit that referenced this pull request Sep 28, 2021
Provide version number at build time
Stonre pushed a commit to Stonre/strands-fork that referenced this pull request Jul 21, 2025
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

Successfully merging this pull request may close these issues.

3 participants