Skip to content

Improve the output when non-existent command used#662

Merged
mcncl merged 2 commits intomainfrom
better_error_output
Feb 24, 2026
Merged

Improve the output when non-existent command used#662
mcncl merged 2 commits intomainfrom
better_error_output

Conversation

@mcncl
Copy link
Contributor

@mcncl mcncl commented Feb 24, 2026

Description

Currently we just output something like unexpected argument "foo" then do nothing, but we should;

  • suggest "did you mean..." when a clear typo is the issue
  • output the command help when an obscure argument/subcommand is used

Changes

  • adds some logic to check the output string contains did you mean
    • if so continues, else outputs parent command help
  • removes UsageOnError as we never fell through to it anyway due to lack of Fatal

Testing

  • Code is formatted (with go fmt ./...)

Currently we just output something like `unexpected argument "foo"` then do nothing, but we should;

- suggest "did you mean..." when a clear typo is the issue
- output the command help when an obscure argument/subcommand is used
@mcncl mcncl requested a review from a team as a code owner February 24, 2026 00:52

var parseErr *kong.ParseError
if errors.As(err, &parseErr) && !strings.Contains(err.Error(), "did you mean") {
_ = parseErr.Context.PrintUsage(false)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we check if PrintUsage throws an error?
Also, going through the doc, if the flag is true, it only gives a summary of the error https://pkg.go.dev/github.com/alecthomas/kong#Context.PrintUsage, Will it be helpful to locate the issue faster than having a full stack trace?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PrintUsage(true) outputs the short version of the help menu, rather than the full available output, eg:

go run . pipeline loooooo
Usage: bk pipeline <command> [flags]

Manage pipelines

Run "bk pipeline --help" for more information.

Error: unexpected argument loooooo
exit status 1

Re: error return on PrintUsage(), it only throws an error when io.Writer fails, which means the terminal itself has disappeared/crashed.

@mcncl mcncl merged commit b6131df into main Feb 24, 2026
1 check passed
@mcncl mcncl deleted the better_error_output branch February 24, 2026 04:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants