Skip to content

fix: return exit code 2 and usage for flag and argument syntax errors - #602

Open
aryarathoree wants to merge 1 commit into
microcks:masterfrom
aryarathoree:fix/cli-syntax-error-exit-code
Open

aryarathoree wants to merge 1 commit into
microcks:masterfrom
aryarathoree:fix/cli-syntax-error-exit-code

Conversation

@aryarathoree

@aryarathoree aryarathoree commented Oct 28, 2026 •

Copy link
Copy Markdown

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 code 2 (KindUsage) defined in documentation/error-handling.md, and omitted the command usage help text.

Root Cause

  1. In cmd/cmd.go, SilenceUsage: true and SilenceErrors: true are configured on the root command so that cmd.Handle serves as the single exit and error printing point.
  2. Cobra and pflag flag parsing errors, positional argument validation (cmd.ValidateArgs), and flag group validation (cmd.ValidateFlagGroups) return standard unwrapped errors.
  3. cmd/exit.go inspects errors.KindOf(err). Unwrapped errors default to KindGeneric (exit code 20), causing Cobra syntax and argument errors to bypass KindUsage (exit code 2).
  4. printError only prints usage text when err wraps a *usageError, so usage instructions were omitted when syntax errors occurred.

Changes

  1. cmd/usage.go:
    • Added wrapUsageError(cmd, err) to wrap standard errors in *usageError carrying errors.KindUsage and cmd.UsageString().
    • Added setupUsageErrors(cmd) to:
      • Register SetFlagErrorFunc across commands to wrap flag parsing failures (unknown flags, missing flag arguments, invalid syntax).
      • Wrap cmd.Args to catch positional argument validation errors and unknown commands, converting them to usageError.
  2. cmd/cmd.go:
    • Replaced PersistentPreRun with PersistentPreRunE to validate required flags and flag groups (e.g. required-together Keycloak flags), returning wrapUsageError upon validation failure.
    • Invoked setupUsageErrors(command) on the root command tree.
  3. cmd/exit_test.go:
    • Added tests in TestCobraSyntaxAndArgumentErrorsReturnKindUsage and TestWrapUsageError covering unknown flags, missing flag arguments, missing positional arguments, required-together flags, and unknown commands.
    • Verified that all scenarios produce exit code 2, carry KindUsage, 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

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>
@Caesarsage

Copy link
Copy Markdown
Collaborator

Thank you @aryarathoree

Could you link the issue this PR is addressing ?

We typical triage the issue first before proposing a fix

@aryarathoree

aryarathoree commented Oct 29, 2026 •

Copy link
Copy Markdown
Author

Hi @Caesarsage, sorry I missed the issue link in my initial PR description.
This PR addresses Issue#600, and I’ve updated the description to include Fixes #600.
I understand the triage first process and will make sure to follow it for future contributions. Thanks for your understanding!

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.

bug: flag and argument syntax errors exit with code 20 (generic) instead of code 2 (usage)

2 participants