Documentation ¶
Overview ¶
Package enumflag defines a flag.Value implementation that accepts one of a specified collection of string keys. Values are compared without respect to case, so that "foo" and "Foo" are accepted as equivalent to "FOO".
Example:
import ( "flag" "github.com/creachadair/goflags/enumflag" ) // The first enumerated value is the default. var color = enumflag.New("", "red", "orange", "yellow", "green", "blue") func init() { flag.Var(color, "color", color.Help("What color to paint the bikeshed")) }
Example ¶
package main import ( "flag" "fmt" "log" "github.com/creachadair/goflags/enumflag" ) func main() { fs := flag.NewFlagSet("example", flag.ContinueOnError) var feature = enumflag.New("auto", "on", "off") fs.Var(feature, "feature", feature.Help("Enable the new behaviour")) if err := fs.Parse([]string{"-feature", "off"}); err != nil { log.Fatalf("Parse: %v", err) } fmt.Printf("Chose index %d (%q)\n", feature.Index(), feature.Key()) }
Output: Chose index 2 ("off")
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Value ¶
type Value struct {
// contains filtered or unexported fields
}
A Value represents an enumeration of string values. A pointer to a Value satisfies the flag.Value interface. Use the Key method to recover the currently-selected value of the enumeration.
func New ¶
New returns a *Value for the specified enumerators, where defaultKey is the default value and otherKeys are additional options. The index of a selected key reflects its position in the order given to this function, so that if:
v := enumflag.New("a", "b", "c", "d")
then the index of "a" is 0, "b" is 1, "c" is 2, "d" is 3. The default key is always stored at index 0.
func (Value) Get ¶
Get satisfies the flag.Getter interface. The concrete value is the the string of the current key.
func (Value) Help ¶
Help concatenates a human-readable string summarizing the legal values of v to h, for use in generating a documentation string.
func (Value) Index ¶
Index returns the currently-selected index in the enumeration. The order of keys reflects the original order in which they were passed to the constructor, so index 0 is the default value.
func (Value) Key ¶
Key returns the currently-selected key in the enumeration. The original spelling of the selected value is returned, as given to the constructor, not the value as parsed.