pflag

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: May 2, 2024 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package pflag loads configuration from flags defined by spf13/pflag.

PFlag loads flags in pflag.CommandLine whose names starts with the given prefix and returns them as a nested map[string]any. The unchanged flags with zero default value are skipped to avoid overriding values set by other loader.

It splits the names by delimiter. For example, with the default delimiter ".", the flag `parent.child.key="1"` is loaded as `{parent: {child: {key: "1"}}}`.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Option

type Option func(*options)

Option configures the a PFlag with specific options.

func WithFlagSet

func WithFlagSet(set *pflag.FlagSet) Option

WithFlagSet provides the pflag.FlagSet that loads configuration from.

The default flag set is pflag.CommandLine plus flag.CommandLine.

func WithNameSplitter added in v0.5.0

func WithNameSplitter(splitter func(string) []string) Option

WithNameSplitter provides the function used to split flag names into nested keys. If it returns an nil/[]string{}/[]string{""}, the variable will be ignored.

For example, with the default splitter, an flag name like "parent.child.key" would be split into "parent", "child", and "key".

func WithPrefix

func WithPrefix(prefix string) Option

WithPrefix provides the prefix used when loading flags. Only flags with names that start with the prefix will be loaded.

For example, if the prefix is "server", only flags whose names start with "server" will be loaded. By default, it has no prefix which loads all flags.

type PFlag

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

PFlag is a Provider that loads configuration from flags defined by spf13/pflag.

To create a new PFlag, call New.

func New

func New(konf konf, opts ...Option) PFlag

New creates a PFlag with the given Option(s).

The first parameter is the konf Config instance that checks if the defined flags have been set by other providers. If not, default flag values are merged. If they exist, flag values are merged only if explicitly set in the command line.

func (PFlag) Load

func (f PFlag) Load() (map[string]any, error)

func (PFlag) String

func (f PFlag) String() string

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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