Documentation ¶
Overview ¶
Package opt provides common properties for custom types and support for the options pattern.
Opt was created with the intent of promoting a pattern for handling optional parameters to functions. By making a parameter optional we effectively provide a third value of unset for a given param which other patterns don't have. For instance by passing in a struct as options, you get the value and Go's default if not set, but you don't know if the not set value was intentional where as with an option not existing you know the user intended to not set that option. This is an important distinction that the struct based option pattern does not have.
Options Pattern:
Packages making use of the variadic options pattern should ignore options that are not supported and clearly call out in function comments which options are supported.
Create helper functions that wrap your constants to allow the compiler to assist. The creation functions should be called <option>Opt, checkers <option>OptExists, getters Get<option>Opt and default helper Default<option>Opt. e.g. DebugOpt, DebugOptExists, GetDebugOpt and DefaultDebugOpt
Common Properties:
To support the options pattern I've included some common reusable properties that custom types can inherit from and their supporting option helper functions.
Index ¶
- Variables
- func Add(opts *[]*Opt, o ...*Opt) (result bool)
- func DebugOptExists(opts []*Opt) bool
- func Default(opts *[]*Opt, o ...*Opt) (result bool)
- func DefaultDebugOpt(opts []*Opt, val bool) bool
- func DefaultDryrunOpt(opts []*Opt, val bool) bool
- func DefaultErrOpt(opts []*Opt, val io.Writer) io.Writer
- func DefaultHomeOpt(opts []*Opt, val string) string
- func DefaultInOpt(opts []*Opt, val io.Reader) io.Reader
- func DefaultOutOpt(opts []*Opt, val io.Writer) io.Writer
- func DefaultQuietOpt(opts []*Opt, val bool) bool
- func DefaultTestingOpt(opts []*Opt, val bool) bool
- func DryrunOptExists(opts []*Opt) bool
- func ErrOptExists(opts []*Opt) bool
- func Exists(opts []*Opt, key string) bool
- func GetBool(opts []*Opt, key string) (result bool)
- func GetDebugOpt(opts []*Opt) (result bool)
- func GetDryrunOpt(opts []*Opt) (result bool)
- func GetErrOpt(opts []*Opt) io.Writer
- func GetHomeOpt(opts []*Opt) string
- func GetInOpt(opts []*Opt) io.Reader
- func GetOutOpt(opts []*Opt) io.Writer
- func GetQuietOpt(opts []*Opt) (result bool)
- func GetString(opts []*Opt, key string) (result string)
- func GetTestingOpt(opts []*Opt) (result bool)
- func HomeOptExists(opts []*Opt) bool
- func InOptExists(opts []*Opt) bool
- func OutOptExists(opts []*Opt) bool
- func OverwriteDebugOpt(opts *[]*Opt, val bool) bool
- func OverwriteDryrunOpt(opts *[]*Opt, val bool) bool
- func OverwriteErrOpt(opts *[]*Opt, val io.Writer) io.Writer
- func OverwriteHomeOpt(opts *[]*Opt, val string) string
- func OverwriteInOpt(opts *[]*Opt, val io.Reader) io.Reader
- func OverwriteOutOpt(opts *[]*Opt, val io.Writer) io.Writer
- func OverwriteQuietOpt(opts *[]*Opt, val bool) bool
- func OverwriteTestingOpt(opts *[]*Opt, val bool) bool
- func QuietOptExists(opts []*Opt) bool
- func Remove(opts *[]*Opt, key string)
- func TestingOptExists(opts []*Opt) bool
- type Opt
- func Copy(opts []*Opt) []*Opt
- func DebugOpt(val bool) *Opt
- func DryrunOpt(val bool) *Opt
- func ErrOpt(val io.Writer) *Opt
- func Get(opts []*Opt, key string) *Opt
- func HomeOpt(val string) *Opt
- func InOpt(val io.Reader) *Opt
- func New(key string, val interface{}) *Opt
- func OutOpt(val io.Writer) *Opt
- func Overwrite(opts *[]*Opt, opt *Opt) *Opt
- func QuietOpt(val bool) *Opt
- func TestingOpt(val bool) *Opt
- type Opts
- func (x *Opts) Add(o ...*Opt) (result bool)
- func (x *Opts) Copy() []*Opt
- func (x *Opts) Default(o ...*Opt) (result bool)
- func (x *Opts) Exists(key string) bool
- func (x *Opts) Get(key string) *Opt
- func (x *Opts) GetBool(key string) (result bool)
- func (x *Opts) GetString(key string) (result string)
- func (x *Opts) Len() int
- func (x *Opts) Overwrite(opt *Opt) *Opt
- func (x *Opts) Remove(key string)
- type Std
- type StdProps
- type StdStream
- type StdStreamProps
Constants ¶
This section is empty.
Variables ¶
var ( InOptKey = "in" OutOptKey = "out" ErrOptKey = "err" HomeOptKey = "home" QuietOptKey = "quiet" DebugOptKey = "debug" DryrunOptKey = "dry-run" TestingOptKey = "testing" )
Common options keys
Functions ¶
func Add ¶
Add an option to the options slice if it doesn't exist. Just an alias to Default, but the alternate naming is more intuitive in different scenarios.
func DebugOptExists ¶
DebugOptExists determines if the option exists in the given options
func Default ¶
Default adds an option to the options slice if it doesn't exist. Returns true the option was added to the options slice or false if the given slice or option are nil or the option already exists in the slice.
func DefaultDebugOpt ¶
DefaultDebugOpt sets the default value for the option if it doesn't exist already. Use this when the Get's default is not desirable.
func DefaultDryrunOpt ¶ added in v1.1.43
DefaultDryrunOpt sets the default value for the option if it doesn't exist already. Use this when the Get's default is not desirable.
func DefaultErrOpt ¶
DefaultErrOpt sets the default value for the option if it doesn't exist already. Use this when the Get's default is not desirable.
func DefaultHomeOpt ¶
DefaultHomeOpt sets the default value for the option if it doesn't exist already. Use this when the Get's default is not desirable.
func DefaultInOpt ¶
DefaultInOpt sets the default value for the option if it doesn't exist already. Use this when the Get's default is not desirable.
func DefaultOutOpt ¶
DefaultOutOpt sets the default value for the option if it doesn't exist already. Use this when the Get's default is not desirable.
func DefaultQuietOpt ¶
DefaultQuietOpt sets the default value for the option if it doesn't exist already. Use this when the Get's default is not desirable.
func DefaultTestingOpt ¶
DefaultTestingOpt returns the option value if found else the one given Use this when the Get's default is not desirable.
func DryrunOptExists ¶ added in v1.1.43
DryrunOptExists determines if the option exists in the given options
func ErrOptExists ¶
ErrOptExists determines if the option exists in the given options
func GetDebugOpt ¶
GetDebugOpt finds and returns the option's value or defaults to false
func GetDryrunOpt ¶ added in v1.1.43
GetDryrunOpt finds and returns the option's value or defaults to false
func GetHomeOpt ¶
GetHomeOpt finds and returns the option's value or defaults to empty string
func GetQuietOpt ¶
GetQuietOpt finds and returns the option's value or defaults to false
func GetTestingOpt ¶
GetTestingOpt finds and returns the option's value or defaults to false
func HomeOptExists ¶
HomeOptExists determines if the option exists in the given options
func InOptExists ¶
InOptExists determines if the option exists in the given options
func OutOptExists ¶
OutOptExists determines if the option exists in the given options
func OverwriteDebugOpt ¶ added in v1.1.4
OverwriteDebugOpt sets the value for the option to the given value. Use this when the new value needs set regardless if the option exists or not.
func OverwriteDryrunOpt ¶ added in v1.1.43
OverwriteDryrunOpt sets the value for the option to the given value. Use this when the new value needs set regardless if the option exists or not.
func OverwriteErrOpt ¶ added in v1.1.4
OverwriteErrOpt sets the value for the option to the given value. Use this when the new value needs set regardless if the option exists or not.
func OverwriteHomeOpt ¶ added in v1.1.4
OverwriteHomeOpt sets the value for the option to the given value. Use this when the new value needs set regardless if the option exists or not.
func OverwriteInOpt ¶ added in v1.1.4
OverwriteInOpt sets the value for the option to the given value. Use this when the new value needs set regardless if the option exists or not.
func OverwriteOutOpt ¶ added in v1.1.4
OverwriteOutOpt sets the value for the option to the given value. Use this when the new value needs set regardless if the option exists or not.
func OverwriteQuietOpt ¶ added in v1.1.4
OverwriteQuietOpt sets the value for the option to the given value. Use this when the new value needs set regardless if the option exists or not.
func OverwriteTestingOpt ¶ added in v1.1.4
OverwriteTestingOpt sets the value for the option to the given value. Use this when the new value needs set regardless if the option exists or not.
func QuietOptExists ¶
QuietOptExists determines if the option exists in the given options
func TestingOptExists ¶
TestingOptExists determines if the option exists in the given options
Types ¶
type Opt ¶
type Opt struct { Key string // name of the option Val interface{} // value of the option }
Opt provides a mechanism for passing in optional paramaters to functions.
func Copy ¶
Copy the given options slice removing nil options. Although the slice and options are new distinct objects the Values are the original objects.
func TestingOpt ¶
TestingOpt creates the new option with the given value
type Opts ¶ added in v1.1.44
type Opts []*Opt
Opts provides some convenience methods for working with options
func (*Opts) Add ¶ added in v1.1.44
Add an option to the options slice if it doesn't exist. Just an alias to Default, but the alternate naming is more intuitive in different scenarios.
func (*Opts) Copy ¶ added in v1.1.44
Copy the given options slice removing nil options. Although the slice and options are new distinct objects the Values are the original objects.
func (*Opts) Default ¶ added in v1.1.44
Default adds an option to the options slice if it doesn't exist. Returns true the option was added to the options slice or false if the given slice or option are nil or the option already exists in the slice.
func (*Opts) Exists ¶ added in v1.1.44
Exists checks if the given opt exists in the opts slice by key
func (*Opts) GetString ¶ added in v1.1.45
GetString gets an option by key and casts it to a string type.
type Std ¶ added in v1.1.42
type Std interface { StdStream Home(home ...string) string // Home path to use Quiet(quiet ...bool) bool // Quiet mode when true Debug(debug ...bool) bool // Debug mode when true Dryrun(dryrun ...bool) bool // Dryrun mode when true Testing(testing ...bool) bool // Testing mode when true }
Std provides an interface for the standard properties
type StdProps ¶
type StdProps struct { StdStreamProps Home string // Home path to use Quiet bool // Quiet mode when true Debug bool // Debug mode when true Dryrun bool // Dryrun mode when true Testing bool // Testing mode when true }
StdProps provides common properties for custom types