7

I'm using go-flags to parse command line options.

Per the go-flags docs:

... [if] either -h or --help was specified in the command line arguments, a help message will be automatically printed. Furthermore, the special error type ErrHelp is returned.

The method I'm calling is:

func (p *Parser) Parse() ([]string, error) {

I'm calling it with:

var opts struct {
    // ...
}

func main() {

    parser := flags.NewParser(&opts, flags.Default)

    args, err := parser.Parse()

A snippet from the file that defines ErrHelp looks like this:

type ErrorType uint

const (
    // Unknown or generic error
    ErrUnknown ErrorType = iota

    // Expected an argument but got none
    ErrExpectedArgument

    // ...

    // The error contains the builtin help message
    ErrHelp

    // ...
)

// Error represents a parser error. The error returned from Parse is of this
// type. The error contains both a Type and Message.
type Error struct {
    // The type of error
    Type ErrorType

    // The error message
    Message string
}

// Get the errors error message.
func (e *Error) Error() string {
    return e.Message
}

func newError(tp ErrorType, message string) *Error {
    return &Error{
        Type:    tp,
        Message: message,
    }
}

So they have this custom "Error" type. And in the Parse() method above, internally, the error is getting created with a block of code like this:

    help.ShowHelp = func() error {
        var b bytes.Buffer
        p.WriteHelp(&b)
        return newError(ErrHelp, b.String())
    }

As you can see newError() returns "*Error" as it's type. But the anonymous function just above returns type "error" - so those types must be compatible.(?)

But now back to the original problem - I'm just trying to see if my "err" is an "Error" and has member "Type" equal to ErrHelp. So I try this:

if err != nil && flags.Error(err).Type == flags.ErrHelp {

Or even just this:

fmt.Printf("test:", flags.Error(err))

And either way the compiler gives me:

main.go:37: cannot convert err (type error) to type flags.Error

But does not state why that conversion can't be done. Any ideas?

(I don't get how "*Error" successfully converts to "error" in the anonymous function above, and I even more don't get why if that works then I can't convert it back the other way... I must be missing something really dumb here, but I'm not seeing what it is.)

4

1 回答 1

18

Anerror是具有单一方法的接口Error() string。见http://golang.org/pkg/builtin/#error

Aflags.Error有这样的方法,所以它可以作为一个error.

然而,相反,aflags.Error是一个结构,没有办法将任意值转换为结构。

能做的是,我认为这是你问题的答案,如果你有一个flags.Value内部的 an error,那么你可以将errorback 转换为底层类型。其语法是e := err.(*flags.Error). 这会给你一个类型的值*flags.Error(或者恐慌,如果基础类型不是*flags.Error)。在这种情况下,您可以通过使用逗号-ok 形式来避免恐慌,即e, ok := err.(*flags.Error).

具体来说,你会写:

  args, err := flags.Parse()
  if err != nil {
     if ferr, ok := err.(*flags.Error); ok {
       // ... something using ferr
     } else {
       // ... deal with non-flags.Error case, if that's possible.
     }
  }
于 2013-08-24T07:49:34.903 回答