Repository navigation
fix: return exit code 2 and usage for flag and argument syntax errors - #602
Open
aryarathoree wants to merge 1 commit into
Open
aryarathoree wants to merge 1 commit into
aryarathoree wants to merge 1 commit into
Conversation
Cobra and pflag parsing, argument validation, and flag group validation previously returned unwrapped errors. Because SilenceUsage and SilenceErrors were configured on the root command, these errors bypassed usage formatting and fell back to KindGeneric (exit code 20) instead of the contracted KindUsage (exit code 2) defined in documentation/error-handling.md. Configure FlagErrorFunc, argument validation wrappers, and PersistentPreRunE flag group validation to wrap Cobra errors in usageError with KindUsage and the command usage string. Signed-off-by: Arya Rathore <aryarathoree@gmail.com>
aryarathoree
requested review from
Caesarsage,
Harsh4902,
lbroudoux and
yada
as code owners
October 28, 2026 17:03
Collaborator
|
Thank you @aryarathoree Could you link the issue this PR is addressing ? We typical triage the issue first before proposing a fix |
Author
|
Hi @Caesarsage, sorry I missed the issue link in my initial PR description. |
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.
Fixes #600
Description
This PR fixes an issue where CLI syntax, flag, and positional argument errors produced exit code
20(KindGeneric) instead of the contracted exit code2(KindUsage) defined indocumentation/error-handling.md, and omitted the command usage help text.Root Cause
cmd/cmd.go,SilenceUsage: trueandSilenceErrors: trueare configured on the root command so thatcmd.Handleserves as the single exit and error printing point.cmd.ValidateArgs), and flag group validation (cmd.ValidateFlagGroups) return standard unwrapped errors.cmd/exit.goinspectserrors.KindOf(err). Unwrapped errors default toKindGeneric(exit code 20), causing Cobra syntax and argument errors to bypassKindUsage(exit code 2).printErroronly prints usage text whenerrwraps a*usageError, so usage instructions were omitted when syntax errors occurred.Changes
cmd/usage.go:wrapUsageError(cmd, err)to wrap standard errors in*usageErrorcarryingerrors.KindUsageandcmd.UsageString().setupUsageErrors(cmd)to:SetFlagErrorFuncacross commands to wrap flag parsing failures (unknown flags, missing flag arguments, invalid syntax).cmd.Argsto catch positional argument validation errors and unknown commands, converting them tousageError.cmd/cmd.go:PersistentPreRunwithPersistentPreRunEto validate required flags and flag groups (e.g. required-together Keycloak flags), returningwrapUsageErrorupon validation failure.setupUsageErrors(command)on the root command tree.cmd/exit_test.go:TestCobraSyntaxAndArgumentErrorsReturnKindUsageandTestWrapUsageErrorcovering unknown flags, missing flag arguments, missing positional arguments, required-together flags, and unknown commands.2, carryKindUsage, and include the command usage text in output.AI Disclosure
In accordance with the repository's AI policy, this PR was authored by Arya Rathore with the assistance of an AI assistant (Google Antigravity) for root cause analysis, code implementation, and test cases.
Signed-off-by: Arya Rathore aryarathoree@gmail.com