jessevdk/go-flags

Example error handling seems broken

gavincarr opened this issue · 0 comments

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.