posflag

package
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Jan 19, 2023 License: MIT Imports: 4 Imported by: 104

Documentation

Overview

Package posflag implements a koanf.Provider that reads commandline parameters as conf maps using spf13/pflag, a POSIX compliant alternative to Go's stdlib flag package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FlagVal added in v1.3.2

func FlagVal(fs *pflag.FlagSet, f *pflag.Flag) interface{}

FlagVal examines a pflag.Flag and returns a typed value as an interface{} from the types that pflag supports. If it is of a type that isn't known for any reason, the value is returned as a string.

Types

type Posflag

type Posflag struct {
	// contains filtered or unexported fields
}

Posflag implements a pflag command line provider.

func Provider

func Provider(f *pflag.FlagSet, delim string, ko *koanf.Koanf) *Posflag

Provider returns a commandline flags provider that returns a nested map[string]interface{} of environment variable where the nesting hierarchy of keys are defined by delim. For instance, the delim "." will convert the key `parent.child.key: 1` to `{parent: {child: {key: 1}}}`.

It takes an optional (but recommended) Koanf instance to see if the the flags defined have been set from other providers, for instance, a config file. If they are not, then the default values of the flags are merged. If they do exist, the flag values are not merged but only the values that have been explicitly set in the command line are merged.

func ProviderWithFlag added in v1.3.2

func ProviderWithFlag(f *pflag.FlagSet, delim string, ko *koanf.Koanf, cb func(f *pflag.Flag) (string, interface{})) *Posflag

ProviderWithFlag takes pflag.FlagSet and a callback that takes *pflag.Flag and applies the callback to all items in the flagset. It does not parse pflag.Flag values and expects the callback to process the keys and values from *pflag.Flag however. FlagVal() can be used in the callbakc to avoid repeating the type-switch block for parsing values. Returning "" for the key causes the particular flag to be disregarded.

Example:

p := posflag.ProviderWithFlag(flagset, ".", ko, func(f *pflag.Flag) (string, interface{}) {
   // Transform the key in whatever manner.
   key := f.Name

   // Use FlagVal() and then transform the value, or don't use it at all
   // and add custom logic to parse the value.
   val := posflag.FlagVal(flagset, f)

   return key, val
})

func ProviderWithValue added in v0.16.0

func ProviderWithValue(f *pflag.FlagSet, delim string, ko *koanf.Koanf, cb func(key string, value string) (string, interface{})) *Posflag

ProviderWithValue works exactly the same as Provider except the callback takes a (key, value) with the variable name and value and allows their modification. This is useful for cases where complex types like slices separated by custom separators. Returning "" for the key causes the particular flag to be disregarded.

func (*Posflag) Read

func (p *Posflag) Read() (map[string]interface{}, error)

Read reads the flag variables and returns a nested conf map.

func (*Posflag) ReadBytes

func (p *Posflag) ReadBytes() ([]byte, error)

ReadBytes is not supported by the pflag provider.

func (*Posflag) Watch added in v0.5.0

func (p *Posflag) Watch(cb func(event interface{}, err error)) error

Watch is not supported.

Jump to

Keyboard shortcuts

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