flagswrap

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 2, 2022 License: BSD-2-Clause Imports: 3 Imported by: 1

README

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

Documentation

Overview

Note: the documentation / examples for error handling in the go-flags library is currently a combination of lacking and wrong: * https://github.com/jessevdk/go-flags/issues/306 * https://github.com/jessevdk/go-flags/issues/361 * https://github.com/jessevdk/go-flags/issues/377

This package attempts to make working with CLI errors easier (and easier to maintain).

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrGoFlagsErrorWrapper = errors.New("couldn't wrap go-flags error")
)

Functions

func Version

func Version() string

Types

type Error

type Error struct {
	// contains filtered or unexported fields
}

func WrapError

func WrapError(err error) *Error

func (*Error) Error

func (e *Error) Error() string

func (*Error) IsHelp

func (e *Error) IsHelp() bool

func (*Error) IsSilent

func (e *Error) IsSilent() bool

func (*Error) IsVerbose

func (e *Error) IsVerbose() bool

func (*Error) Unwrap

func (e *Error) Unwrap() error

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL