-
Notifications
You must be signed in to change notification settings - Fork 72
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
Conversation
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) |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
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