conflag

package module
v0.3.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 9, 2022 License: GPL-3.0 Imports: 6 Imported by: 13

README

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:

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=20
male

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=Jason
age=20
male

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 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

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

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 New

func New(args ...string) *Conflag

New parses os args and returns a new Conflag instance.

func NewFromFile

func NewFromFile(app, cfgFile string) *Conflag

NewFromFile parses cfgFile and returns a new Conflag instance.

func (*Conflag) AppDir

func (c *Conflag) AppDir() string

AppDir returns the app dir.

func (*Conflag) ConfDir

func (c *Conflag) ConfDir() string

ConfDir returns the config file dir.

func (*Conflag) Parse

func (c *Conflag) Parse() (err error)

Parse parses config file and flags.

func (*Conflag) StringSlice

func (c *Conflag) StringSlice(name string, value []string, usage string) *[]string

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

func (c *Conflag) StringSliceUniq(name string, value []string, usage string) *[]string

StringSliceUniq works like StringSlice but the items in slice are unique.

func (*Conflag) StringSliceUniqVar

func (c *Conflag) StringSliceUniqVar(p *[]string, name string, value []string, usage string)

StringSliceUniqVar works like StringSliceVar but the items in slice are unique.

func (*Conflag) StringSliceVar

func (c *Conflag) StringSliceVar(p *[]string, name string, value []string, usage string)

StringSliceVar defines a string flag with specified name, default value, and usage string. The argument p points to a []string variable in which to store the value of the flag.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL