basicflag

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2023 License: MIT Imports: 3 Imported by: 9

Documentation

Overview

Package basicflag implements a koanf.Provider that reads commandline parameters as conf maps using the Go's flag package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type KoanfIntf added in v1.0.0

type KoanfIntf interface {
	Exists(string) bool
}

KoanfIntf is an interface that represents a small subset of methods used by this package from Koanf{}.

type Opt added in v1.0.0

type Opt struct {
	KeyMap KoanfIntf
}

Opt represents optional options (yup) passed to the provider.

type Pflag

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

Pflag implements a pflag command line provider.

func Provider

func Provider(f *flag.FlagSet, delim string, opt ...*Opt) *Pflag

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) Opt{} argument containing a Koanf instance. It checks if the defined flags have been set by other providers (e.g., a config file). If not, default flag values are merged. If they exist, flag values are merged only if explicitly set in the command line. The function is variadic to maintain backward compatibility. See https://github.com/knadh/koanf/issues/255

func ProviderWithValue

func ProviderWithValue(f *flag.FlagSet, delim string, cb func(key string, value string) (string, interface{}), ko ...KoanfIntf) *Pflag

ProviderWithValue works exactly the same as Provider except the callback takes a (key, value) with the variable name and value and allows you to modify both. This is useful for cases where you may want to return other types like a string slice instead of just a string.

It takes an optional Opt{} (but recommended) argument with a Koanf instance (opt.KeyMap) 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. It is a variadic function as a hack to ensure backwards compatibility with the function definition. See https://github.com/knadh/koanf/issues/255

func (*Pflag) Read

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

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

func (*Pflag) ReadBytes

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

ReadBytes is not supported by the basicflag koanf.

Jump to

Keyboard shortcuts

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