Documentation
¶
Overview ¶
Package conflag is a simple wrapper around flag package that fills flag values from configuration files before parsing them from command-line.
Configuration files contain list of flags in the same format they would appear as command line arguments, but without the leading dash:
http=localhost:8080 play=false
The order of loading configurations is:
/etc/progname $HOME/.progname
These files are parsed before command-line arguments, so real arguments override flags from configuration file.
$ mycmd -play=true
TODO: Support Windows-specific paths.
Use this package like you would normally use flag:
import ( ... flag "github.com/dchest/conflag" ... )
But before calling Parse(), set program name:
func main() { flag.SetProgName("mycmd") flag.Parse() ... }
If your program overwrites Usage variable, call SetUsage() instead.
Index ¶
- func Arg(i int) string
- func Args() []string
- func Bool(name string, value bool, usage string) *bool
- func BoolVar(p *bool, name string, value bool, usage string)
- func Duration(name string, value time.Duration, usage string) *time.Duration
- func DurationVar(p *time.Duration, name string, value time.Duration, usage string)
- func Float64(name string, value float64, usage string) *float64
- func Float64Var(p *float64, name string, value float64, usage string)
- func GlobalConfigFilePath() string
- func Int(name string, value int, usage string) *int
- func Int64(name string, value int64, usage string) *int64
- func Int64Var(p *int64, name string, value int64, usage string)
- func IntVar(p *int, name string, value int, usage string)
- func Lookup(name string) *flag.Flag
- func NArg() int
- func NFlag() int
- func NewFlagSet(name string, errorHandling flag.ErrorHandling) *flag.FlagSet
- func Parse()
- func Parsed() bool
- func PrintDefaults()
- func Set(name, value string) error
- func SetProgName(name string)
- func SetUsage(usage func())
- func String(name string, value string, usage string) *string
- func StringVar(p *string, name string, value string, usage string)
- func Uint(name string, value uint, usage string) *uint
- func Uint64(name string, value uint64, usage string) *uint64
- func Uint64Var(p *uint64, name string, value uint64, usage string)
- func UintVar(p *uint, name string, value uint, usage string)
- func Usage()
- func UserConfigFilePath() string
- func Var(value flag.Value, name string, usage string)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Arg ¶
Arg returns the i'th command-line argument. Arg(0) is the first remaining argument after flags have been processed.
func Bool ¶
Bool defines a bool flag with specified name, default value, and usage string. The return value is the address of a bool variable that stores the value of the flag.
func BoolVar ¶
BoolVar defines a bool flag with specified name, default value, and usage string. The argument p points to a bool variable in which to store the value of the flag.
func Duration ¶
Duration defines a time.Duration flag 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 flag.
func DurationVar ¶
DurationVar defines a time.Duration flag 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 flag.
func Float64 ¶
Float64 defines a float64 flag with specified name, default value, and usage string. The return value is the address of a float64 variable that stores the value of the flag.
func Float64Var ¶
Float64Var defines a float64 flag with specified name, default value, and usage string. The argument p points to a float64 variable in which to store the value of the flag.
func GlobalConfigFilePath ¶
func GlobalConfigFilePath() string
GlobalConfigFilePath returns user configuration file path (/etc/progname). If program name is not set, returns an empty string.
func Int ¶
Int defines an int flag with specified name, default value, and usage string. The return value is the address of an int variable that stores the value of the flag.
func Int64 ¶
Int64 defines an int64 flag with specified name, default value, and usage string. The return value is the address of an int64 variable that stores the value of the flag.
func Int64Var ¶
Int64Var defines an int64 flag with specified name, default value, and usage string. The argument p points to an int64 variable in which to store the value of the flag.
func IntVar ¶
IntVar defines an int flag with specified name, default value, and usage string. The argument p points to an int variable in which to store the value of the flag.
func Lookup ¶
Lookup returns the Flag structure of the named command-line flag, returning nil if none exists.
func NArg ¶
func NArg() int
NArg is the number of arguments remaining after flags have been processed.
func NewFlagSet ¶
func NewFlagSet(name string, errorHandling flag.ErrorHandling) *flag.FlagSet
NewFlagSet returns a new, empty flag set with the specified name and error handling property.
func Parse ¶
func Parse()
Parse parses the command-line flags from os.Args[1:]. Must be called after all flags are defined and before flags are accessed by the program.
func PrintDefaults ¶
func PrintDefaults()
PrintDefaults prints to standard error the default values of all defined command-line flags.
func SetProgName ¶
func SetProgName(name string)
SetProgName sets program name, which is used for locating configuration file, and outputting usage or error information.
If program name is not set, the package won't use configuration file, only command line arguments.
func String ¶
String 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 StringVar ¶
StringVar 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.
func Uint ¶
Uint defines a uint flag with specified name, default value, and usage string. The return value is the address of a uint variable that stores the value of the flag.
func Uint64 ¶
Uint64 defines a uint64 flag with specified name, default value, and usage string. The return value is the address of a uint64 variable that stores the value of the flag.
func Uint64Var ¶
Uint64Var defines a uint64 flag with specified name, default value, and usage string. The argument p points to a uint64 variable in which to store the value of the flag.
func UintVar ¶
UintVar defines a uint flag with specified name, default value, and usage string. The argument p points to a uint variable in which to store the value of the flag.
func UserConfigFilePath ¶
func UserConfigFilePath() string
UserConfigFilePath returns user configuration file path ($HOME/.progname). If program name is not set, returns an empty string.
func Var ¶
Var defines a flag with the specified name and usage string. The type and value of the flag 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 flag 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 ¶
This section is empty.