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

Print usage for CLI parsing errors #165

Merged
merged 1 commit into from
Oct 1, 2020
Merged

Conversation

bfirsh
Copy link
Member

@bfirsh bfirsh commented Sep 30, 2020

Without SilenceUsage, errors in RunE also produce usage, so we have
to make our own wrapper that prints errors without usage.

See also:
spf13/cobra#340
spf13/cobra#914

Closes #159

Without SilenceUsage, errors in RunE also produce usage, so we have
to make our own wrapper that prints errors without usage.

See also:
spf13/cobra#340
spf13/cobra#914

Closes #159
// handlErrors wraps a cobra function, and will print and exit on error
//
// We don't use RunE because if that returns an error, Cobra will print usage.
// That behavior can be disabled with SilenceUsage option, but then Cobra arg/flag errors don't display usage. (sigh)
Copy link
Member

Choose a reason for hiding this comment

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

Ah yes I remember this, it's why I turned off usage errors in the first place!

// We don't use RunE because if that returns an error, Cobra will print usage.
// That behavior can be disabled with SilenceUsage option, but then Cobra arg/flag errors don't display usage. (sigh)
func handleErrors(f func(cmd *cobra.Command, args []string) error) func(cmd *cobra.Command, args []string) {
return func(cmd *cobra.Command, args []string) {
Copy link
Member

Choose a reason for hiding this comment

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

Does this mean that we print usage info on every error? As a future improvement it'd be nice to only print usage info if the user is doing something stupid (not on random internal errors, etc.). Not trivial to implement, but nice to have.

Copy link
Member Author

Choose a reason for hiding this comment

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

This is the whole point of this function - it catches errors so cobra doesn’t display them.

Copy link
Member Author

Choose a reason for hiding this comment

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

Maybe not obvious that fatal quits, so I’ll clarify comment

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh, the comment does say that. I guess you misunderstood this, or maybe I'm misunderstanding what you're saying? Do you mean random internal errors in Cobra?

@bfirsh bfirsh merged commit 24f6021 into master Oct 1, 2020
@bfirsh bfirsh deleted the bfirsh/print-cli-usage branch October 1, 2020 15:06
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.

Show usage instructions when you give bad arguments to replicate run
2 participants