Coder Social home page Coder Social logo

raintears2 / conflag Goto Github PK

View Code? Open in Web Editor NEW

This project forked from nadoo/conflag

0.0 0.0 0.0 30 KB

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

License: GNU General Public License v3.0

Go 100.00%

conflag's Introduction

conflag

Go Report Card GitHub tag Go Document

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

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 key name:

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

# format:
#KEY=VALUE, 
# just use the command line flag name as 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

See simple.conf

conflag's People

Contributors

felixonmars avatar nadoo avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.