flags

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: May 2, 2022 License: MIT Imports: 3 Imported by: 1

README

Go flags

Go Coverage Status Go Report Card

Configuration package inspired by this talk / article by Peter Bourgon.

The guiding idea behind this package is that flags are the best way to configure your program and thus it provides a thin wrapper around go standard flag package while providig an extra degree of configurability via different extendable / composable mechanisms such as environment variables, config files etc... on an opt-in basis.

Two of these mechanisms are provided by default env for environment variables and json for flat json config giles. Others resolvers can be implemented additionaly.

Example Usage

As (almost) drop-in replacement for flag package:

var fs flags.FlagSet

var (
	host     = fs.String("host", "DB host", "localhost")
	username = fs.String("username", "DB username", "root")
	port     = fs.Int("port", "DB port", 3306)
)

fs.Parse(os.Args)

Though where it really shines is when you use it in combination with different config mechanisms such as env flags or json config files:

var fs flags.FlagSet

var (
	host     = fs.String("host", "DB host", "localhost")
	username = fs.String("username", "DB username", "root", json.ByName(), env.Named("UNAME"))
	port     = fs.Int("port", "DB port", 3306, json.ByName(), env.ByName())
	cfg      = fs.String("config", "JSON Config file", "config.json", env.ByName())
)

fs.Parse(
	os.Args,
	env.WithPrefix("MYAPP_"), // Optional prefix
	json.WithConfigFile(cfg), // Path to config file
)

As you can see, flag definitions accept additional arguments where you can set different mechanisms that will be evaluated in a sequence and the first one that yields a valid value will be used.

For example here:

username = fs.String("username", "DB username", "root", json.ByName(), env.Named("UNAME"))

If no -username flag is provided, the package would next try to find the value as a json key in the provided config file (see below), if the value is not found it will try to read the environment variable UNAME. You can put as many resolvers as you want (even of the same kind).

NOTE If flag is provided explicitly on the command line it wil always take a precedence regardless if value exists in environment or config file for example.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BoolValue

type BoolValue struct {
	Value
	V *bool
}

BoolValue flag type.

func (*BoolValue) Set

func (val *BoolValue) Set(v interface{}) error

Set converts and sets a value

type FlagSet

type FlagSet struct {
	Values []interface{}
	Config map[string]interface{}
	// contains filtered or unexported fields
}

FlagSet represents flags container.

func (*FlagSet) Bool

func (fs *FlagSet) Bool(name, usage string, val bool, r ...ResolverFunc) *bool

Bool creates new Bool flag. Accepts a list of additional resolvers that are evaluated in sequence and the first one to yield a valid value is chosen. If no resolver yileds a valid value the default flag value is used. If flag is provided as a cli arg it will take precedence over all resolvers and the default value.

func (*FlagSet) Float64

func (fs *FlagSet) Float64(name, usage string, val float64, r ...ResolverFunc) *float64

Float64 creates new Float64 flag. Accepts a list of additional resolvers that are evaluated in sequence and the first one to yield a valid value is chosen. If no resolver yileds a valid value the default flag value is used. If flag is provided as a cli arg it will take precedence over all resolvers and the default value.

func (*FlagSet) Int

func (fs *FlagSet) Int(name, usage string, val int, r ...ResolverFunc) *int

Int creates new Int flag. Accepts a list of additional resolvers that are evaluated in sequence and the first one to yield a valid value is chosen. If no resolver yileds a valid value the default flag value is used. If flag is provided as a cli arg it will take precedence over all resolvers and the default value.

func (*FlagSet) Int64

func (fs *FlagSet) Int64(name, usage string, val int64, r ...ResolverFunc) *int64

Int64 creates new Int64 flag. Accepts a list of additional resolvers that are evaluated in sequence and the first one to yield a valid value is chosen. If no resolver yileds a valid value the default flag value is used. If flag is provided as a cli arg it will take precedence over all resolvers and the default value.

func (*FlagSet) Parse

func (fs *FlagSet) Parse(args []string, opts ...FlagSetOption)

Parse parses command line flags and runs all additional resolvers for each flag in sequence. If command line flag is set it takes precedence over all other resolvers which are skipped in that case.

func (*FlagSet) Set

func (fs *FlagSet) Set(i int, v interface{}, t interface{}) error

Set sets a flag value for flag at i index

func (*FlagSet) String

func (fs *FlagSet) String(name, usage string, val string, r ...ResolverFunc) *string

String creates new String flag. Accepts a list of additional resolvers that are evaluated in sequence and the first one to yield a valid value is chosen. If no resolver yileds a valid value the default flag value is used. If flag is provided as a cli arg it will take precedence over all resolvers and the default value.

func (*FlagSet) Uint

func (fs *FlagSet) Uint(name, usage string, val uint, r ...ResolverFunc) *uint

Uint creates new Uint flag. Accepts a list of additional resolvers that are evaluated in sequence and the first one to yield a valid value is chosen. If no resolver yileds a valid value the default flag value is used. If flag is provided as a cli arg it will take precedence over all resolvers and the default value.

func (*FlagSet) Uint64

func (fs *FlagSet) Uint64(name, usage string, val uint64, r ...ResolverFunc) *uint64

Uint64 creates new Uint64 flag. Accepts a list of additional resolvers that are evaluated in sequence and the first one to yield a valid value is chosen. If no resolver yileds a valid value the default flag value is used. If flag is provided as a cli arg it will take precedence over all resolvers and the default value.

type FlagSetOption

type FlagSetOption func(*FlagSet)

FlagSetOption func mainly used for extending FlagSet configuration for different types of resolvers.

type Float64Value

type Float64Value struct {
	Value
	V *float64
}

Float64Value flag type.

func (*Float64Value) Set

func (val *Float64Value) Set(v interface{}) error

Set converts and sets a value

type Int64Value

type Int64Value struct {
	Value
	V *int64
}

Int64Value flag type.

func (*Int64Value) Set

func (val *Int64Value) Set(v interface{}) error

Set converts and sets a value

type IntValue

type IntValue struct {
	Value
	V *int
}

IntValue flag type.

func (*IntValue) Set

func (val *IntValue) Set(v interface{}) error

Set converts and sets a value

type ResolverFunc

type ResolverFunc func(*FlagSet, string, interface{}, int) bool

ResolverFunc represents a resolver for a primitive flag type. Used for implementing additional config mechanisms (see env/ and json/).

type StringValue

type StringValue struct {
	Value
	V *string
}

StringValue flag type.

func (*StringValue) Set

func (val *StringValue) Set(v interface{}) error

Set converts and sets a value

type Uint64Value

type Uint64Value struct {
	Value
	V *uint64
}

Uint64Value flag type.

func (*Uint64Value) Set

func (val *Uint64Value) Set(v interface{}) error

Set converts and sets a value

type UintValue

type UintValue struct {
	Value
	V *uint
}

UintValue flag type.

func (*UintValue) Set

func (val *UintValue) Set(v interface{}) error

Set converts and sets a value

type Value

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

Value represents a config value

Directories

Path Synopsis
cmd
gen

Jump to

Keyboard shortcuts

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