-
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -80,3 +80,15 @@ func getStorage(storageURL, projectDir string) (storage.Storage, error) { | |
} | ||
return store, nil | ||
} | ||
|
||
// 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) | ||
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 commentThe 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 commentThe 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 commentThe 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 commentThe 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? |
||
if err := f(cmd, args); err != nil { | ||
console.Fatal(err.Error()) | ||
} | ||
} | ||
} |
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!