ff: github.com/peterbourgon/ff Index | Examples | Files | Directories

package ff

import "github.com/peterbourgon/ff"

Index

Examples

Package Files

flag_set.go source.go value.go variable.go

Variables

var ErrHelp = errors.New("help requested")

ErrHelp is returned by Parse when the user passed -h.

type DurationValue Uses

type DurationValue time.Duration

DurationValue wraps time.Duration and implements the Value interface.

func (*DurationValue) Get Uses

func (v *DurationValue) Get() interface{}

Get implements Value.

func (*DurationValue) Set Uses

func (v *DurationValue) Set(s string) error

Set implements Value.

func (*DurationValue) String Uses

func (v *DurationValue) String() string

String implements Value.

type FlagSet Uses

type FlagSet struct {
    // Usage prints help text to stdout.
    Usage func()
    // contains filtered or unexported fields
}

FlagSet is a collection of variables.

func NewFlagSet Uses

func NewFlagSet(short string) *FlagSet

NewFlagSet returns an empty set of variables. The short help text will be displayed with -h.

func (*FlagSet) Duration Uses

func (fs *FlagSet) Duration(name string, def time.Duration, usage string, options ...VariableOption) *time.Duration

Duration allocates and returns a time.Duration variable that's been installed to the flag set.

func (*FlagSet) DurationVar Uses

func (fs *FlagSet) DurationVar(p *time.Duration, name string, def time.Duration, usage string, options ...VariableOption)

DurationVar installs a remote time.Duration variable to the flag set.

func (*FlagSet) Parse Uses

func (fs *FlagSet) Parse(args []string, otherSources ...Source) error

Parse defined variables from commandline flags, and optionally other sources, in decreasing order of priority. If error is non-nil, the set must be considered invalid.

Code:

os.Setenv("EXAMPLE_FOO", "foo_from_env")
os.Setenv("EXAMPLE_BAR", "2s")
os.Setenv("EXAMPLE_BAZ", "baz_from_env")
args := []string{"--foo=xyz", "-bar", "3s"}

fs := ff.NewFlagSet("example [flags]")
var (
    foo = fs.String("foo", "default_value", "a string variable", ff.Env("EXAMPLE_FOO"))
    bar = fs.Duration("bar", time.Second, "a duration variable", ff.Env("EXAMPLE_BAR"))
    baz = fs.String("baz", "", "another string variable", ff.Env("EXAMPLE_BAZ"))
)
if err := fs.Parse(args, ff.FromEnvironment("EXAMPLE_")); err != nil {
    panic(err)
}

fmt.Println("foo", *foo)
fmt.Println("bar", bar.String())
fmt.Println("baz", *baz)

Output:

foo xyz
bar 3s
baz baz_from_env

func (*FlagSet) String Uses

func (fs *FlagSet) String(name string, def string, usage string, options ...VariableOption) *string

String allocates and returns a string variable that's been installed to the flag set.

func (*FlagSet) StringVar Uses

func (fs *FlagSet) StringVar(p *string, name string, def string, usage string, options ...VariableOption)

StringVar installs a remote string variable to the flag set.

func (*FlagSet) Var Uses

func (fs *FlagSet) Var(value Value, name, def, usage string, options ...VariableOption)

Var installs a variable of any concrete type to the flag set.

type Source Uses

type Source interface {
    Type() SourceType
    Fetch(*FlagSet) map[string]string
}

Source from which variable values may be retrieved.

func FromEnvironment Uses

func FromEnvironment(prefix string) Source

FromEnvironment returns a source that takes values from environment variables with the given prefix. The prefix is not stripped from the selected vars.

func FromJSONFile Uses

func FromJSONFile(filename string) Source

FromJSONFile returns a source that takes values from a JSON object encoded in the file. The file must contain a single JSON object, with keys at the top level, and values as strings.

func FromJSONFileVia Uses

func FromJSONFileVia(configFileFlagName string) Source

FromJSONFileVia returns a parse source that takes values from a JSON object encoded in a file referenced by the passed variable name. The file must contain a single JSON object, with keys at the top level, and values as strings.

func FromJSONReader Uses

func FromJSONReader(r io.Reader) Source

FromJSONReader returns a parse source that unmarshals a map[string]string from the provided reader. The reader must contain a single JSON object, with keys at the top level, and values as strings.

type SourceType Uses

type SourceType uint8

SourceType enumerates the sources from which we can retrieve values.

const (
    // SourceTypeEnv takes variables from the environment.
    SourceTypeEnv SourceType = iota

    // SourceTypeJSON takes variables from a JSON object serialized to a file.
    SourceTypeJSON
)

type StringValue Uses

type StringValue string

StringValue wraps string and implements the Value interface.

func (*StringValue) Get Uses

func (v *StringValue) Get() interface{}

Get implements Value.

func (*StringValue) Set Uses

func (v *StringValue) Set(val string) error

Set implements Value.

func (*StringValue) String Uses

func (v *StringValue) String() string

String implements Value.

type Value Uses

type Value interface {
    Set(string) error
    Get() interface{}
    String() string
}

Value represents a single configuration parameter. It must be implemented by each supported, concrete type.

type Variable Uses

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

Variable is the internal representation of an individual variable, wrapping a Value and other per-variable metadata.

type VariableOption Uses

type VariableOption func(*Variable)

VariableOption sets a per-variable option.

func Env Uses

func Env(name string) VariableOption

Env instructs the package to take a value from the environment with the given env var name.

func JSON Uses

func JSON(key string) VariableOption

JSON instructs the variable to take a value from a JSON config object with the given key.

Directories

PathSynopsis
ffexample

Package ff imports 8 packages (graph) and is imported by 1 packages. Updated 2017-09-01. Refresh now. Tools for package owners.