Skip to content

tep/flags-tristate

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

tristate Mit License GitHub Release GoDoc Go Report Card Build Status

import "toolman.org/flags/tristate"

    go get toolman.org/flags/tristate

Package tristate provides a custom TriState flag for use with the alternate flag package github.com/spf13/pflag. A TriState value may take one of three forms: True, False or None and is most useful when you are, for instance, filtering records based on a current boolean value -- and you need all three possibilities (e.g. True, False and I Don't Care)

There are 8 separate functions provided for defining a TriState flag with some combination of the suffixes: "Var", "P", and "FS" each having the following meaning:

Var: Accepts a TriState pointer instead of returning one.
P:   Also takes a shorthand character for use with a single dash
FS:  Accepts a *FlagSet where the flag should be added

The "Var" and "P" suffixes follow the common pflag convention. The "FS" suffix is added to allow the use of alternate FlagSets.

tristate.go

var (
    // CommandLine is the default FlagSet where flags will be added (unless
    // otherwise specified)
    CommandLine = pflag.CommandLine
)
var ErrBadTriStateValue = errors.New("bad tristate value")

ErrBadTriStateValue is returned by Set if if cannot parse its input.

func FlagVar(ts *TriState, name string, value TriState, usage string)

FlagVar is similar to Flag that also accepts a pointer to TriState variable where the flag value should be stored.

func FlagVarFS(fs *pflag.FlagSet, ts *TriState, name string, value TriState, usage string)

FlagVarFS is similar to FlagVar but accepts a pointer to the FlagSet where this flag should be added.

func FlagVarP(ts *TriState, name, shorthand string, value TriState, usage string)

FlagVarP is the combination of FlagVar and FlagP.

func FlagVarPFS(fs *pflag.FlagSet, ts *TriState, name, shorthand string, value TriState, usage string)

FlagVarPFS is similar to FlagVarP but accepts a pointer to the FlagSet where this flag should be added.

type TriState int

TriState is a TriState Value and may have one of three values: None, False or True. Its "zero" value is None.

const (
    None TriState = iota
    False
    True
)

The three possible tristate values

func Flag(name string, value TriState, usage string) *TriState

Flag defines a tristate.TriState flag with the specified name, default value and usage string. The return value is the address of a TriState variable the stores the values of the flag.

func FlagFS(fs *pflag.FlagSet, name string, value TriState, usage string) *TriState

FlagFS is similar to Flag but accepts a pointer to the FlagSet where this flag should be added.

func FlagP(name, shorthand string, value TriState, usage string) *TriState

FlagP is similar to Flag butl also accepts a shorthand letter to be used after a single dash.

func FlagPFS(fs *pflag.FlagSet, name, shorthand string, value TriState, usage string) *TriState

FlagPFS is similar to FlagP but accepts a pointer to the FlagSet where this flag should be added.

func (*TriState) Bool

func (ts *TriState) Bool() *bool

Bool returns a pointer to a bool holding the value of ts. If ts is None then Bool returns nil.

func (*TriState) Get

func (ts *TriState) Get() interface{}

Get implements flag.Getter

func (*TriState) Set

func (ts *TriState) Set(s string) error

Set the tristate.Value by parsing s according to the following rules:

Value: Strings
------ ----------------------------------------------------------
True:  1, t, true, y, yes
False: 0, f, false, n, no
None:  -1, u, unknown, e, either, b, both, a, all, none, null, nil

Allstring input is case insensitive. Any string not mentioned above will return ErrBadTriStateValue.

Set contributes to the implementation of pflag.Value

func (TriState) String

func (ts TriState) String() string

String contributes to the implementation of pflag.Value

func (*TriState) Type

func (ts *TriState) Type() string

Type contributes to the implementation of pflag.Value