opt

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: May 15, 2024 License: MIT Imports: 2 Imported by: 1

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

Constants

This section is empty.

Variables

View Source
var (
	InOptKey      = "in"
	OutOptKey     = "out"
	ErrOptKey     = "err"
	HomeOptKey    = "home"
	QuietOptKey   = "quiet"
	DebugOptKey   = "debug"
	DryrunOptKey  = "dry-run"
	TestingOptKey = "testing"
)

Common options keys

Functions

func Add

func Add(opts *[]*Opt, o ...*Opt) (result bool)

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

func DebugOptExists(opts []*Opt) bool

DebugOptExists determines if the option exists in the given options

func Default

func Default(opts *[]*Opt, o ...*Opt) (result bool)

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

func DefaultDebugOpt(opts []*Opt, val bool) bool

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

func DefaultDryrunOpt(opts []*Opt, val bool) bool

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

func DefaultErrOpt(opts []*Opt, val io.Writer) io.Writer

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

func DefaultHomeOpt(opts []*Opt, val string) string

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

func DefaultInOpt(opts []*Opt, val io.Reader) io.Reader

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

func DefaultOutOpt(opts []*Opt, val io.Writer) io.Writer

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

func DefaultQuietOpt(opts []*Opt, val bool) bool

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

func DefaultTestingOpt(opts []*Opt, val bool) bool

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

func DryrunOptExists(opts []*Opt) bool

DryrunOptExists determines if the option exists in the given options

func ErrOptExists

func ErrOptExists(opts []*Opt) bool

ErrOptExists determines if the option exists in the given options

func Exists

func Exists(opts []*Opt, key string) bool

Exists checks if the given opt exists in the opts slice by key

func GetBool added in v1.1.45

func GetBool(opts []*Opt, key string) (result bool)

GetBool gets an option by key and casts it to a bool type.

func GetDebugOpt

func GetDebugOpt(opts []*Opt) (result bool)

GetDebugOpt finds and returns the option's value or defaults to false

func GetDryrunOpt added in v1.1.43

func GetDryrunOpt(opts []*Opt) (result bool)

GetDryrunOpt finds and returns the option's value or defaults to false

func GetErrOpt

func GetErrOpt(opts []*Opt) io.Writer

GetErrOpt finds and returns the option's value or defaults to os.Stderr

func GetHomeOpt

func GetHomeOpt(opts []*Opt) string

GetHomeOpt finds and returns the option's value or defaults to empty string

func GetInOpt

func GetInOpt(opts []*Opt) io.Reader

GetInOpt finds and returns the option's value or defaults to os.Stdin

func GetOutOpt

func GetOutOpt(opts []*Opt) io.Writer

GetOutOpt finds and returns the option's value or defaults to os.Stdout

func GetQuietOpt

func GetQuietOpt(opts []*Opt) (result bool)

GetQuietOpt finds and returns the option's value or defaults to false

func GetString added in v1.1.45

func GetString(opts []*Opt, key string) (result string)

GetString gets an option by key and casts it to a string type.

func GetTestingOpt

func GetTestingOpt(opts []*Opt) (result bool)

GetTestingOpt finds and returns the option's value or defaults to false

func HomeOptExists

func HomeOptExists(opts []*Opt) bool

HomeOptExists determines if the option exists in the given options

func InOptExists

func InOptExists(opts []*Opt) bool

InOptExists determines if the option exists in the given options

func OutOptExists

func OutOptExists(opts []*Opt) bool

OutOptExists determines if the option exists in the given options

func OverwriteDebugOpt added in v1.1.4

func OverwriteDebugOpt(opts *[]*Opt, val bool) bool

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

func OverwriteDryrunOpt(opts *[]*Opt, val bool) bool

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

func OverwriteErrOpt(opts *[]*Opt, val io.Writer) io.Writer

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

func OverwriteHomeOpt(opts *[]*Opt, val string) string

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

func OverwriteInOpt(opts *[]*Opt, val io.Reader) io.Reader

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

func OverwriteOutOpt(opts *[]*Opt, val io.Writer) io.Writer

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

func OverwriteQuietOpt(opts *[]*Opt, val bool) bool

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

func OverwriteTestingOpt(opts *[]*Opt, val bool) bool

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

func QuietOptExists(opts []*Opt) bool

QuietOptExists determines if the option exists in the given options

func Remove

func Remove(opts *[]*Opt, key string)

Remove an existing option by key from the options list

func TestingOptExists

func TestingOptExists(opts []*Opt) bool

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

func Copy(opts []*Opt) []*Opt

Copy the given options slice removing nil options. Although the slice and options are new distinct objects the Values are the original objects.

func DebugOpt

func DebugOpt(val bool) *Opt

DebugOpt creates the new option with the given value

func DryrunOpt added in v1.1.43

func DryrunOpt(val bool) *Opt

DryrunOpt creates the new option with the given value

func ErrOpt

func ErrOpt(val io.Writer) *Opt

ErrOpt creates the new option with the given value

func Get

func Get(opts []*Opt, key string) *Opt

Get an option by the given key

func HomeOpt

func HomeOpt(val string) *Opt

HomeOpt creates the new option with the given value

func InOpt

func InOpt(val io.Reader) *Opt

InOpt creates the new option with the given value

func New added in v1.1.44

func New(key string, val interface{}) *Opt

New is a conveniance function to new up an Opt

func OutOpt

func OutOpt(val io.Writer) *Opt

OutOpt creates the new option with the given value

func Overwrite

func Overwrite(opts *[]*Opt, opt *Opt) *Opt

Overwrite replaces an existing option or adds the option if it doesn't exist.

func QuietOpt

func QuietOpt(val bool) *Opt

QuietOpt creates the new option with the given value

func TestingOpt

func TestingOpt(val bool) *Opt

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 NewOpts added in v1.1.46

func NewOpts(o ...*Opt) (opts *Opts)

NewOpts is a conveniance function to new up an Opts

func (*Opts) Add added in v1.1.44

func (x *Opts) Add(o ...*Opt) (result bool)

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

func (x *Opts) Copy() []*Opt

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

func (x *Opts) Default(o ...*Opt) (result bool)

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

func (x *Opts) Exists(key string) bool

Exists checks if the given opt exists in the opts slice by key

func (*Opts) Get added in v1.1.44

func (x *Opts) Get(key string) *Opt

Get an option by the given key

func (*Opts) GetBool added in v1.1.45

func (x *Opts) GetBool(key string) (result bool)

GetBool gets an option by key and casts it to a bool type.

func (*Opts) GetString added in v1.1.45

func (x *Opts) GetString(key string) (result string)

GetString gets an option by key and casts it to a string type.

func (*Opts) Len added in v1.1.44

func (x *Opts) Len() int

Len provides the length of the options

func (*Opts) Overwrite added in v1.1.44

func (x *Opts) Overwrite(opt *Opt) *Opt

Overwrite replaces an existing option or adds the option if it doesn't exist.

func (*Opts) Remove added in v1.1.44

func (x *Opts) Remove(key string)

Remove an existing option by key from the options list

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

type StdStream added in v1.1.42

type StdStream interface {
	In(in ...io.Reader) io.Reader   // Input stream to use
	Out(out ...io.Writer) io.Writer // Output stream to use
	Err(err ...io.Writer) io.Writer // Error stream to use
}

StdStream provides an interface for the standard streams

type StdStreamProps

type StdStreamProps struct {
	In  io.Reader // Input stream to use
	Out io.Writer // Output stream to use
	Err io.Writer // Error stream to use
}

StdStreamProps provides common io stream properties for custom types

Jump to

Keyboard shortcuts

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