Documentation ¶
Index ¶
- Constants
- Variables
- func GetDefaultFile() string
- func GetDefaultFlags() map[string]interface{}
- func Init(errorHandling ErrorHandling)
- func LookupFlag(name string) *flag.Flag
- func ParseConfigFile(cfg interface{}, filename string, dirs ...string) (err error)
- func ParseDefaultConfigFile(cfg interface{}) (err error)
- func ParseFlags() error
- func ParsedFlag(f *flag.Flag) bool
- func SetDefaultFile(fpath string) (err error)
- func SetEnvPrefix(prefix string)
- func SetEnvsToParse(envVarNames []string) (err error)
- func SetFlagDefault(fName, def string) error
- func SetFlagSet(f *flag.FlagSet)
- func SetFlagSetArgs(args []string)
- func SetUpConfiguration(cfg interface{}) (err error)
- func SetUpConfigurationWithConfigFile(cfg interface{}, filename string, dirs ...string) (err error)
- func String(c interface{}) string
- func StringIgnoreZeroValues(c interface{}) string
- func Usage()
- type ErrorHandling
- type FlagValue
- type FlagValueBool
Constants ¶
const ( ContinueOnError = ErrorHandling(flag.ContinueOnError) // Return a descriptive error. ExitOnError = ErrorHandling(flag.ExitOnError) // Call os.Exit(2) or for -h/-help Exit(0). PanicOnError = ErrorHandling(flag.PanicOnError) // Call panic with a descriptive error. )
Variables ¶
var ( ErrNoDefaultConfig = errors.New("no default config file to parse") ErrFailedToParseDefaultConfig = fmt.Errorf("failed to parse default config (%s)", defaultFile) ErrNotAPointer = errors.New("argument to must be a pointer") ErrInvalidConfigFile = errors.New("unsupported or invalid file") ErrInvalidFormat = errors.New("invalid format of file") ErrNoConfigFileToParse = errors.New("no file given to parse") ErrNoFileFound = syscall.Errno(2) // "could not find file" )
Functions ¶
func GetDefaultFile ¶
func GetDefaultFile() string
func GetDefaultFlags ¶
func GetDefaultFlags() map[string]interface{}
Returns a map of all flag defaults. The key is the flag name and the value the flag's default value.
func Init ¶ added in v0.0.5
func Init(errorHandling ErrorHandling)
Init sets the global error handling property, as well as the error handling property for the flagset.
The error handling for the config package is similar to that of the standard flag package; there are three modes: Continue, Panic and Exit.
The default mode is Continue.
func LookupFlag ¶
Returns the Flag structure of the named flag of the global flag set, returning nil if none exists. By default, the global flag set is that of the command-line.
func ParseConfigFile ¶
Parse the given config fiĺe into the value pointed to by cfg. Returns error regardless of error handling scheme.
If cfg is not a pointer, ParseConfigFile returns an ErrNotAPointer.
The 'filename' must either be an absolute path to the config file, exist in the current working directory, or in one of the directories given as 'dirs'. If the given file cannot be found, ParseConfig file returns an ErrNoConfigFileToParse.
func ParseDefaultConfigFile ¶
func ParseDefaultConfigFile(cfg interface{}) (err error)
Parse the default config fiĺe into the value pointed to by cfg. Returns error regardless of error handling mode.
If cfg is not a pointer, ParseDefaultConfigFile returns an ErrNotAPointer.
func ParseFlags ¶
func ParseFlags() error
Parses flags, stores all default flags in a list, and all parsed flags in another.
func SetDefaultFile ¶
Set default file. fpath must be absolute path. If the file cannot be opened, the function will return an error. Note that the error will only be return if the error handling mode is set to ContinueOnError, else the function will Panic or Exit depending on the mode.
func SetEnvPrefix ¶
func SetEnvPrefix(prefix string)
Set a prefix to use for all environmental variables.
For example to different between what is used in testing and otherwise, the prefix "TEST_" could be used. The environmental variables TEST_timeout and TEST_angle would then map to the properties 'timeout' and 'angle'.
func SetEnvsToParse ¶
Set a list of environmental variable names to check when filling out the configuration struct.
The list can consist of variables both containing a set env prefix and not, but the environmental variable that is looked for will be that with the prefix. That is, if the prefix is set as TEST_ and the list envVarNames is ["timeout", "TEST_angle"], the environmental variables that will be looked for are ["TEST_timeout", "TEST_angle"].
If the environmental variable(s) cannot be find, SetEnvsToParse will return an error containing all the names of the non-existant variables. Note that the error will only be return if the error handling mode is set to ContinueOnError, else the function will Panic or Exit depending on the mode.
func SetFlagDefault ¶
Set, or reset, the default value of a flag.
func SetFlagSetArgs ¶
func SetFlagSetArgs(args []string)
Set a list of flags to parse. As default, args is set os os.Args[1:], the command-line args.
SetFlagSetArgs is particularly useful for testing.
func SetUpConfiguration ¶
func SetUpConfiguration(cfg interface{}) (err error)
Parse all the sources (flags, env vars, default config file) and store the result in the value pointer to by cfg.
If cfg is not a pointer, SetUpConfiguration returns an ErrNotAPointer.
func SetUpConfigurationWithConfigFile ¶
Parse all the sources (flags, env vars, given config file, default config file) and store the result in the value pointer to by cfg.
If cfg is not a pointer, SetUpConfigurationWithConfigFile returns an ErrNotAPointer.
The 'filename' must either be an absolute path to the config file, exist in the current working directory, or in one of the directories given as 'dirs'. If the given file cannot be found, the other sources will still be parsed, but an ErrNoConfigFileToParse will be returned.
func String ¶ added in v0.0.5
func String(c interface{}) string
Creates a string given a ptr to a struct.
Example:
type Person struct { Name string Age int } func printMio() { mio := &Person{Name: "Mio", Age: 9} fmt.Println(String(mio)) }
output:
name: Mio age: 9
func StringIgnoreZeroValues ¶ added in v0.0.5
func StringIgnoreZeroValues(c interface{}) string
Same as String(), except ignores zero values e.g. empty strings and zeroes
func Usage ¶ added in v0.0.5
func Usage()
Usage prints a usage message documenting all defined command-line flags to the set FlagSet's output, which by default is os.Stderr. It is based on the standard flag package's PrintDefaults() but includes two more default flags in addition to 'help': write-def-conf (write to default config file) and print-conf (print current configuration to stdout).
Usage is called when an error occurs while parsing flags.
Types ¶
type ErrorHandling ¶ added in v0.1.0
type ErrorHandling flag.ErrorHandling
Aliasing flag.Errorhandling for clarity and easy of use.
type FlagValue ¶
FlagValue is a wrapper for flag.Value, which stores the dynamic value of a flag, and information on if the flag has been parsed.
type FlagValueBool ¶ added in v0.0.4
type FlagValueBool struct {
FlagValue
}
Implements flag package's boolFlag interface. Special case to enable using bool flags like switches.
func (*FlagValueBool) IsBoolFlag ¶ added in v0.0.4
func (fvb *FlagValueBool) IsBoolFlag() bool
Intercepts the standard flag package's IsBoolFlag().