Skip to content

fix: correct error type assertion in example for ErrHelp check#429

Open
Yanhu007 wants to merge 1 commit into
jessevdk:mainfrom
Yanhu007:fix/example-help-error-check
Open

fix: correct error type assertion in example for ErrHelp check#429
Yanhu007 wants to merge 1 commit into
jessevdk:mainfrom
Yanhu007:fix/example-help-error-check

Conversation

@Yanhu007
Copy link
Copy Markdown

Fixes #425

Problem

The example main.go uses an incorrect type assertion:

case flags.ErrorType:    // ❌ ErrorType is an int alias
    if flagsErr == flags.ErrHelp {

But parser.Parse() returns *flags.Error (a struct pointer), not flags.ErrorType. The type switch never matches, so -h/--help always falls through to os.Exit(1).

Fix

case *flags.Error:       // ✅ correct type
    if flagsErr.Type == flags.ErrHelp {

This matches the actual error type and correctly checks the .Type field.

The example uses a type switch on flags.ErrorType (an int type alias),
but parser.Parse() returns *flags.Error (a struct pointer). The type
assertion never matches, so -h/--help always exits with status 1
instead of 0.

Fix by switching on *flags.Error and checking flagsErr.Type.

Fixes jessevdk#425
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.

-h-/--help flag is broken: ErrHelp check fails

1 participant