go-flags doesn't work with go's flag package
subtleseeker opened this issue · 2 comments
package main
import (
"flag"
"log"
"github.com/jessevdk/go-flags"
)
var f = flag.Bool("a", false, "yeyey")
type A struct {
X string `long:"tf" description:"testflag" default:"def_val" env:"TF"`
}
func main() {
flag.Parse()
a := &A{}
parser := flags.NewParser(a, flags.IgnoreUnknown|flags.HelpFlag|flags.PrintErrors)
_, err := parser.Parse()
if err != nil {
log.Fatal(err)
}
println(*f)
println((*a).X)
}
$ go run flagtest.go --tf bc
flag provided but not defined: -tf
Usage of /tmp/go-build245317497/b001/exe/flagtest:
-a yeyey
exit status 2
After commenting the line, flag.Parse()
:
$ go run flagtest.go --tf bc
false
bc
The use-case occurs when you import a package which is using go's flag package and my package uses go-flags. In that case, go-flags are shadowed by the go's flag library.
Was this a thought out decision while designing? Are there any ways to use both of them together?
I have run into this as well just now. As much as I enjoy the go-flags
package, it's interfering with my use of glog
, which is also very convenient.
This is not a use case that go-flags was designed for. The error that you're getting is from the flag
package from what I can see. I don't think it make much sense to use multiple flags parsers at the same time. You could try passing the "rest" arguments returned from Parse
from go-flags to the CommandLine.Parse
from go flag and see if that works for you. Otherwise this is simply not a goal for go-flags to support.