Skip to content

feat: inherit OnUsageError from the nearest ancestor - #2559

Open
mokevnin wants to merge 1 commit into
urfave:mainfrom
mokevnin:on-usage-error-inherits
Open

mokevnin wants to merge 1 commit into
urfave:mainfrom
mokevnin:on-usage-error-inherits

Conversation

@mokevnin

Copy link
Copy Markdown

What type of PR is this?

  • feature

What this PR does / why we need it:

OnUsageError is read only from the command where the usage error happened, while ArgValidator is
already looked up from the nearest ancestor that sets one. An application that wants one way of
reporting usage errors (a wrong flag value, a missing required flag or argument, mutually exclusive
flags) has to set OnUsageError on every command of its tree today, or a subcommand falls back to
Incorrect Usage plus help while the error is also returned to the caller.

  • command_run.go: findOnUsageError walks from the command to the root, as findArgValidator
    does; the four places that called cmd.OnUsageError use it.
  • command.go, funcs.go: the doc comments say the handler is inherited.
  • command_test.go: a subcommand's flag error and required flag reach the root's handler (with the
    subcommand as cmd and isSubcommand true), and a subcommand's own handler still wins.
  • godoc-current.txt, testdata/godoc-v3.x.txt: regenerated and promoted with make v3approve;
    the diff is the two doc comments only.

Which issue(s) this PR fixes:

None filed; happy to open one if you prefer to discuss it there first.

Special notes for your reviewer:

Behaviour changes only for a subcommand without its own OnUsageError whose ancestor has one: it
used to print Incorrect Usage and help, and now the ancestor's handler runs. That is what the
handler on the root was most likely meant for, and it matches ArgValidator, but it is a change in
the v3 series, so please say if you would rather have it opt-in.

Testing

make generate, make vet, make test (99.4% coverage) pass, and make v3diff is clean after
make v3approve. The two inheritance tests fail without the change in command_run.go.

Release Notes

A subcommand without its own OnUsageError now uses the one of its nearest ancestor, like ArgValidator.

🤖 Generated with Claude Code

A subcommand that sets no OnUsageError now uses the one of its nearest ancestor that does, the
way ArgValidator is already found. An application can then handle usage errors (a wrong flag
value, a missing required flag or argument, mutually exclusive flags) once on the root instead
of on every command.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@mokevnin
mokevnin requested a review from a team as a code owner November 28, 2026 20:02
mokevnin added a commit to getsphericon/sphericon that referenced this pull request Nov 28, 2026
…nd a logged fault (#1898)

A wrong flag value, a missing required flag or argument and flags that exclude each other were
printed as urfave's help page, then logged by ExitCode as a fault. refuseUsage words them as the
command line's other refusals: one line and where the help is. urfave reads OnUsageError only from
the command that failed, so newRoot gives it to every command until urfave/cli#2559, which makes
it inherited like ArgValidator, ships.

Co-authored-by: Claude Opus 5.5 <[email protected]>

This branch has not been deployed

No deployments
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.

1 participant