configue

package module
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Sep 15, 2025 License: MIT Imports: 12 Imported by: 0

README

cute fig

configue - Configuration library for Go

Go doc go report card license card PRs welcome card Go version card

configue is a library for reading configuration from different sources in Go. It is a simpler alternative to popular configuration loading libraries (spf13/viper, knadh/koanf, etc).

configue has package for reading configuration from environment variables, INI files and command line flags. It is easy to plug in custom source by implementing the Backend interface.

There is no external dependency and the API is strongly inspired by the flag package from the standard library.

Getting started

Here is a simple example:

package main

import (
	"errors"
	"fmt"
	"runtime"

	"github.com/negrel/configue"
)

type Option struct {
	Debug   bool
	MaxProc int
}

func main() {
	// Create backends.
	env := configue.NewEnv("MYAPP")
	flag := configue.NewFlag()
	ini := configue.NewINI(configue.File("./", "config.ini"))

	// Create a figue that loads from flags, INI file, env vars and flags in this
	// specific order.
	figue := configue.New(
		"",                       // Subcommand name.
		configue.ContinueOnError, // Error handling strategy.
		flag,
		ini,
		env,
		flag,
	)
	figue.Usage = func() {
		_, _ = fmt.Fprintln(figue.Output(), "myapp - a great app")
		_, _ = fmt.Fprintln(figue.Output())
		_, _ = fmt.Fprintln(figue.Output(), "Usage:")
		_, _ = fmt.Fprintln(figue.Output(), "  myapp [flags]")
		_, _ = fmt.Fprintln(figue.Output())
		figue.PrintDefaults()
	}

	// Define options.
	var options Option
	figue.BoolVar(&options.Debug, "debug", false, "enable debug logs")
	figue.IntVar(&options.MaxProc, "max.proc", runtime.NumCPU(), "maximum number of CPU that can be executed simultaneously")

	// Flag only option.
	flag.StringVar(&ini.FilePath, "config", ini.FilePath, "custom config file path")

	// Parse options.
	err := figue.Parse()
	if errors.Is(err, configue.ErrHelp) {
		return
	}
	if err != nil {
		// handle error
	}
}
$ myapp -h
myapp - a great app

Usage:
  myapp [flags]

Flags:
  -config string
        custom config file path (default "/home/anegrel/.config/myapp/config.ini")
  -debug
        enable debug logs
  -max-proc value
        maximum number of CPU that can be executed simultaneously (default 16)

Environment variables:
  MYAPP_DEBUG
        enable debug logs
  MYAPP_MAX_PROC int
        maximum number of CPU that can be executed simultaneously (default 16)

Configuration file is located at /home/anegrel/.config/myapp/config.ini

For a real example, see Prisme Analytics configuration loading.

Contributing

If you want to contribute to configue to add a feature or improve the code contact me at [email protected], open an issue or make a pull request.

🌠 Show your support

Please give a ⭐ if this project helped you!

buy me a coffee

📜 License

MIT © Alexandre Negrel

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

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

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

func AppDir(defaultUserDir string) string

AppDir returns application configuration directory which is UserDir joined with os.Args[0].

func Bool

func Bool(name string, value bool, usage string) *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

func BoolSlice(name string, value []bool, usage string) *[]bool

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

func BoolSliceVar(p *[]bool, name string, value []bool, usage string)

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

func BoolVar(p *bool, name string, value bool, usage string)

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

func Duration(name string, value time.Duration, usage string) *time.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

func DurationSlice(name string, value []time.Duration, usage string) *[]time.Duration

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

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

func DurationVar(p *time.Duration, name string, value time.Duration, usage string)

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

func File(defaultUserDir, fname string) string

File returns path to configuration file by joining AppDir with provided `fname`.

func Float64

func Float64(name string, value float64, usage string) *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

func Float64Slice(name string, value []float64, usage string) *[]float64

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

func Float64SliceVar(p *[]float64, name string, value []float64, usage string)

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

func Float64Var(p *float64, name string, value float64, usage string)

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 Func(name, usage string, fn func(string) error)

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

func Int(name string, value int, usage string) *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

func Int64(name string, value int64, usage string) *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

func Int64Slice(name string, value []int64, usage string) *[]int64

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

func Int64SliceVar(p *[]int64, name string, value []int64, usage string)

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

func Int64Var(p *int64, name string, value int64, usage string)

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

func IntSlice(name string, value []int, usage string) *[]int

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

func IntSliceVar(p *[]int, name string, value []int, usage string)

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

func IntVar(p *int, name string, value int, usage string)

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 Parsed

func Parsed() bool

Parsed reports whether the command-line options have been parsed.

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 Set

func Set(name, value string) error

Set sets the value of the named command-line option.

func String

func String(name string, value string, usage string) *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

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

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

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

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

func StringVar(p *string, name string, value string, usage string)

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

func Uint(name string, value uint, usage string) *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

func Uint64(name string, value uint64, usage string) *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

func Uint64Slice(name string, value []uint64, usage string) *[]uint64

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

func Uint64SliceVar(p *[]uint64, name string, value []uint64, usage string)

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

func Uint64Var(p *uint64, name string, value uint64, usage string)

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

func UintSlice(name string, value []uint, usage string) *[]uint

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

func UintSliceVar(p *[]uint, name string, value []uint, usage string)

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

func UintVar(p *uint, name string, value uint, usage string)

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

func UnquoteUsage(optValue option.Value, optUsage string) (name string, usage string)

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

func UserDir(def string) string

UserDir returns user configuration directory or provided default on error (e.g. when os.UserConfigDir fails).

func Var

func Var(value option.Value, name string, usage string)

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

type Env struct {
	*env.EnvSet
	// contains filtered or unexported fields
}

Env defines an environment variables based backend.

func NewEnv

func NewEnv(prefix string) *Env

NewEnv returns a new environment variable based Backend implementation.

func (*Env) Init added in v0.5.0

func (env *Env) Init(name string)

Init implements Backend.

func (*Env) Parse added in v0.5.0

func (env *Env) Parse() error

Parse implements Backend by parsing os.Environ().

func (*Env) PrintDefaults added in v0.5.0

func (env *Env) PrintDefaults()

PrintDefaults implements Backend.

func (*Env) Set added in v0.5.0

func (env *Env) Set(name, value string) error

Set sets the value of the named command-line option.

func (*Env) Var added in v0.5.0

func (env *Env) Var(val Value, name, usage string) string

Var implements Backend.

func (*Env) Visit added in v0.5.0

func (eb *Env) Visit(fn func(option.Option))

Visit 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

func (f *Figue) Bool(
	name string,
	value bool,
	usage string,
) *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

func (f *Figue) BoolSlice(
	name string,
	value []bool,
	usage string,
) *[]bool

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

func (f *Figue) BoolSliceVar(
	p *[]bool,
	name string,
	value []bool,
	usage string,
)

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

func (f *Figue) BoolVar(
	p *bool,
	name string,
	value bool,
	usage string,
)

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

func (f *Figue) Duration(
	name string,
	value time.Duration,
	usage string,
) *time.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

func (f *Figue) DurationSlice(
	name string,
	value []time.Duration,
	usage string,
) *[]time.Duration

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

func (f *Figue) DurationVar(
	p *time.Duration,
	name string,
	value time.Duration,
	usage string,
)

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

func (f *Figue) Float64(
	name string,
	value float64,
	usage string,
) *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

func (f *Figue) Float64Slice(
	name string,
	value []float64,
	usage string,
) *[]float64

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

func (f *Figue) Float64SliceVar(
	p *[]float64,
	name string,
	value []float64,
	usage string,
)

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

func (f *Figue) Float64Var(
	p *float64,
	name string,
	value float64,
	usage string,
)

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 (f *Figue) Func(name, usage string, fn func(string) error)

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

func (f *Figue) Int(
	name string,
	value int,
	usage string,
) *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

func (f *Figue) Int64(
	name string,
	value int64,
	usage string,
) *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

func (f *Figue) Int64Slice(
	name string,
	value []int64,
	usage string,
) *[]int64

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

func (f *Figue) Int64SliceVar(
	p *[]int64,
	name string,
	value []int64,
	usage string,
)

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

func (f *Figue) Int64Var(
	p *int64,
	name string,
	value int64,
	usage string,
)

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

func (f *Figue) IntSlice(
	name string,
	value []int,
	usage string,
) *[]int

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

func (f *Figue) IntSliceVar(
	p *[]int,
	name string,
	value []int,
	usage string,
)

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

func (f *Figue) IntVar(
	p *int,
	name string,
	value int,
	usage string,
)

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

func (f *Figue) Output() io.Writer

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

func (f *Figue) Parse() error

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

func (f *Figue) Parsed() bool

Parsed reports whether Figue.Parse has been called.

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

func (f *Figue) Set(name, value string) error

Set sets the value of the named command-line option.

func (*Figue) SetOutput

func (f *Figue) SetOutput(w io.Writer)

SetOutput sets the destination for usage and error messages. If output is nil, os.Stderr is used.

func (*Figue) String

func (f *Figue) String(
	name string,
	value string,
	usage string,
) *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

func (f *Figue) StringSlice(
	name string,
	value []string,
	usage string,
) *[]string

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

func (f *Figue) StringSliceVar(
	p *[]string,
	name string,
	value []string,
	usage string,
)

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

func (f *Figue) StringVar(
	p *string,
	name string,
	value string,
	usage string,
)

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

func (f *Figue) Uint(
	name string,
	value uint,
	usage string,
) *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

func (f *Figue) Uint64(
	name string,
	value uint64,
	usage string,
) *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

func (f *Figue) Uint64Slice(
	name string,
	value []uint64,
	usage string,
) *[]uint64

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

func (f *Figue) Uint64SliceVar(
	p *[]uint64,
	name string,
	value []uint64,
	usage string,
)

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

func (f *Figue) Uint64Var(
	p *uint64,
	name string,
	value uint64,
	usage string,
)

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

func (f *Figue) UintSlice(
	name string,
	value []uint,
	usage string,
) *[]uint

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

func (f *Figue) UintSliceVar(
	p *[]uint,
	name string,
	value []uint,
	usage string,
)

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

func (f *Figue) UintVar(
	p *uint,
	name string,
	value uint,
	usage string,
)

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

func (f *Figue) Var(val option.Value, path string, usage string)

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

type Flag struct {
	*flag.FlagSet
	// contains filtered or unexported fields
}

Flag defines a flag based Backend implementation.

func NewFlag

func NewFlag() *Flag

NewFlag returns a new flag based backend.

func (*Flag) Init added in v0.5.0

func (flag *Flag) Init(name string)

Init implements Backend.

func (*Flag) Parse added in v0.5.0

func (flag *Flag) Parse() error

Parse implements Backend by parsing flags from os.Args[1:].

func (*Flag) PrintDefaults added in v0.5.0

func (flag *Flag) PrintDefaults()

PrintDefaults implements Backend.

func (*Flag) Set added in v0.5.0

func (flag *Flag) Set(name, value string) error

Set sets the value of the named command-line option.

func (*Flag) Var added in v0.5.0

func (flag *Flag) Var(val Value, name, usage string) string

Var implements Backend.

func (*Flag) Visit added in v0.5.0

func (fb *Flag) Visit(fn func(option.Option))

Visit visits the flags in lexicographical order, calling fn for each. It visits only those flags that have been set.

func (*Flag) VisitAll added in v0.5.0

func (fb *Flag) VisitAll(fn func(option.Option))

VisitAll visits the flags in lexicographical order, calling fn for each. It visits all flags, even those not set.

type Ini added in v0.5.0

type Ini struct {
	*ini.PropSet
	FilePath string
}

Ini defines an INI file based Backend implementation.

func NewINI added in v0.5.0

func NewINI(fpath string) *Ini

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) Init added in v0.5.0

func (ini *Ini) Init(name string)

Init implements Backend.

func (*Ini) Parse added in v0.5.0

func (ini *Ini) Parse() error

Parse implements Backend.

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.

func (*Ini) Set added in v0.5.0

func (ini *Ini) Set(name, value string) error

Set sets the value of the named command-line option.

func (*Ini) Var added in v0.5.0

func (ini *Ini) Var(val Value, name, usage string) string

Var implements Backend.

func (*Ini) Visit added in v0.5.0

func (ib *Ini) Visit(fn func(option.Option))

Visit implements Backend.

type Value

type Value interface {
	fmt.Stringer
	Set(string) error
}

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.

Jump to

Keyboard shortcuts

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