flag

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Jun 15, 2019 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package flag defines a config.Loader type that allows laoding values from command line arguments using the language's flag package.

We import the flag package but give it the name flaglib to avoid confusion. References to flaglib throughout the documentation refer to the standard "flag" package.

Example
loader := New("")

boolVar := false

loader.FlagSet.String("s", "", "")
loader.FlagSet.Int("i", 0, "")
loader.FlagSet.BoolVar(&boolVar, "b", false, "")
loader.FlagSet.Float64("f", 0, "")
loader.FlagSet.String("withdefault", "default", "")
loader.FlagSet.String("one-two-three", "", "")
loader.FlagSet.String("aliased", "", "")

loader.AddAlias("aliased", "a")

//This means that "withdefault" -> "default" will be inserted in the resulting Config.
//Leaving this blank would not insert that association because the -withdefault
//flag is not set below.
loader.LoadDefaults = true

loader.Args = []string{
	"-s", "sValue",
	"-i", "12",
	"-b=true",
	"-f", "1.2",
	//notice no withdefault flag set
	"-one-two-three", "three",
	"-aliased", "aValue",
}

c := config.New()

_, err := c.MergeLoaders(loader)
if err != nil {
	fmt.Println(err)
	return
}

fmt.Println(c.GetStringOk("s"))
fmt.Println(c.GetInt64Ok("i"))
fmt.Println(c.GetBoolOk("b"))
fmt.Println(c.GetFloat64Ok("f"))
fmt.Println(c.GetStringOk("withdefault"))
fmt.Println(c.GetStringOk("one.two.three"))
fmt.Println(c.GetStringOk("a")) //notice the use of the "a" key - aliased above.
Output:

sValue true
12 true
true true
1.2 true
default true
three true
aValue true

Index

Examples

Constants

View Source
const DashSeparatorKeyParser = config.SeparatorKeyParser("-")

DashSeparatorKeyParser is the default KeyParser for a Loader (set in New()).

Variables

This section is empty.

Functions

This section is empty.

Types

type Loader

type Loader struct {
	//Args is the slice of strings sent to flaglib.FlagSet.Parse().
	//It is set to os.Args[1:] by New().
	Args []string

	//Aliases provides a way to change the name of a command line flag before it
	//gets parsed by KeyParser and inserted into the resulting Values instance.
	//If a flaglib.Flag's Name is a key in this map, then the associated value
	//will be used to create the key instead.
	Aliases map[string]string

	//KeyParser is used to turn a flaglib.Flag's Name (or alias from Aliases)
	//into a Key for insertion into the resulting Values.
	KeyParser config.KeyParser

	//LoadDefaults tells Loader to include in the resulting Values all flags that
	//are defined even if they are not set.
	//The default operation is the to only insert flags that have been set explicitly
	//in Args.
	LoadDefaults bool

	//FlagSet is the provided type for managing arguments from the flaglib package.
	//Work with this instance directly to add flags you want parsed and inserted
	//into the resulting Values.
	FlagSet *flaglib.FlagSet
}

Loader provides settings to load values from command line arguments (or any slice of strings). Loader implements config.Loader.

func New

func New(name string) *Loader

New creates a *Loader with Args set to os.Args[1:], Aliases set to a new map, KeyParser set to DashSeparatorKeyParser, LoadDefaults set to false, and FlagSet set to flaglib.NewFlagSet(name, flaglib.ContinueOnError).

func (*Loader) AddAlias

func (l *Loader) AddAlias(name, alias string) *Loader

AddAlias inserts name, alias into l.Aliases.

func (*Loader) Load

func (l *Loader) Load() (*config.Values, error)

Load is the config.Loader required method. It calls l.FlagSet.Parse(l.Args) if l.FlagSet.Parsed() is false. It then calls one of the flaglib.FlagSet.Visit*() methods depending on the value of l.LoadDefaults, and parses each flag's Name or alias and inserts it into the returned Values.

Jump to

Keyboard shortcuts

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