Documentation
¶
Overview ¶
Package ini implements INI properties parsing.
Usage ¶
Define properties using ini.String, Bool, Int, etc.
This declares an integer property, N, stored in the pointer nProp, with type *int:
import "github.com/negrel/configue/ini"
var nProp = ini.Int("N", 1234, "help message for INI property N")
If you like, you can bind the property to a variable using the Var() functions.
var prop int
func init() {
ini.IntVar(&prop, "PROPNAME", 1234, "help message for PROPNAME")
}
Or you can create custom properties that satisfy the Value interface (with pointer receivers) and couple them to property parsing by
ini.Var(&prop, "PROPNAME", "help message for PROPNAME")
For such properties, the default value is just the initial value of the variable.
After all properties are defined, call
ini.Parse(r) // r is an io.Reader.
to parse the command line into the defined properties.
Properties may then be used directly. If you're using the properties themselves, they are all pointers; if you bind to variables, they're values.
fmt.Println("ip has value ", *ip)
fmt.Println("property has value ", prop)
Command line property syntax ¶
See [`option`](../option#pkg-overview) documentation for options syntax of basic types.
The default set of command-line properties is controlled by top-level functions. The PropSet type allows one to define independent sets of properties.
Index ¶
- Constants
- Variables
- 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 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(r io.Reader) 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 Var(value option.Value, name string, usage string)
- func Visit(fn func(*Property))
- func VisitAll(fn func(*Property))
- type ErrorHandling
- type PropSet
- func (ps *PropSet) Bool(name string, value bool, usage string) *bool
- func (ps *PropSet) BoolSlice(name string, value []bool, usage string) *[]bool
- func (ps *PropSet) BoolSliceVar(p *[]bool, name string, value []bool, usage string)
- func (ps *PropSet) BoolVar(p *bool, name string, value bool, usage string)
- func (ps *PropSet) Duration(name string, value time.Duration, usage string) *time.Duration
- func (ps *PropSet) DurationSlice(name string, value []time.Duration, usage string) *[]time.Duration
- func (ps *PropSet) DurationSliceVar(p *[]time.Duration, name string, value []time.Duration, usage string)
- func (ps *PropSet) DurationVar(p *time.Duration, name string, value time.Duration, usage string)
- func (ps *PropSet) Float64(name string, value float64, usage string) *float64
- func (ps *PropSet) Float64Slice(name string, value []float64, usage string) *[]float64
- func (ps *PropSet) Float64SliceVar(p *[]float64, name string, value []float64, usage string)
- func (ps *PropSet) Float64Var(p *float64, name string, value float64, usage string)
- func (ps *PropSet) Func(name, usage string, fn func(string) error)
- func (ps *PropSet) Init(name string, errorHandling ErrorHandling)
- func (ps *PropSet) Int(name string, value int, usage string) *int
- func (ps *PropSet) Int64(name string, value int64, usage string) *int64
- func (ps *PropSet) Int64Slice(name string, value []int64, usage string) *[]int64
- func (ps *PropSet) Int64SliceVar(p *[]int64, name string, value []int64, usage string)
- func (ps *PropSet) Int64Var(p *int64, name string, value int64, usage string)
- func (ps *PropSet) IntSlice(name string, value []int, usage string) *[]int
- func (ps *PropSet) IntSliceVar(p *[]int, name string, value []int, usage string)
- func (ps *PropSet) IntVar(p *int, name string, value int, usage string)
- func (ps *PropSet) Lookup(name string) *Property
- func (ps *PropSet) Name() string
- func (ps *PropSet) Output() io.Writer
- func (ps *PropSet) Parse(r io.Reader) error
- func (ps *PropSet) Parsed() bool
- func (ps *PropSet) PrintDefaults()
- func (ps *PropSet) Set(key, value string) error
- func (ps *PropSet) SetOutput(output io.Writer)
- func (ps *PropSet) String(name string, value string, usage string) *string
- func (ps *PropSet) StringSlice(name string, value []string, usage string) *[]string
- func (ps *PropSet) StringSliceVar(p *[]string, name string, value []string, usage string)
- func (ps *PropSet) StringVar(p *string, name string, value string, usage string)
- func (ps *PropSet) TextVar(p encoding.TextUnmarshaler, name string, value encoding.TextMarshaler, ...)
- func (ps *PropSet) Uint(name string, value uint, usage string) *uint
- func (ps *PropSet) Uint64(name string, value uint64, usage string) *uint64
- func (ps *PropSet) Uint64Slice(name string, value []uint64, usage string) *[]uint64
- func (ps *PropSet) Uint64SliceVar(p *[]uint64, name string, value []uint64, usage string)
- func (ps *PropSet) Uint64Var(p *uint64, name string, value uint64, usage string)
- func (ps *PropSet) UintSlice(name string, value []uint, usage string) *[]uint
- func (ps *PropSet) UintSliceVar(p *[]uint, name string, value []uint, usage string)
- func (ps *PropSet) UintVar(p *uint, name string, value uint, usage string)
- func (ps *PropSet) Var(val option.Value, name string, usage string)
- func (ps *PropSet) Visit(fn func(*Property))
- func (ps *PropSet) VisitAll(fn func(*Property))
- type Property
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 PropSet.Parse to behave as described if the parse fails.
Variables ¶
var ( // CommandLine is the default set of command-line properties, parsed from // provided io.Reader when calling [Parse]. The top-level functions such as // BoolVar, and so on are wrappers for the methods of CommandLine. CommandLine = NewPropSet("", ExitOnError) // Usage prints a usage message documenting all defined command-line // properties to CommandLine's output, which by default is os.Stderr. It is // called when an error occurs while parsing INI properties. 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() } )
Functions ¶
func Bool ¶
Bool defines a bool property with specified name, default value, and usage string. The return value is the address of a bool variable that stores the value of the property.
func BoolSlice ¶
BoolSlice defines a slice of bool property 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 property.
func BoolSliceVar ¶
BoolSliceVar defines a slice of bool property 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 property.
func BoolVar ¶
BoolVar defines a bool property with specified name, default value, and usage string. The argument p points to a bool variable in which to store the value of the property.
func Duration ¶
Duration defines a time.Duration property 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 property.
func DurationSlice ¶
DurationSlice defines a slice of time.Duration property 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 property.
func DurationSliceVar ¶
DurationSliceVar defines a slice of time.Duration property 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 property.
func DurationVar ¶
DurationVar defines a time.Duration property 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 property.
func Float64 ¶
Float64 defines a float64 property with specified name, default value, and usage string. The return value is the address of a float64 variable that stores the value of the property.
func Float64Slice ¶
Float64Slice defines a slice of float64 property 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 property.
func Float64SliceVar ¶
Float64SliceVar defines a slice of float64 property 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 property.
func Float64Var ¶
Float64Var defines a float64 property with specified name, default value, and usage string. The argument p points to a float64 variable in which to store the value of the property.
func Func ¶
Func defines a property with the specified name and usage string. Each time the property is seen, fn is called with the value of the property. If fn returns a non-nil error, it will be treated as a value parsing error.
func Int ¶
Int defines an int property with specified name, default value, and usage string. The return value is the address of an int variable that stores the value of the property.
func Int64 ¶
Int64 defines an int64 property with specified name, default value, and usage string. The return value is the address of an int64 variable that stores the value of the property.
func Int64Slice ¶
Int64Slice defines a slice of int64 property 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 property.
func Int64SliceVar ¶
Int64SliceVar defines a slice of int64 property 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 property.
func Int64Var ¶
Int64Var defines an int64 property with specified name, default value, and usage string. The argument p points to an int64 variable in which to store the value of the property.
func IntSlice ¶
IntSlice defines a slice of int property 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 property.
func IntSliceVar ¶
IntSliceVar defines a slice of int property 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 property.
func IntVar ¶
IntVar defines an int property with specified name, default value, and usage string. The argument p points to an int variable in which to store the value of the property.
func Parse ¶
Parse parses the command-line properties from provided io.Reader. Must be called after all properties are defined and before properties are accessed by the program.
func Parsed ¶
func Parsed() bool
Parsed reports whether the command-line propertys 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 propertys.
See the documentation *PropSet.PrintDefaults for more information.
To change the destination for property messages, call CommandLine.SetOutput.
func String ¶
String defines a string property with specified name, default value, and usage string. The return value is the address of a string variable that stores the value of the property.
func StringSlice ¶
StringSlice defines a slice of string property 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 property.
func StringSliceVar ¶
StringSliceVar defines a slice of string property 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 property.
func StringVar ¶
StringVar defines a string property with specified name, default value, and usage string. The argument p points to a string variable in which to store the value of the property.
func TextVar ¶
func TextVar( p encoding.TextUnmarshaler, name string, value encoding.TextMarshaler, usage string, )
TextVar defines a property 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 property, and p must implement encoding.TextUnmarshaler. If the property is used, the property 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 property with specified name, default value, and usage string. The return value is the address of an uint variable that stores the value of the property.
func Uint64 ¶
Uint64 defines an uint64 property with specified name, default value, and usage string. The return value is the address of an uint64 variable that stores the value of the property.
func Uint64Slice ¶
Uint64Slice defines a slice of uint64 property 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 property.
func Uint64SliceVar ¶
Uint64SliceVar defines a slice of uint64 property 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 property.
func Uint64Var ¶
Uint64Var defines an uint64 property with specified name, default value, and usage string. The argument p points to an uint64 variable in which to store the value of the property.
func UintSlice ¶
UintSlice defines a slice of uint property 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 property.
func UintSliceVar ¶
UintSliceVar defines a slice of uint property 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 property.
func UintVar ¶
UintVar defines an uint property with specified name, default value, and usage string. The argument p points to an uint variable in which to store the value of the property.
func UnquoteUsage ¶
UnquoteUsage extracts a back-quoted name from the usage string for an property 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 property's value, or the empty string if the property is boolean.
func Var ¶
Var defines an property with the specified name and usage string. The type and value of the property are represented by the first argument, of type [Value], which typically holds a user-defined implementation of [Value]. For instance, the caller could create a property 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 ErrorHandling ¶
type ErrorHandling = flag.ErrorHandling
ErrorHandling defines how PropSet.Parse behaves if the parse fails.
type PropSet ¶
type PropSet struct {
Usage func()
// contains filtered or unexported fields
}
PropSet represents a set of defined options. The zero value of a PropSet has no name and has ContinueOnError error handling.
PropSet names must be unique within a PropSet. An attempt to define an option whose name is already in use will cause a panic.
func NewPropSet ¶
func NewPropSet(name string, errorHandling ErrorHandling) *PropSet
func (*PropSet) 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 (*PropSet) BoolSlice ¶
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 (*PropSet) BoolSliceVar ¶
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 (*PropSet) 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 (*PropSet) 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 (*PropSet) DurationSlice ¶
func (ps *PropSet) 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 (*PropSet) DurationSliceVar ¶
func (ps *PropSet) 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 (*PropSet) 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 (*PropSet) 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 (*PropSet) Float64Slice ¶
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 (*PropSet) Float64SliceVar ¶
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 (*PropSet) 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 (*PropSet) Func ¶
Func defines a property with the specified name and usage string. Each time the property is seen, fn is called with the value of the property. If fn returns a non-nil error, it will be treated as a value parsing error.
func (*PropSet) Init ¶
func (ps *PropSet) Init(name string, errorHandling ErrorHandling)
Init sets the name and error handling property for a property set. By default, the zero PropSet uses an empty name and the ContinueOnError error handling policy.
func (*PropSet) 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 (*PropSet) 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 (*PropSet) Int64Slice ¶
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 (*PropSet) Int64SliceVar ¶
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 (*PropSet) 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 (*PropSet) IntSlice ¶
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 (*PropSet) IntSliceVar ¶
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 (*PropSet) 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 (*PropSet) Lookup ¶
Lookup returns the Property structure of the named property, returning nil if none exists.
func (*PropSet) 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 (*PropSet) Parse ¶
Parse parses INI properties from the provided io.Reader. Must be called after all properties in the PropSet are defined and before properties are accessed by the program.
func (*PropSet) PrintDefaults ¶
func (ps *PropSet) PrintDefaults()
PrintDefaults prints, to standard error unless configured otherwise, the default values of all defined properties in the set. For an integer valued property X with a default value of 7, the output has the form
; usage-message-for-x X = 7
The usage message appear as an INI comment on a separate line.
func (*PropSet) SetOutput ¶
SetOutput sets the destination for usage and error messages. If output is nil, os.Stderr is used.
func (*PropSet) 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 (*PropSet) StringSlice ¶
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 (*PropSet) StringSliceVar ¶
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 (*PropSet) 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 (*PropSet) TextVar ¶
func (ps *PropSet) 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 (*PropSet) 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 (*PropSet) 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 (*PropSet) Uint64Slice ¶
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 (*PropSet) Uint64SliceVar ¶
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 (*PropSet) 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 (*PropSet) UintSlice ¶
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 (*PropSet) UintSliceVar ¶
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 (*PropSet) 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 (*PropSet) Var ¶
Var defines a property with the specified name and usage string. The type and value of the option are represented by the first argument, of type option.Value, which typically holds a user-defined implementation of option.Value. For instance, the caller could create a property that turns a comma-separated string into a slice of strings by giving the slice the methods of option.Value; in particular, Set would decompose the comma-separated string into the slice.