diff options
Diffstat (limited to 'cmd/jp/flag.go')
-rw-r--r-- | cmd/jp/flag.go | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/cmd/jp/flag.go b/cmd/jp/flag.go new file mode 100644 index 0000000..533b35d --- /dev/null +++ b/cmd/jp/flag.go @@ -0,0 +1,26 @@ +package main + +import ( + "fmt" + "strings" +) + +type enumVar struct { + Choices []string // The acceptable choices the user may pass to the flag + Value string // the current value of the flag +} + +// Set implements the flag.Value interface. +func (so *enumVar) Set(v string) error { + for _, c := range so.Choices { + if c == v { + so.Value = v + return nil + } + } + return fmt.Errorf("invalid choice; must be one of %s", strings.Join(so.Choices, ",")) +} + +func (so *enumVar) String() string { + return so.Value +} |