Documentation ¶
Overview ¶
Package conflag is a drop-in replacement for Go's standard flag package with config file support.
Usage ¶
Create a conflag instance and then you can use it like the standard flag package.
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) }
Config File ¶
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 the key name # your name name=Jason # your age age=20 # are you male? male=true # use include to include more config files include=part1.inc.conf include=part2.inc.conf
Index ¶
- type Conflag
- func (c *Conflag) AppDir() string
- func (c *Conflag) ConfDir() string
- func (c *Conflag) Parse() (err error)
- func (c *Conflag) StringSlice(name string, value []string, usage string) *[]string
- func (c *Conflag) StringSliceUniq(name string, value []string, usage string) *[]string
- func (c *Conflag) StringSliceUniqVar(p *[]string, name string, value []string, usage string)
- func (c *Conflag) StringSliceVar(p *[]string, name string, value []string, usage string)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Conflag ¶
type Conflag struct { // embedded the standard FlagSet so we can use all its methods. *flag.FlagSet // contains filtered or unexported fields }
A Conflag represents the state of a conflag.
func NewFromFile ¶
NewFromFile parses cfgFile and returns a new Conflag instance.
func (*Conflag) StringSlice ¶
StringSlice defines a string flag with specified name, default value, and usage string. The return value is the address of a []string variable that stores the value of the flag.
func (*Conflag) StringSliceUniq ¶ added in v0.2.0
StringSliceUniq works like StringSlice but the items in slice are unique.
func (*Conflag) StringSliceUniqVar ¶
StringSliceUniqVar works like StringSliceVar but the items in slice are unique.