configfile

package module
v0.0.0-...-8062233 Latest Latest
Warning

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

Go to latest
Published: Apr 18, 2023 License: LGPL-3.0 Imports: 6 Imported by: 0

README

configfile - An easy config file parser to be integrated with Go's flag package

You simply created command line parameter parsing with Go's flag package, but you need to parse a config file too? This package is for you.

Examples

See the self documented examples.

I recommend to use your favorite diff to see how the examples evolove.

The first example is the easiest, just add 2 lines of code to parse a configuration file. Try it with:

go run ....

Example 2 defines a reduced set of parameters to be parsed from the configuration file. Additionally an extra handler function for invalid parameters is used.

go run ./example2.go --password=monkey -automatic=false

In example 3 the filename for the configuration file is read from a parameter. To do so an extra flagset is used. With configfile you just have to add one line of code. Try it out with.

go run ./example3.go -config example3.conf -password=monkey -automatic=false

Licenses

This is free documentation and software.

The configfile library itself is licensed under the terms of the GNU Lesser General Public License. https://www.gnu.org/licenses/lgpl-3.0.html

This documentation and the examples are licensed under a Creative Commons Attribution 4.0 International License. http://creativecommons.org/licenses/by/4.0/

configfile is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

SPDX-License-Identifier: CC-BY-4.0

EOF

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func FileToArgs

func FileToArgs(filename string) ([]string, error)

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 FlagSetHelp

func FlagSetHelp()

FlagSetHelp implements the default usage output.

func GetFirstParameter

func GetFirstParameter(args []string, parameter, defaultValue string) (string, error)

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

func InvalidIgnore(line int, param string)

InvalidIgnore is a function which can be used as the invalid function in FileToArgsValid() parameter invalid. It ignores invalid parameters.

func InvalidToStderr

func InvalidToStderr(line int, param string)

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

func SetFlagSetDefaultHelp(fs *flag.FlagSet)

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.

Jump to

Keyboard shortcuts

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