alphazero / Go-Redis

Google Go Client and Connectors for Redis
Apache License 2.0
440 stars 123 forks source link

Use of flags in init() short circuits argument parsing #26

Open sigmonsays opened 11 years ago

sigmonsays commented 11 years ago

calling flag.Parse() in init() breaks flags module

$ cat test/test_redis.go package main import ( "fmt" "flag" redis "Go-Redis" ) func main() { var configfile string flag.StringVar(&configfile, "config", "/etc/config.ini", "configuration file") flag.Parse() fmt.Println("Config file", configfile)

spec := redis.DefaultSpec()
_ = spec

--- output ---

go run test/test_redis.go -config /etc/config.ini flag provided but not defined: -config Usage of /tmp/go-build929316002/command-line-arguments/_obj/a.out: -redis:d=false: debug flag for go-redis exit status 2

sigmonsays commented 11 years ago

simple patch to move the invocation of flag.Parse() into a seperate function which must be called manually.

func init() { runtime.GOMAXPROCS(2); +} + +func ParseFlags() { flag.Parse(); }