Documentation ¶
Overview ¶
Package configfile provides a simple config file parser which is integrated with Go's flag package.
The config file is a simple text file with one parameter per line. The parameter is separated from the value by a colon, an equal sign or a space. The parameter is case sensitive. The value is trimmed of leading and trailing spaces. Empty lines and lines starting with a hash (#) are ignored.
The simplest usage (ommiting error handling) is:
configFileArgs, err := configfile.FileToArgs("example.conf") myArgs := append(configFileArgs, os.Args[1:]...) err := myFlagSet.Parse(myArgs)
Check out more features the example directory in the source code repository https://git.sr.ht/~kulbartsch/configfile
Index ¶
- func FileToArgs(filename string) ([]string, error)
- func FileToArgsValid(filename string, valid StringMapEmpty, invalid func(line int, param string)) ([]string, error)
- func FlagSetHelp()
- func GetFirstParameter(args []string, parameter, defaultValue string) (string, error)
- func InvalidIgnore(line int, param string)
- func InvalidToStderr(line int, param string)
- func SetFlagSetDefaultHelp(fs *flag.FlagSet)
- type StringMapEmpty
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func FileToArgs ¶
FileToArgs reads a config file with the filename and returns a slice of strings which can be used as arguments for flag.Parse().
func FileToArgsValid ¶
func FileToArgsValid(filename string, valid StringMapEmpty, invalid func(line int, param string)) ([]string, error)
FileToArgsValid reads a config file with the filename and returns a slice of strings which can be used as arguments for flag.Parse(). The valid parameter is a StringMapEmpty with all valid parameters as keys. The invalid function is called for every invalid parameter not in the valid parameter.
func GetFirstParameter ¶
GetFirstParameter returns the parameter of the args slice if it matchs. If the parameter is not found, the defaultValue is returned. (It must be the first parameter, because any other parameter will cancel the flag.Parse() parsing.)
func InvalidIgnore ¶
InvalidIgnore is a function which can be used as the invalid function in FileToArgsValid() parameter invalid. It ignores invalid parameters.
func InvalidToStderr ¶
InvalidIgnore is a function which can be used as the invalid function in FileToArgsValid() parameter invalid. It prints invalid parameters to os.Stderr.
func SetFlagSetDefaultHelp ¶
SetFlagSetDefaultHelp sets the FlagSet.Usage for fs to FlagSetHelp, which behaves like the default, but if a self defined flag set is used, the usage function for the help text must be set explicitly.
Types ¶
type StringMapEmpty ¶
type StringMapEmpty map[string]struct{}
StringMapEmpty is a map of strings with an empty struct as value. It is used to handle a list of strings, but as a map it has build in functions for lookup and deletion.
func FlagSetToValidArgs ¶
func FlagSetToValidArgs(f *flag.FlagSet) StringMapEmpty
FlagSetToValidArgs returns a StringMapEmpty with all parameters of the flag.FlagSet f as keys. This can be used as the valid parameter in FileToArgsValid().
With delete(validParams, flagname) you can delete unwanted parameters.