I've been using the "flag" package to parse a command line argument, "-cfg file.conf" to get the name of a config file.
I used:
flag.StringVar(&cfgFile, "cfg", "file.conf", "-cfg file.conf")
followed by:
gcfg.ReadFileInto(&Cfg, cfgFile) // Cfg is a struct...
which reads the file named in the variable cfgFile just peachy. As long as that file name is "file.conf".
Note that I had forgotten to call flag.Parse().... but the value of the variable "cfgFile" is set to my default value anyway. The doc for the flag package says that
the flag variables are not usable until after flag.Parse() is called, which - to me - indicates that "cfgFile" should have been empty. This seems like a bug in the flag package. The flag variables defined should really not have values until flag.Parse() runs - assigning the defaults at the time the flags are first defined made it difficult to notice that I had an error in my logic until the program moved to another platform and we tried to run:
./myprogram -cfg someotherfile.conf
which immediately croaked with "can't open file.conf".
Since the doc says that flag.Parse() is needed to set the flag values, it would be great if it actually worked that way.
Ken