conflag
A combination command-line flag and configuration file library for Go.
Usage
// define your flags.
var procs int
flag.IntVar(&procs, "procs", runtime.NumCPU(), "GOMAXPROCS")
// set flags from configuration before parse command-line flags.
if args, err := conflag.ArgsFrom("/path/to/config.toml"); err == nil {
flag.CommandLine.Parse(args)
}
// parse command-line flags.
flag.Parse()
and you create /path/to/config.toml
procs = 2
and run your app without option, procs
flag will be set in 2
that is defined at configration file.
Priority of flag
A priority of flag is
command-line flag
> configration file
> flag default value
In the above case,
run |
procs |
myapp -procs 3 |
3 |
myapp (with config-file) |
2 |
myapp (without config-file) |
runtime.NumCPU() (default of flag) |
Position
You can specify positions
arguments to ArgsFrom
function.
[options]
flag = "value"
[other settings]
hoge = "fuga"
// parse configration only under the options section.
conflag.ArgsFrom("/path/to/config.toml", "options")
List
You can use list for multiple parameters.
The following toml makes -flag value1 -flag value2
arguments.
flag = [ "value1", "value2" ]
go-flags
If you use go-flags package, you can specify options like the following.
parser := go-flags.NewParser(&opts, go-flags.Default)
conflag.LongHyphen = true
conflag.BoolValue = false
if args, err := conflag.ArgsFrom("/path/to/config.toml"); err == nil {
parser.ParseArgs(args)
}
Features
- Combine command-line flag and configuration file.
- Specify configration section.
- Specify list parameters.
- Support TOML configuration file.
- Support JSON configuration file.
- Support YAML configuration file.
Installation
$ go get github.com/monochromegane/conflag
Contribution
- Fork it
- Create a feature branch
- Commit your changes
- Rebase your local changes against the master branch
- Run test suite with the
go test ./...
command and confirm that it passes
- Run
gofmt -s
- Create new Pull Request
License
MIT
Author
monochromegane