Skip to content

Adds some convenience wrapping around go-flags, mostly for error handling

License

Notifications You must be signed in to change notification settings

geomyidia/flagswrap

Repository files navigation

flagswrap

Build Status

Some convenience wrapping around go-flags, mostly for error handling

The intent of this project is to provide a band-aide until the go-flags project has a chance to address the following issues:

One possible use for this library is to change the (currently broken) example error handling here from this:

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)
        }
    }
}

to this:

func main() {
    if _, err := parser.Parse(); err != nil {
        wrappedErr := flagswrap.WrapError(err)
        switch {
        case wrappedErr.IsHelp():
            os.Exit(0)
        // Self-documenting go-flags errors:
        case wrappedErr.IsVerbose():
            os.Exit(1)
        // go-flags errors that need more context:
        case wrappedErr.IsSilent():
            // TODO: if you see duplicate error messages here, then
            // you just need to move the error in question from the
            // goFlagsSilentErrors to the goFlagsVerboseErrors map
            // in ./errors.go -- and then submit a PR!
            fmt.Printf("Error: %v\n", wrappedErr)
            os.Exit(1)
        default:
            // TODO: anything here might justify a PR ...
            fmt.Printf("ERROR (unexpected): %+v\n", wrappedErr)
            os.Exit(1)
        }
    }
}

You can give this a shot with:

make
./bin/simple -h
./bin/simple
./bin/simple --bloop
./bin/version --version
./bin/dupe

Btw, this doesn't feel super Go-idiomatic -- any PRs making such improvements would be gladly reviewed and (ideally) accepted :-)

About

Adds some convenience wrapping around go-flags, mostly for error handling

Resources

License

Stars

Watchers

Forks

Packages

No packages published