Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example error handling seems broken #361

Open
gavincarr opened this issue May 2, 2021 · 0 comments
Open

Example error handling seems broken #361

gavincarr opened this issue May 2, 2021 · 0 comments

Comments

@gavincarr
Copy link

The error handling in examples/main.go, which seems to be the only (non-panic) example I can find, seems broken to me:

func main() {
    if _, err := parser.Parse(); err != nil {
        switch flagsErr := err.(type) {
        case flags.ErrorType:
            if flagsErr == flags.ErrHelp {
                os.Exit(0)
            }
            os.Exit(1)
        default:
            os.Exit(1)
        }
    }
}

err.(type) is returning *flags.Error, I believe, so the case flags.ErrorType is never matched, and we just fall through to the default. Shouldn't it be something like:

func main() {
    if _, err := parser.Parse(); err != nil {
        flagsErr := err.(*flags.Error)
        if flagsErr.Type == flags.ErrHelp {
            os.Exit(0)
        }
        os.Exit(2)
    }
}

(This panics if err isn't *flags.Error, but given that's the documented return type from Parse(), I'm not sure we need to handle other cases?)

Separately, I wonder if it would be worth including the error handling example in the README, rather than just panicking?

P.S. Thanks for a great package - I'm loving it now I've figured out the error handling.

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

No branches or pull requests

1 participant