conflag is a drop-in replacement for Go's standard flag package with config file support.
package main
import (
"fmt"
"github.com/nadoo/conflag"
)
var conf struct {
Name string
Age int
Male bool
}
func main() {
// get a new conflag instance
flag := conflag.New()
// setup flags as the standard flag package
flag.StringVar(&conf.Name, "name", "", "your name")
flag.IntVar(&conf.Age, "age", 0, "your age")
flag.BoolVar(&conf.Male, "male", false, "your sex")
// parse before access flags
flag.Parse()
// now you're able to get the parsed flag values
fmt.Printf(" Name: %s\n", conf.Name)
fmt.Printf(" Age: %d\n", conf.Age)
fmt.Printf(" Male: %v\n", conf.Male)
}
command:
example -name Jay -age 30
output:
Name: Jay
Age: 30
Male: false
example.conf:
name={$NAME}
age=20
male
command: use "-config" flag to specify the config file path.
NAME=Jason example -config example.conf
output:
Name: Jason
Age: 20
Male: true
example.conf:
name=Jason
age=20
male
command:
example -config example.conf -name Michael
output:
Name: Michael
Age: 20
Male: true
- format: KEY=VALUE
just use the command line flag name as key name:
## config file
# comment line starts with "#"
# format:
#KEY=VALUE,
# just use the command line flag name as key name
# use {$ENV_VAR_NAME} in VALUE to get the Environment Variable value
# your name
name={$NAME}
# your age
age=20
# are you male?
male=true
# use include to include more config files
include=part1.inc.conf
include=part2.inc.conf
See example.conf