Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

conflag is a drop-in replacement for Go's standard flag package with config file support.

License

NotificationsYou must be signed in to change notification settings

nadoo/conflag

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Go Report CardGitHub tagGo Document

conflag is a drop-in replacement for Go's standard flag package with config file support.

Usage

Your code:

package mainimport ("fmt""github.com/nadoo/conflag")varconfstruct {NamestringAgeintMalebool}funcmain() {// get a new conflag instanceflag:=conflag.New()// setup flags as the standard flag packageflag.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 flagsflag.Parse()// now you're able to get the parsed flag valuesfmt.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:

example -name Jay -age 30

output:

  Name: Jay  Age: 30  Male:false

Run with config file and environment variable(-config):

example.conf:

name={$NAME}age=20male

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

NAME=Jason example -config example.conf

output:

  Name: Jason  Age: 20  Male:true

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

example.conf:

name=Jasonage=20male

command:

example -config example.conf -name Michael

output:

  Name: Michael  Age: 20  Male:true

Config File

  • 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 namename={$NAME}# your ageage=20# are you male?male=true# use include to include more config filesinclude=part1.inc.confinclude=part2.inc.conf

Seeexample.conf

About

conflag is a drop-in replacement for Go's standard flag package with config file support.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp