jsonflag
Use JSON (json5) configs and environmental variables in conjunction with Go's flag package.
See the godocs for documentation and a working example.
Example
Example config.json5
file:
{
"flag1": "jsonFlag1",
"flag2": "jsonFlag2",
"flag3": 3, // Comments and trailing commas in json config are recommended.
}
// Config struct name (tag should) match the json key.
type Config struct {
Flag1 string
Flag2 string
Flag3 int
}
func main(){
var config Config
// `flag` is still from the standard library.
flag.StringVar(&config.Flag1, "flag1name", "defaultFlag1", "flag1Desc")
flag.StringVar(&config.Flag2, "flag2name", "defaultFlag2", "flag2Desc")
flag.IntVar(&config.Flag3, "flag3name", 1, "flag3Desc")
// Instead of `flag.parse`, use `jsonflag.Parse(&config)`
// This is the only line that must be different from using `flag` normally.
jsonflag.Parse(&config)
}
Environmental variables and cli flags give further flexibility. See the documentation for order of precedence.
`FLAG1=flag1EnvValue go run --flag2=flag2CliValue`