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

Give less confusing error message if a command without arguments received one. #2902

Merged
merged 1 commit into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions internal/captain/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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: "
Expand All @@ -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",
Expand Down
2 changes: 2 additions & 0 deletions internal/locale/locales/en-us.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Loading