Skip to content
JSON configs and environmental variables in addition to Go's flag package.
Go
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.

Files

Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
README.md
config.json
example_jsonflag_test.go
jsonflag.go
jsonflag_test.go
test_config.json

README.md

jsonflag

Go Report Card GoDoc

Use JSON configs and environmental variables in conjunction with Go's flag package.

See the godocs for documentation and a working example.

Example

Example config.json file:

{
"flag1": "jsonFlag1",
"flag2": "jsonFlag2",
"flag3": 3,  // Trailing commas and comments 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`
You can’t perform that action at this time.