Closed nyonson closed 3 years ago
WithConfigFile
is provided only so there is a way to set a config file without necessarily exposing the parameter as a flag, but that is a very rare circumstance. If you want to provide an e.g. -config-file
flag with some default, just define it as a flag with a default value.
var defaultConfigFile string
if d, err := os.UserConfigDir(); err == nil {
defaultConfigFile = filepath.Join(d, "myctl", "myctl.conf")
}
fs := flag.NewFlagSet("myctl", flag.ExitOnError)
var (
foo = fs.String("foo", "hello world", "foo flag")
_ = fs.String("config", defaultConfigFile, "config file")
)
ff.Parse(fs, os.Args[1:], ff.WithConfigFileFlag("config"))
Ah, I totally misinterpreted how WithConfigFileFlag
worked, thanks for the example. I went with your solution.
I would like to provided a default configuration file based on XDG (e.g.
~/.config/APP.config
) which could then be overwritten byWithConfigFileFlag
if a user prefers a different location. I was surprised to find that with the existing settingsWithConfigFile
overwritesWithConfigFileFlag
, is there a use case for this that I am missing?