Repository navigation
Conversation
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.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What type of PR is this?
What this PR does / why we need it:
For an unknown flag or an invalid argument, the error returned by
OnUsageErrorgoes throughExitErrHandler(HandleExitCoderby default), so returningcli.Exitfrom it sets the exit code. For a missing required flag or argument, and for mutually exclusive flags given together,Runreturns that error without handling it:On main:
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: callhandleExitCoderon the error fromOnUsageErrorinhandleRequiredError(required flags and arguments) and in the mutually exclusive group check, the same two lines as on the flag parse error path. WithoutOnUsageErrornothing changes.command_test.go:TestCommand_OnUsageError_ExitCoderchecks thatExitErrHandlergets the errorOnUsageErrorreturns, for each kind of usage error.Which issue(s) this PR fixes:
None open. This is the
ExitErrHandlerhalf of #2145 ("Missing required flags does not triggerExitErrHandler"), which #2161 closed by callingOnUsageError.Special notes for your reviewer:
As on the flag parse error path,
ExitErrHandleris also called whenOnUsageErrorreturns nil. Usage errors without anOnUsageErrorstill don't reachExitErrHandler, 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 diffcheckandmake v3diffpass, as doesgo test ./...withurfave_cli_no_docsandurfave_cli_no_suggest.goimports -l .is clean. No API change.Release Notes