fix(cmd): print only the error on CLI usage errors

cli v3.10 dumps the full command help to stdout on a usage error (unknown
flag, missing required flag, etc.). Take over that reporting via
OnUsageError so usage errors print only "Incorrect Usage: <err>" to
stderr and nothing to stdout, covering the auto-built completion subtree
through ConfigureShellCompletionCommand. Also stop RunMainApp from
printing the error a second time for non-unknown-flag usage errors.

Assisted-by: Claude:Opus-4.8
This commit is contained in:
silverwind
2026-06-29 03:21:00 +02:00
parent 1c4989c193
commit 34a2f682a5
2 changed files with 27 additions and 6 deletions

View File

@@ -202,8 +202,8 @@ func TestCliCmdError(t *testing.T) {
r, err = runTestApp(app, "./gitea", "test-cmd", "--no-such")
assert.Error(t, err)
assert.Equal(t, 1, r.ExitCode)
assert.NotEmpty(t, r.Stdout) // since cli v3.10, the usage help is printed to stdout on a usage error
assert.Equal(t, "Incorrect Usage: flag provided but not defined: -no-such\n\n", r.Stderr)
assert.Empty(t, r.Stdout)
assert.Equal(t, "Incorrect Usage: flag provided but not defined: -no-such\n", r.Stderr)
app = newTestApp(cli.Command{Action: func(ctx context.Context, cmd *cli.Command) error { return nil }})
r, err = runTestApp(app, "./gitea", "test-cmd")