glider/vendor/github.com/nadoo/conflag/README.md
2018-07-07 11:07:38 +08:00

1.6 KiB

conflag

conflag is a config file and command line parser based on Go's standard flag package.

Usage

Your code:

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)
}

Run without config file:

command:

sample -name Jay -age 30

output:

  Name: Jay
  Age: 30
  Male: false

Run with config file(-config):

sample.conf:

name=Jason
age=20
male

command: use "-config" flag to specify the config file path.

sample -config sample.conf

output:

  Name: Jason
  Age: 20
  Male: true

Run with config file and OVERRIDE a flag value using commandline:

sample.conf:

name=Jason
age=20
male

command:

sample -config sample.conf -name Michael

output:

  Name: Michael
  Age: 20
  Male: true

Config File

  • format: KEY=VALUE

just use the command line flag name as the key name:

## config file
# comment line starts with "#"

# format:
#KEY=VALUE, 
# just use the command line flag name as the key name

# your name
name=Jason

# your age
age=20

# are you male?
male

See simple.conf