Documentation
¶
Overview ¶
Package configue facilitate parsing command-line options from multiple sources (environments variables, flag, INI files, etc).
Usage ¶
Define options using configue.String, Bool, Int, etc.
This declares an integer option, n, stored in the pointer n, with type *int:
import "github.com/negrel/configue"
var n = configue.Int("n", 1234, "help message for option n")
If you like, you can bind the option to a variable using the Var() functions.
var n int
func init() {
configue.IntVar(&n, "n", 1234, "help message for n")
}
Or you can create custom options that satisfy the Value interface (with pointer receivers) and couple them to options parsing by
configue.Var(&n, "n", "help message for n")
For such options, the default value is just the initial value of the variable.
After all options are defined, call
configue.Parse()
to parse the command line into the defined options.
Options may then be used directly. If you're using the options themselves, they are all pointers; if you bind to variables, they're values.
fmt.Println("ip has value ", *ip)
fmt.Println("n has value ", n)
Command line option syntax ¶
Options are loaded/parsed by Backend. Built-in flag, environment variable and INI file based backends are provided by NewFlag, NewEnv, NewINI respectively. They parse options value the same way. See [`option`](./option#pkg-overview) documentation for more information.
Configue is a dependency-free configuration library inspired by Go's flag package from the standard library.
Index ¶
- Constants
- Variables
- func AppDir(defaultUserDir string) string
- func Bool(name string, value bool, usage string) *bool
- func BoolSlice(name string, value []bool, usage string) *[]bool
- func BoolSliceVar(p *[]bool, name string, value []bool, usage string)
- func BoolVar(p *bool, name string, value bool, usage string)
- func Duration(name string, value time.Duration, usage string) *time.Duration
- func DurationSlice(name string, value []time.Duration, usage string) *[]time.Duration
- func DurationSliceVar(p *[]time.Duration, name string, value []time.Duration, usage string)
- func DurationVar(p *time.Duration, name string, value time.Duration, usage string)
- func File(defaultUserDir, fname string) string
- func Float64(name string, value float64, usage string) *float64
- func Float64Slice(name string, value []float64, usage string) *[]float64
- func Float64SliceVar(p *[]float64, name string, value []float64, usage string)
- func Float64Var(p *float64, name string, value float64, usage string)
- func Func(name, usage string, fn func(string) error)
- func Int(name string, value int, usage string) *int
- func Int64(name string, value int64, usage string) *int64
- func Int64Slice(name string, value []int64, usage string) *[]int64
- func Int64SliceVar(p *[]int64, name string, value []int64, usage string)
- func Int64Var(p *int64, name string, value int64, usage string)
- func IntSlice(name string, value []int, usage string) *[]int
- func IntSliceVar(p *[]int, name string, value []int, usage string)
- func IntVar(p *int, name string, value int, usage string)
- func Parse() error
- func Parsed() bool
- func PrintDefaults()
- func Set(name, value string) error
- func String(name string, value string, usage string) *string
- func StringSlice(name string, value []string, usage string) *[]string
- func StringSliceVar(p *[]string, name string, value []string, usage string)
- func StringVar(p *string, name string, value string, usage string)
- func TextVar(p encoding.TextUnmarshaler, name string, value encoding.TextMarshaler, ...)
- func Uint(name string, value uint, usage string) *uint
- func Uint64(name string, value uint64, usage string) *uint64
- func Uint64Slice(name string, value []uint64, usage string) *[]uint64
- func Uint64SliceVar(p *[]uint64, name string, value []uint64, usage string)
- func Uint64Var(p *uint64, name string, value uint64, usage string)
- func UintSlice(name string, value []uint, usage string) *[]uint
- func UintSliceVar(p *[]uint, name string, value []uint, usage string)
- func UintVar(p *uint, name string, value uint, usage string)
- func UnquoteUsage(optValue option.Value, optUsage string) (name string, usage string)
- func UserDir(def string) string
- func Var(value option.Value, name string, usage string)
- type Backend
- type Env
- type ErrorHandling
- type Figue
- func (f *Figue) Bool(name string, value bool, usage string) *bool
- func (f *Figue) BoolSlice(name string, value []bool, usage string) *[]bool
- func (f *Figue) BoolSliceVar(p *[]bool, name string, value []bool, usage string)
- func (f *Figue) BoolVar(p *bool, name string, value bool, usage string)
- func (f *Figue) Duration(name string, value time.Duration, usage string) *time.Duration
- func (f *Figue) DurationSlice(name string, value []time.Duration, usage string) *[]time.Duration
- func (f *Figue) DurationSliceVar(p *[]time.Duration, name string, value []time.Duration, usage string)
- func (f *Figue) DurationVar(p *time.Duration, name string, value time.Duration, usage string)
- func (f *Figue) Float64(name string, value float64, usage string) *float64
- func (f *Figue) Float64Slice(name string, value []float64, usage string) *[]float64
- func (f *Figue) Float64SliceVar(p *[]float64, name string, value []float64, usage string)
- func (f *Figue) Float64Var(p *float64, name string, value float64, usage string)
- func (f *Figue) Func(name, usage string, fn func(string) error)
- func (f *Figue) Int(name string, value int, usage string) *int
- func (f *Figue) Int64(name string, value int64, usage string) *int64
- func (f *Figue) Int64Slice(name string, value []int64, usage string) *[]int64
- func (f *Figue) Int64SliceVar(p *[]int64, name string, value []int64, usage string)
- func (f *Figue) Int64Var(p *int64, name string, value int64, usage string)
- func (f *Figue) IntSlice(name string, value []int, usage string) *[]int
- func (f *Figue) IntSliceVar(p *[]int, name string, value []int, usage string)
- func (f *Figue) IntVar(p *int, name string, value int, usage string)
- func (f *Figue) Output() io.Writer
- func (f *Figue) Parse() error
- func (f *Figue) Parsed() bool
- func (f *Figue) PrintDefaults()
- func (f *Figue) Set(name, value string) error
- func (f *Figue) SetOutput(w io.Writer)
- func (f *Figue) String(name string, value string, usage string) *string
- func (f *Figue) StringSlice(name string, value []string, usage string) *[]string
- func (f *Figue) StringSliceVar(p *[]string, name string, value []string, usage string)
- func (f *Figue) StringVar(p *string, name string, value string, usage string)
- func (f *Figue) TextVar(p encoding.TextUnmarshaler, name string, value encoding.TextMarshaler, ...)
- func (f *Figue) Uint(name string, value uint, usage string) *uint
- func (f *Figue) Uint64(name string, value uint64, usage string) *uint64
- func (f *Figue) Uint64Slice(name string, value []uint64, usage string) *[]uint64
- func (f *Figue) Uint64SliceVar(p *[]uint64, name string, value []uint64, usage string)
- func (f *Figue) Uint64Var(p *uint64, name string, value uint64, usage string)
- func (f *Figue) UintSlice(name string, value []uint, usage string) *[]uint
- func (f *Figue) UintSliceVar(p *[]uint, name string, value []uint, usage string)
- func (f *Figue) UintVar(p *uint, name string, value uint, usage string)
- func (f *Figue) Var(val option.Value, path string, usage string)
- type Flag
- func (flag *Flag) Init(name string)
- func (flag *Flag) Parse() error
- func (flag *Flag) PrintDefaults()
- func (flag *Flag) Set(name, value string) error
- func (flag *Flag) Var(val Value, name, usage string) string
- func (fb *Flag) Visit(fn func(option.Option))
- func (fb *Flag) VisitAll(fn func(option.Option))
- type Ini
- type Value
Constants ¶
const ( ContinueOnError ErrorHandling = flag.ContinueOnError // Return a descriptive error. ExitOnError = flag.ExitOnError // Call os.Exit(2). PanicOnError = flag.PanicOnError // Call panic with a descriptive error. )
These constants cause Figue.Parse to behave as described if the parse fails.
Variables ¶
var ( // CommandLine is the default set of command-line options, parsed from // INI file, environments variable and flags in this specific order. The // top-level functions such as BoolVar, and so on are wrappers for the methods // of CommandLine. CommandLine = New("", ExitOnError, NewINI(File("./", "config.ini")), NewEnv(""), NewFlag()) // Usage prints a usage message documenting all defined command-line options // to CommandLine's output, which by default is os.Stderr. It is called when // an error occurs while parsing env vars. The function is a variable that may // be changed to point to a custom function. By default it prints a simple // header and calls PrintDefaults; for details about the format of the Output // and how to control it, see the documentation for PrintDefaults. Custom // usage functions may choose to exit the program; by default exiting happens // anyway as the command line's error handling strategy is set to ExitOnError. Usage = func() { _, _ = fmt.Fprintf(CommandLine.Output(), "Usage of %s:\n", os.Args[0]) PrintDefaults() } // ErrHelp is the error returned if the -help or -h flag is invoked but no // such flag is defined. ErrHelp = flag.ErrHelp )
Functions ¶
func AppDir ¶ added in v0.5.0
AppDir returns application configuration directory which is UserDir joined with os.Args[0].
func Bool ¶
Bool defines a bool option with specified name, default value, and usage string. The return value is the address of a bool variable that stores the value of the option.
func BoolSlice ¶ added in v0.5.0
BoolSlice defines a slice of bool option with specified name, default value, and usage string. The return value is the address of a slice of bool variable that stores the value of the option.
func BoolSliceVar ¶ added in v0.5.0
BoolSliceVar defines a slice of bool option with specified name, default value, and usage string. The argument p points to a slice of bool variable in which to store the value of the option.
func BoolVar ¶
BoolVar defines a bool option with specified name, default value, and usage string. The argument p points to a bool variable in which to store the value of the option.
func Duration ¶
Duration defines a time.Duration option with specified name, default value, and usage string. The return value is the address of a time.Duration variable that stores the value of the option.
func DurationSlice ¶ added in v0.5.0
DurationSlice defines a slice of time.Duration option with specified name, default value, and usage string. The return value is the address of a slice of time.Duration variable that stores the value of the option.
func DurationSliceVar ¶ added in v0.5.0
DurationSliceVar defines a slice of time.Duration option with specified name, default value, and usage string. The argument p points to a slice of time.Duration variable in which to store the value of the option.
func DurationVar ¶
DurationVar defines a time.Duration option with specified name, default value, and usage string. The argument p points to a time.Duration variable in which to store the value of the option.
func File ¶ added in v0.5.0
File returns path to configuration file by joining AppDir with provided `fname`.
func Float64 ¶
Float64 defines a float64 option with specified name, default value, and usage string. The return value is the address of a float64 variable that stores the value of the option.
func Float64Slice ¶ added in v0.5.0
Float64Slice defines a slice of float64 option with specified name, default value, and usage string. The return value is the address of a slice of float64 variable that stores the value of the option.
func Float64SliceVar ¶ added in v0.5.0
Float64SliceVar defines a slice of float64 option with specified name, default value, and usage string. The argument p points to a slice of float64 variable in which to store the value of the option.
func Float64Var ¶
Float64Var defines a float64 option with specified name, default value, and usage string. The argument p points to a float64 variable in which to store the value of the option.
func Func ¶ added in v0.5.0
Func defines an option with the specified name and usage string. Each time the option is seen, fn is called with the value of the option. If fn returns a non-nil error, it will be treated as a value parsing error.
func Int ¶
Int defines an int option with specified name, default value, and usage string. The return value is the address of an int variable that stores the value of the option.
func Int64 ¶
Int64 defines an int64 option with specified name, default value, and usage string. The return value is the address of an int64 variable that stores the value of the option.
func Int64Slice ¶ added in v0.5.0
Int64Slice defines a slice of int64 option with specified name, default value, and usage string. The return value is the address of a slice of int64 variable that stores the value of the option.
func Int64SliceVar ¶ added in v0.5.0
Int64SliceVar defines a slice of int64 option with specified name, default value, and usage string. The argument p points to a slice of int64 variable in which to store the value of the option.
func Int64Var ¶
Int64Var defines an int64 option with specified name, default value, and usage string. The argument p points to an int64 variable in which to store the value of the option.
func IntSlice ¶ added in v0.5.0
IntSlice defines a slice of int option with specified name, default value, and usage string. The return value is the address of a slice of int variable that stores the value of the option.
func IntSliceVar ¶ added in v0.5.0
IntSliceVar defines a slice of int option with specified name, default value, and usage string. The argument p points to a slice of int variable in which to store the value of the option.
func IntVar ¶
IntVar defines an int option with specified name, default value, and usage string. The argument p points to an int variable in which to store the value of the option.
func Parse ¶
func Parse() error
Parse parses the command-line env vars from os.Environ(). Must be called after all env vars are defined and before env vars are accessed by the program.
func PrintDefaults ¶
func PrintDefaults()
PrintDefaults prints, to standard error unless configured otherwise, a usage message showing the default settings of all defined options.
See the documentation *Figue.PrintDefaults for more information.
To change the destination for option messages, call CommandLine.SetOutput.
func String ¶
String defines a string option with specified name, default value, and usage string. The return value is the address of a string variable that stores the value of the option.
func StringSlice ¶ added in v0.5.0
StringSlice defines a slice of string option with specified name, default value, and usage string. The return value is the address of a slice of string variable that stores the value of the option.
func StringSliceVar ¶ added in v0.5.0
StringSliceVar defines a slice of string option with specified name, default value, and usage string. The argument p points to a slice of string variable in which to store the value of the option.
func StringVar ¶
StringVar defines a string option with specified name, default value, and usage string. The argument p points to a string variable in which to store the value of the option.
func TextVar ¶
func TextVar( p encoding.TextUnmarshaler, name string, value encoding.TextMarshaler, usage string, )
TextVar defines an option with a specified name, default value, and usage string. The argument p must be a pointer to a variable that will hold the value of the option, and p must implement encoding.TextUnmarshaler. If the option is used, the option value will be passed to p's UnmarshalText method. The type of the default value must be the same as the type of p.
func Uint ¶
Uint defines an uint option with specified name, default value, and usage string. The return value is the address of an uint variable that stores the value of the option.
func Uint64 ¶
Uint64 defines an uint64 option with specified name, default value, and usage string. The return value is the address of an uint64 variable that stores the value of the option.
func Uint64Slice ¶ added in v0.5.0
Uint64Slice defines a slice of uint64 option with specified name, default value, and usage string. The return value is the address of a slice of uint64 variable that stores the value of the option.
func Uint64SliceVar ¶ added in v0.5.0
Uint64SliceVar defines a slice of uint64 option with specified name, default value, and usage string. The argument p points to a slice of uint64 variable in which to store the value of the option.
func Uint64Var ¶
Uint64Var defines an uint64 option with specified name, default value, and usage string. The argument p points to an uint64 variable in which to store the value of the option.
func UintSlice ¶ added in v0.5.0
UintSlice defines a slice of uint option with specified name, default value, and usage string. The return value is the address of a slice of uint variable that stores the value of the option.
func UintSliceVar ¶ added in v0.5.0
UintSliceVar defines a slice of uint option with specified name, default value, and usage string. The argument p points to a slice of uint variable in which to store the value of the option.
func UintVar ¶
UintVar defines an uint option with specified name, default value, and usage string. The argument p points to an uint variable in which to store the value of the option.
func UnquoteUsage ¶
UnquoteUsage extracts a back-quoted name from the usage string for an option and returns it and the un-quoted usage. Given "a `name` to show" it returns ("name", "a name to show"). If there are no back quotes, the name is an educated guess of the type of the option's value, or the empty string if the option is boolean.
func UserDir ¶ added in v0.5.0
UserDir returns user configuration directory or provided default on error (e.g. when os.UserConfigDir fails).
func Var ¶
Var defines an option with the specified name and usage string. The type and value of the option are represented by the first argument, of type Value, which typically holds a user-defined implementation of Value. For instance, the caller could create an option that turns a comma-separated string into a slice of strings by giving the slice the methods of Value; in particular, Set would decompose the comma-separated string into the slice.
Types ¶
type Backend ¶
type Backend interface {
Init(name string)
Var(val Value, name, usage string) string
Parse() error
Parsed() bool
Set(name, value string) error
PrintDefaults()
SetOutput(io.Writer)
}
Backend define options registry and parser.
type Env ¶ added in v0.5.0
Env defines an environment variables based backend.
func (*Env) PrintDefaults ¶ added in v0.5.0
func (env *Env) PrintDefaults()
PrintDefaults implements Backend.
type ErrorHandling ¶
type ErrorHandling = flag.ErrorHandling
ErrorHandling defines how Figue.Parse behaves if the parse fails.
type Figue ¶
type Figue struct {
Usage func()
// contains filtered or unexported fields
}
Figue define the top level configuration loader.
func New ¶
func New( name string, errorHandling ErrorHandling, backends ...Backend, ) *Figue
New returns a new Fig instance. This function panics if 0 backend is provided.
func (*Figue) Bool ¶
Bool defines a bool option with specified name, default value, and usage string. The return value is the address of a bool variable that stores the value of the option.
func (*Figue) BoolSlice ¶ added in v0.5.0
Bool defines a slice of bool option with specified name, default value, and usage string. The return value is the address of a slice of bool variable that stores the value of the option.
func (*Figue) BoolSliceVar ¶ added in v0.5.0
BoolSliceVar defines a slice of bool option with specified name, default value, and usage string. The argument p points to a slice of bool variable in which to store the value of the option.
func (*Figue) BoolVar ¶
Bool defines a bool option with specified name, default value, and usage string. The argument p points to a bool variable in which to store the value of the option.
func (*Figue) Duration ¶
Duration defines a time.Duration option with specified name, default value, and usage string. The return value is the address of a time.Duration variable that stores the value of the option.
func (*Figue) DurationSlice ¶ added in v0.5.0
Duration defines a slice of time.Duration option with specified name, default value, and usage string. The return value is the address of a slice of time.Duration variable that stores the value of the option.
func (*Figue) DurationSliceVar ¶ added in v0.5.0
func (f *Figue) DurationSliceVar( p *[]time.Duration, name string, value []time.Duration, usage string, )
DurationSliceVar defines a slice of time.Duration option with specified name, default value, and usage string. The argument p points to a slice of time.Duration variable in which to store the value of the option.
func (*Figue) DurationVar ¶
Duration defines a time.Duration option with specified name, default value, and usage string. The argument p points to a time.Duration variable in which to store the value of the option.
func (*Figue) Float64 ¶
Float64 defines a float64 option with specified name, default value, and usage string. The return value is the address of a float64 variable that stores the value of the option.
func (*Figue) Float64Slice ¶ added in v0.5.0
Float64 defines a slice of float64 option with specified name, default value, and usage string. The return value is the address of a slice of float64 variable that stores the value of the option.
func (*Figue) Float64SliceVar ¶ added in v0.5.0
Float64SliceVar defines a slice of float64 option with specified name, default value, and usage string. The argument p points to a slice of float64 variable in which to store the value of the option.
func (*Figue) Float64Var ¶
Float64 defines a float64 option with specified name, default value, and usage string. The argument p points to a float64 variable in which to store the value of the option.
func (*Figue) Func ¶ added in v0.5.0
Func defines an option with the specified name and usage string. Each time the option is seen, fn is called with the value of the option. If fn returns a non-nil error, it will be treated as a value parsing error.
func (*Figue) Int ¶
Int defines an int option with specified name, default value, and usage string. The return value is the address of an int variable that stores the value of the option.
func (*Figue) Int64 ¶
Int64 defines an int64 option with specified name, default value, and usage string. The return value is the address of an int64 variable that stores the value of the option.
func (*Figue) Int64Slice ¶ added in v0.5.0
Int64 defines a slice of int64 option with specified name, default value, and usage string. The return value is the address of a slice of int64 variable that stores the value of the option.
func (*Figue) Int64SliceVar ¶ added in v0.5.0
Int64SliceVar defines a slice of int64 option with specified name, default value, and usage string. The argument p points to a slice of int64 variable in which to store the value of the option.
func (*Figue) Int64Var ¶
Int64 defines an int64 option with specified name, default value, and usage string. The argument p points to a int64 variable in which to store the value of the option.
func (*Figue) IntSlice ¶ added in v0.5.0
Int defines a slice of int option with specified name, default value, and usage string. The return value is the address of a slice of int variable that stores the value of the option.
func (*Figue) IntSliceVar ¶ added in v0.5.0
IntSliceVar defines a slice of int option with specified name, default value, and usage string. The argument p points to a slice of int variable in which to store the value of the option.
func (*Figue) IntVar ¶
Int defines an int option with specified name, default value, and usage string. The argument p points to a int variable in which to store the value of the option.
func (*Figue) Output ¶
Output returns the destination for usage and error messages. os.Stderr is returned if output was not set or was set to nil.
func (*Figue) Parse ¶
Parse parses and merges options from their sources. Must be called after all options in the Figue are defined and before options are accessed by the program.
func (*Figue) PrintDefaults ¶
func (f *Figue) PrintDefaults()
PrintDefaults prints, to standard error unless configured otherwise, the default values of all defined command-line options. To do so, it calls in reverse order [Backend.PrintDefaults] of all backends.
func (*Figue) SetOutput ¶
SetOutput sets the destination for usage and error messages. If output is nil, os.Stderr is used.
func (*Figue) String ¶
String defines a string option with specified name, default value, and usage string. The return value is the address of a string variable that stores the value of the option.
func (*Figue) StringSlice ¶ added in v0.5.0
String defines a slice of string option with specified name, default value, and usage string. The return value is the address of a slice of string variable that stores the value of the option.
func (*Figue) StringSliceVar ¶ added in v0.5.0
StringSliceVar defines a slice of string option with specified name, default value, and usage string. The argument p points to a slice of string variable in which to store the value of the option.
func (*Figue) StringVar ¶
String defines a string option with specified name, default value, and usage string. The argument p points to a string variable in which to store the value of the option.
func (*Figue) TextVar ¶
func (f *Figue) TextVar( p encoding.TextUnmarshaler, name string, value encoding.TextMarshaler, usage string, )
TextVar defines an option with a specified name, default value, and usage string. The argument p must be a pointer to a variable that will hold the value of the option, and p must implement encoding.TextUnmarshal. If the option is used, the option value will be passed to p's UnmarshalText method. The type of the default value must be the same as the type of p.
func (*Figue) Uint ¶
Uint defines an uint option with specified name, default value, and usage string. The return value is the address of an uint variable that stores the value of the option.
func (*Figue) Uint64 ¶
Uint64 defines an uint64 option with specified name, default value, and usage string. The return value is the address of an uint64 variable that stores the value of the option.
func (*Figue) Uint64Slice ¶ added in v0.5.0
Uint64 defines a slice of uint64 option with specified name, default value, and usage string. The return value is the address of a slice of uint64 variable that stores the value of the option.
func (*Figue) Uint64SliceVar ¶ added in v0.5.0
Uint64SliceVar defines a slice of uint64 option with specified name, default value, and usage string. The argument p points to a slice of uint64 variable in which to store the value of the option.
func (*Figue) Uint64Var ¶
Uint64 defines an uint64 option with specified name, default value, and usage string. The argument p points to a uint64 variable in which to store the value of the option.
func (*Figue) UintSlice ¶ added in v0.5.0
Uint defines a slice of uint option with specified name, default value, and usage string. The return value is the address of a slice of uint variable that stores the value of the option.
func (*Figue) UintSliceVar ¶ added in v0.5.0
UintSliceVar defines a slice of uint option with specified name, default value, and usage string. The argument p points to a slice of uint variable in which to store the value of the option.
func (*Figue) UintVar ¶
Uint defines an uint option with specified name, default value, and usage string. The argument p points to a uint variable in which to store the value of the option.
func (*Figue) Var ¶
Var defines an option with the specified name and usage string. The type and value of the option are represented by the first argument, of type Value, which typically holds a user-defined implementation of Value. For instance, the caller could create an option that turns a comma-separated string into a slice of strings by giving the slice the methods of Value; in particular, Set would decompose the comma-separated string into the slice.
type Flag ¶ added in v0.5.0
Flag defines a flag based Backend implementation.
func (*Flag) PrintDefaults ¶ added in v0.5.0
func (flag *Flag) PrintDefaults()
PrintDefaults implements Backend.
type Ini ¶ added in v0.5.0
Ini defines an INI file based Backend implementation.
func NewINI ¶ added in v0.5.0
NewINI returns a new INI based backend that will parse data from provided filepath. If the file doesn't exist, this backend will parse nothing.
func (*Ini) PrintDefaults ¶ added in v0.5.0
func (ini *Ini) PrintDefaults()
PrintDefaults implements Backend. Unlike other backends, we only print path to config file here.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package env implements command-line environment variables parsing.
|
Package env implements command-line environment variables parsing. |
|
Package ini implements INI properties parsing.
|
Package ini implements INI properties parsing. |
|
Package option provides flag.Value implementation for basic types.
|
Package option provides flag.Value implementation for basic types. |
