From 7668ce5fdc646b98c364640949f09d9ab76b24e3 Mon Sep 17 00:00:00 2001 From: mitchell Date: Tue, 28 Nov 2023 10:55:26 -0500 Subject: [PATCH] Give less confusing error message if a command without arguments received one. --- internal/captain/command.go | 12 ++++++------ internal/locale/locales/en-us.yaml | 2 ++ 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/internal/captain/command.go b/internal/captain/command.go index ecfd19e776..e18d5cff5a 100644 --- a/internal/captain/command.go +++ b/internal/captain/command.go @@ -290,7 +290,7 @@ func (c *Command) Execute(args []string) error { c.cobra.SetArgs(args) err := c.cobra.Execute() c.cobra.SetArgs(nil) - return setupSensibleErrors(err) + return setupSensibleErrors(err, args) } func (c *Command) SetExamples(examples ...string) *Command { @@ -768,7 +768,7 @@ func (c *Command) argValidator(cobraCmd *cobra.Command, args []string) error { // setupSensibleErrors inspects an error value for certain errors and returns a // wrapped error that can be checked and that is localized. -func setupSensibleErrors(err error) error { +func setupSensibleErrors(err error, args []string) error { if err, ok := err.(error); ok && err == nil { return nil } @@ -816,10 +816,7 @@ func setupSensibleErrors(err error) error { } if pflagErrCmd := pflagCmdErrMsgCmd(errMsg); pflagErrCmd != "" { - return locale.NewInputError( - "command_cmd_no_such_cmd", - "No such command: [NOTICE]{{.V0}}[/RESET]", pflagErrCmd, - ) + return locale.NewInputError("command_cmd_no_such_cmd", "", pflagErrCmd) } // Cobra error message of the form "accepts at most 0 arg(s), received 1, called at: " @@ -830,6 +827,9 @@ func setupSensibleErrors(err error) error { multilog.Error("Unable to parse cobra error message: %v", err) return locale.NewInputError("err_cmd_unexpected_arguments", "Unexpected argument(s) given") } + if max == 0 && received > 0 { + return locale.NewInputError("command_cmd_no_such_cmd", "", args[len(args)-received]) + } return locale.NewInputError( "err_cmd_too_many_arguments", "Too many arguments given: {{.V0}} expected, {{.V1}} received", diff --git a/internal/locale/locales/en-us.yaml b/internal/locale/locales/en-us.yaml index 02ce4f8ec6..22088dd2b3 100644 --- a/internal/locale/locales/en-us.yaml +++ b/internal/locale/locales/en-us.yaml @@ -1177,6 +1177,8 @@ notice_commit_build_script: command_flag_invalid_value: other: "Invalid value for {{.V0}} flag: [NOTICE]{{.V1}}[/RESET]" +command_cmd_no_such_cmd: + other: "No such command: [NOTICE]{{.V0}}[/RESET]" err_cmdtree: other: Could not run the requested command err_fetch_languages: