flag

package
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: 4 Imported by: 0

Documentation

Overview

Package flag loads configuration from flags defined by flag.

Flag loads flags in flag.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 Flag

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

Flag is a Provider that loads configuration from flags.

To create a new Flag, call New.

func New

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

New creates a Flag 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 (Flag) Load

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

func (Flag) String

func (f Flag) String() string

type Option

type Option func(*options)

Option configures the a Flag with specific options.

func WithFlagSet

func WithFlagSet(set *flag.FlagSet) Option

WithFlagSet provides the flag.FlagSet that loads configuration from.

The default flag set is 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.

Jump to

Keyboard shortcuts

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