https://github.com/nadoo/conflag
conflag is a drop-in replacement for Go's standard flag package with config file support.
https://github.com/nadoo/conflag
config flag go
Last synced: about 1 year ago
JSON representation
conflag is a drop-in replacement for Go's standard flag package with config file support.
- Host: GitHub
- URL: https://github.com/nadoo/conflag
- Owner: nadoo
- License: gpl-3.0
- Created: 2017-07-18T09:25:07.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2022-02-09T06:01:10.000Z (over 4 years ago)
- Last Synced: 2025-04-04T03:48:22.033Z (about 1 year ago)
- Topics: config, flag, go
- Language: Go
- Homepage:
- Size: 30.3 KB
- Stars: 5
- Watchers: 1
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# conflag
[](https://goreportcard.com/report/github.com/nadoo/conflag)
[](https://github.com/nadoo/conflag/releases)
[](https://pkg.go.dev/github.com/nadoo/conflag )
conflag is a drop-in replacement for Go's standard flag package with config file support.
## Usage
### Your code:
```Go
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:
```bash
example -name Jay -age 30
```
output:
```bash
Name: Jay
Age: 30
Male: false
```
### Run with config file and environment variable(-config):
example.conf:
```bash
name={$NAME}
age=20
male
```
command: **use "-config" flag to specify the config file path.**
```bash
NAME=Jason example -config example.conf
```
output:
```bash
Name: Jason
Age: 20
Male: true
```
### Run with config file and OVERRIDE a flag value using commandline:
example.conf:
```bash
name=Jason
age=20
male
```
command:
```bash
example -config example.conf -name Michael
```
output:
```bash
Name: Michael
Age: 20
Male: true
```
## Config File
- format: KEY=VALUE
**just use the command line flag name as key name**:
```bash
## 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](example/example.conf)