Skip to content

fix: handle OnUsageError's exit code for required and exclusive flags - #2558

Open
cansentay wants to merge 1 commit into
urfave:mainfrom
cansentay:fix/on-usage-error-exit-code
Open

cansentay wants to merge 1 commit into
urfave:mainfrom
cansentay:fix/on-usage-error-exit-code

Conversation

@cansentay

Copy link
Copy Markdown

What type of PR is this?

  • bug

What this PR does / why we need it:

For an unknown flag or an invalid argument, the error returned by OnUsageError goes through ExitErrHandler (HandleExitCoder by default), so returning cli.Exit from it sets the exit code. For a missing required flag or argument, and for mutually exclusive flags given together, Run returns that error without handling it:

cmd := &cli.Command{
	Name:  "app",
	Flags: []cli.Flag{&cli.StringFlag{Name: "name", Required: true}},
	MutuallyExclusiveFlags: []cli.MutuallyExclusiveFlags{{
		Flags: [][]cli.Flag{{&cli.BoolFlag{Name: "json"}}, {&cli.BoolFlag{Name: "yaml"}}},
	}},
	Arguments: []cli.Argument{&cli.IntArg{Name: "count", Required: true}},
	OnUsageError: func(_ context.Context, _ *cli.Command, err error, _ bool) error {
		return cli.Exit("usage error: "+err.Error(), 2)
	},
	Action: func(context.Context, *cli.Command) error { return nil },
}
if err := cmd.Run(context.Background(), os.Args); err != nil {
	fmt.Fprintln(os.Stderr, "Run returned:", err)
	os.Exit(1)
}

On main:

$ app --nmae x 1; echo "exit=$?"
usage error: flag provided but not defined: -nmae
exit=2
$ app --name x nan; echo "exit=$?"
usage error: invalid value "nan" for argument count: strconv.ParseInt: parsing "nan": invalid syntax
exit=2
$ app 1; echo "exit=$?"
Run returned: usage error: Required flag "name" not set
exit=1
$ app --name x; echo "exit=$?"
Run returned: usage error: Required argument "count" not set
exit=1
$ app --name x --json --yaml 1; echo "exit=$?"
Run returned: usage error: option json cannot be set along with option yaml
exit=1

With this PR the last three print usage error: ... and exit 2, like the first two.

#2161 (for #2145) and #2152 made these errors call OnUsageError, but didn't pass its result on to the exit handler as the flag parse error path does.

  • command_run.go: call handleExitCoder on the error from OnUsageError in handleRequiredError (required flags and arguments) and in the mutually exclusive group check, the same two lines as on the flag parse error path. Without OnUsageError nothing changes.
  • command_test.go: TestCommand_OnUsageError_ExitCoder checks that ExitErrHandler gets the error OnUsageError returns, for each kind of usage error.

Which issue(s) this PR fixes:

None open. This is the ExitErrHandler half of #2145 ("Missing required flags does not trigger ExitErrHandler"), which #2161 closed by calling OnUsageError.

Special notes for your reviewer:

As on the flag parse error path, ExitErrHandler is also called when OnUsageError returns nil. Usage errors without an OnUsageError still don't reach ExitErrHandler, the same as an unknown flag; I left that alone.

Testing

  • TestCommand_OnUsageError_ExitCoder: the required flag, required argument and mutually exclusive cases fail on main (d5c06cb); the unknown flag and invalid argument cases pass before and after.
  • make vet, make test, make test GFLAGS='--tags urfave_cli_no_template', make check-binary-size, make generate, make diffcheck and make v3diff pass, as does go test ./... with urfave_cli_no_docs and urfave_cli_no_suggest. goimports -l . is clean. No API change.

Release Notes

An error returned by `OnUsageError` for a missing required flag or argument, or for mutually exclusive flags, now goes through `ExitErrHandler` (`HandleExitCoder` by default), so its exit code is used, as it already was for unknown flags and invalid arguments.

For an unknown flag or an invalid argument, the error returned by
OnUsageError goes through ExitErrHandler, or HandleExitCoder by
default, so returning cli.Exit from it sets the exit code. For a
missing required flag or argument, and for mutually exclusive flags
given together, the error was returned to the caller without being
handled, so the same OnUsageError exited with its code for some usage
errors and not for others.

Handle it on those paths too. Without OnUsageError nothing changes, as
for an unknown flag.
@cansentay
cansentay requested a review from a team as a code owner November 28, 2026 18:22

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