-
Notifications
You must be signed in to change notification settings - Fork 1
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
feat: Add more help in error message #72
Conversation
This pull request has been linked to Shortcut Story #237653: show the right usage help on subcommand error. |
"ldcli/internal/flags" | ||
) | ||
|
||
func NewCreateCmd(client flags.Client) (*cobra.Command, error) { | ||
cmd := &cobra.Command{ | ||
Use: "create", |
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.
Sorted and moved the validate
function into validators.Validate
.
@@ -12,6 +12,7 @@ import ( | |||
) | |||
|
|||
func TestCreate(t *testing.T) { | |||
errorHelp := ". See `ldcli flags create --help` for supported flags and usage." |
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.
We can refactor these tests to DRY up some duplication.
|
||
// Validate is a validator for commands to print an error when the number provided | ||
// is different than the arguments passed in | ||
func Validate() cobra.PositionalArgs { |
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.
Mostly taken from stripe's CLI.
return func(cmd *cobra.Command, args []string) error { | ||
commandPath := getCommandPath(cmd) | ||
|
||
_, err := url.ParseRequestURI(viper.GetString("baseUri")) |
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.
We may want to extract unexported functions if this grows.
cmd/validators/validators.go
Outdated
// Validate is a validator for commands to print an error when the number provided | ||
// is different than the arguments passed in |
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.
can we update this docstring since we're doing more than just validating the # of args (or just remove)
When a command doesn't have the right flags, we show a one line error message that describes the error and says how to see more info with --help instead of showing the results of calling --help. This can cut down on noise while still giving the user enough info for how to fix the error.
If the error comes from the API, just show that error like normal.
The "flag needs an argument" message doesn't show the additional help text because it's more difficult to hook into the
pflags
error where it originates instead of the other errors that come fromcobra
.