Extract flag options to predict package

This commit is contained in:
Eyal Posener 2019-11-22 11:40:09 +02:00
parent cf002b9a50
commit e8bbd08d57
4 changed files with 109 additions and 80 deletions

View File

@ -39,37 +39,11 @@ import (
"fmt" "fmt"
"os" "os"
"strconv" "strconv"
"strings"
"github.com/posener/complete/v2" "github.com/posener/complete/v2"
"github.com/posener/complete/v2/predict"
) )
// Flag options.
type Option func(*options)
// OptValues allows to set a desired set of valid values for the flag.
func OptValues(values ...string) Option {
return func(o *options) { o.values = values }
}
// OptCheck enforces the valid values on the predicted flag.
func OptCheck() Option {
return func(o *options) { o.check = true }
}
type options struct {
values []string
check bool
}
func config(fs ...Option) options {
var op options
for _, f := range fs {
f(&op)
}
return op
}
// FlagSet is bash completion enabled flag.FlagSet. // FlagSet is bash completion enabled flag.FlagSet.
type FlagSet flag.FlagSet type FlagSet flag.FlagSet
@ -83,36 +57,24 @@ func (fs *FlagSet) Complete(name string) {
complete.Complete(name, complete.FlagSet((*flag.FlagSet)(CommandLine))) complete.Complete(name, complete.FlagSet((*flag.FlagSet)(CommandLine)))
} }
func (fs *FlagSet) String(name string, value string, usage string, options ...Option) *string { func (fs *FlagSet) String(name string, value string, usage string, options ...predict.Option) *string {
p := new(string) p := new(string)
(*flag.FlagSet)(fs).Var(newStringValue(value, p, config(options...)), name, usage) (*flag.FlagSet)(fs).Var(newStringValue(value, p, predict.Options(options...)), name, usage)
return p return p
} }
func (fs *FlagSet) Bool(name string, value bool, usage string, options ...Option) *bool { func (fs *FlagSet) Bool(name string, value bool, usage string, options ...predict.Option) *bool {
p := new(bool) p := new(bool)
(*flag.FlagSet)(fs).Var(newBoolValue(value, p, config(options...)), name, usage) (*flag.FlagSet)(fs).Var(newBoolValue(value, p, predict.Options(options...)), name, usage)
return p return p
} }
func (fs *FlagSet) Int(name string, value int, usage string, options ...Option) *int { func (fs *FlagSet) Int(name string, value int, usage string, options ...predict.Option) *int {
p := new(int) p := new(int)
(*flag.FlagSet)(fs).Var(newIntValue(value, p, config(options...)), name, usage) (*flag.FlagSet)(fs).Var(newIntValue(value, p, predict.Options(options...)), name, usage)
return p return p
} }
func (o options) checkValue(v string) error {
if !o.check || len(o.values) == 0 {
return nil
}
for _, vv := range o.values {
if v == vv {
return nil
}
}
return fmt.Errorf("not in allowed values: %s", strings.Join(o.values, ","))
}
var CommandLine = (*FlagSet)(flag.CommandLine) var CommandLine = (*FlagSet)(flag.CommandLine)
// Parse parses command line arguments. It also performs bash completion when needed. // Parse parses command line arguments. It also performs bash completion when needed.
@ -121,26 +83,26 @@ func Parse(name string) {
CommandLine.Parse(os.Args[1:]) CommandLine.Parse(os.Args[1:])
} }
func String(name string, value string, usage string, options ...Option) *string { func String(name string, value string, usage string, options ...predict.Option) *string {
return CommandLine.String(name, value, usage, options...) return CommandLine.String(name, value, usage, options...)
} }
func Bool(name string, value bool, usage string, options ...Option) *bool { func Bool(name string, value bool, usage string, options ...predict.Option) *bool {
return CommandLine.Bool(name, value, usage, options...) return CommandLine.Bool(name, value, usage, options...)
} }
func Int(name string, value int, usage string, options ...Option) *int { func Int(name string, value int, usage string, options ...predict.Option) *int {
return CommandLine.Int(name, value, usage, options...) return CommandLine.Int(name, value, usage, options...)
} }
type boolValue struct { type boolValue struct {
v *bool v *bool
options predict.Config
} }
func newBoolValue(val bool, p *bool, o options) *boolValue { func newBoolValue(val bool, p *bool, c predict.Config) *boolValue {
*p = val *p = val
return &boolValue{v: p, options: o} return &boolValue{v: p, Config: c}
} }
func (b *boolValue) Set(val string) error { func (b *boolValue) Set(val string) error {
@ -149,7 +111,7 @@ func (b *boolValue) Set(val string) error {
if err != nil { if err != nil {
return fmt.Errorf("bad value for bool flag") return fmt.Errorf("bad value for bool flag")
} }
return b.checkValue(val) return b.Check(val)
} }
func (b *boolValue) Get() interface{} { return bool(*b.v) } func (b *boolValue) Get() interface{} { return bool(*b.v) }
@ -163,9 +125,9 @@ func (b *boolValue) String() string {
func (b *boolValue) IsBoolFlag() bool { return true } func (b *boolValue) IsBoolFlag() bool { return true }
func (b *boolValue) Predict(_ string) []string { func (b *boolValue) Predict(prefix string) []string {
if b.values != nil { if b.Predictor != nil {
return b.values return b.Predictor.Predict(prefix)
} }
// If false, typing the bool flag is expected to turn it on, so there is nothing to complete // If false, typing the bool flag is expected to turn it on, so there is nothing to complete
// after the flag. // after the flag.
@ -178,17 +140,17 @@ func (b *boolValue) Predict(_ string) []string {
type stringValue struct { type stringValue struct {
v *string v *string
options predict.Config
} }
func newStringValue(val string, p *string, o options) *stringValue { func newStringValue(val string, p *string, c predict.Config) *stringValue {
*p = val *p = val
return &stringValue{v: p, options: o} return &stringValue{v: p, Config: c}
} }
func (s *stringValue) Set(val string) error { func (s *stringValue) Set(val string) error {
*s.v = val *s.v = val
return s.options.checkValue(val) return s.Check(val)
} }
func (s *stringValue) Get() interface{} { func (s *stringValue) Get() interface{} {
@ -202,21 +164,21 @@ func (s *stringValue) String() string {
return string(*s.v) return string(*s.v)
} }
func (s *stringValue) Predict(_ string) []string { func (s *stringValue) Predict(prefix string) []string {
if s.values != nil { if s.Predictor != nil {
return s.values return s.Predictor.Predict(prefix)
} }
return []string{""} return []string{""}
} }
type intValue struct { type intValue struct {
v *int v *int
options predict.Config
} }
func newIntValue(val int, p *int, o options) *intValue { func newIntValue(val int, p *int, c predict.Config) *intValue {
*p = val *p = val
return &intValue{v: p, options: o} return &intValue{v: p, Config: c}
} }
func (i *intValue) Set(val string) error { func (i *intValue) Set(val string) error {
@ -225,7 +187,7 @@ func (i *intValue) Set(val string) error {
if err != nil { if err != nil {
return fmt.Errorf("bad value for int flag") return fmt.Errorf("bad value for int flag")
} }
return i.checkValue(val) return i.Check(val)
} }
func (i *intValue) Get() interface{} { return int(*i.v) } func (i *intValue) Get() interface{} { return int(*i.v) }
@ -237,9 +199,9 @@ func (i *intValue) String() string {
return strconv.Itoa(int(*i.v)) return strconv.Itoa(int(*i.v))
} }
func (s *intValue) Predict(_ string) []string { func (s *intValue) Predict(prefix string) []string {
if s.values != nil { if s.Predictor != nil {
return s.values return s.Predictor.Predict(prefix)
} }
return []string{""} return []string{""}
} }

View File

@ -5,6 +5,7 @@ import (
"testing" "testing"
"github.com/posener/complete/v2" "github.com/posener/complete/v2"
"github.com/posener/complete/v2/predict"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@ -30,7 +31,7 @@ func TestString(t *testing.T) {
t.Run("options invalid not checked", func(t *testing.T) { t.Run("options invalid not checked", func(t *testing.T) {
var cmd FlagSet var cmd FlagSet
value := cmd.String("a", "", "", OptValues("1", "2")) value := cmd.String("a", "", "", predict.OptValues("1", "2"))
err := cmd.Parse([]string{"-a", "3"}) err := cmd.Parse([]string{"-a", "3"})
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, "3", *value) assert.Equal(t, "3", *value)
@ -38,7 +39,7 @@ func TestString(t *testing.T) {
t.Run("options valid checked", func(t *testing.T) { t.Run("options valid checked", func(t *testing.T) {
var cmd FlagSet var cmd FlagSet
value := cmd.String("a", "", "", OptValues("1", "2"), OptCheck()) value := cmd.String("a", "", "", predict.OptValues("1", "2"), predict.OptCheck())
err := cmd.Parse([]string{"-a", "2"}) err := cmd.Parse([]string{"-a", "2"})
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, "2", *value) assert.Equal(t, "2", *value)
@ -46,14 +47,14 @@ func TestString(t *testing.T) {
t.Run("options invalid checked", func(t *testing.T) { t.Run("options invalid checked", func(t *testing.T) {
var cmd FlagSet var cmd FlagSet
_ = cmd.String("a", "", "", OptValues("1", "2"), OptCheck()) _ = cmd.String("a", "", "", predict.OptValues("1", "2"), predict.OptCheck())
err := cmd.Parse([]string{"-a", "3"}) err := cmd.Parse([]string{"-a", "3"})
assert.Error(t, err) assert.Error(t, err)
}) })
t.Run("complete", func(t *testing.T) { t.Run("complete", func(t *testing.T) {
var cmd FlagSet var cmd FlagSet
_ = cmd.String("a", "", "", OptValues("1", "2")) _ = cmd.String("a", "", "", predict.OptValues("1", "2"))
complete.Test(t, complete.FlagSet((*flag.FlagSet)(&cmd)), "-a ", []string{"1", "2"}) complete.Test(t, complete.FlagSet((*flag.FlagSet)(&cmd)), "-a ", []string{"1", "2"})
complete.Test(t, complete.FlagSet((*flag.FlagSet)(&cmd)), "-a=", []string{"1", "2"}) complete.Test(t, complete.FlagSet((*flag.FlagSet)(&cmd)), "-a=", []string{"1", "2"})
complete.Test(t, complete.FlagSet((*flag.FlagSet)(&cmd)), "-a 1", []string{"1"}) complete.Test(t, complete.FlagSet((*flag.FlagSet)(&cmd)), "-a 1", []string{"1"})
@ -66,7 +67,7 @@ func TestInt(t *testing.T) {
t.Run("options invalid not checked", func(t *testing.T) { t.Run("options invalid not checked", func(t *testing.T) {
var cmd FlagSet var cmd FlagSet
value := cmd.Int("a", 0, "", OptValues("1", "2")) value := cmd.Int("a", 0, "", predict.OptValues("1", "2"))
err := cmd.Parse([]string{"-a", "3"}) err := cmd.Parse([]string{"-a", "3"})
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, 3, *value) assert.Equal(t, 3, *value)
@ -74,7 +75,7 @@ func TestInt(t *testing.T) {
t.Run("options valid checked", func(t *testing.T) { t.Run("options valid checked", func(t *testing.T) {
var cmd FlagSet var cmd FlagSet
value := cmd.Int("a", 0, "", OptValues("1", "2"), OptCheck()) value := cmd.Int("a", 0, "", predict.OptValues("1", "2"), predict.OptCheck())
err := cmd.Parse([]string{"-a", "2"}) err := cmd.Parse([]string{"-a", "2"})
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, 2, *value) assert.Equal(t, 2, *value)
@ -82,21 +83,21 @@ func TestInt(t *testing.T) {
t.Run("options invalid checked", func(t *testing.T) { t.Run("options invalid checked", func(t *testing.T) {
var cmd FlagSet var cmd FlagSet
_ = cmd.Int("a", 0, "", OptValues("1", "2"), OptCheck()) _ = cmd.Int("a", 0, "", predict.OptValues("1", "2"), predict.OptCheck())
err := cmd.Parse([]string{"-a", "3"}) err := cmd.Parse([]string{"-a", "3"})
assert.Error(t, err) assert.Error(t, err)
}) })
t.Run("options invalid int value", func(t *testing.T) { t.Run("options invalid int value", func(t *testing.T) {
var cmd FlagSet var cmd FlagSet
_ = cmd.Int("a", 0, "", OptValues("1", "2", "x"), OptCheck()) _ = cmd.Int("a", 0, "", predict.OptValues("1", "2", "x"), predict.OptCheck())
err := cmd.Parse([]string{"-a", "x"}) err := cmd.Parse([]string{"-a", "x"})
assert.Error(t, err) assert.Error(t, err)
}) })
t.Run("complete", func(t *testing.T) { t.Run("complete", func(t *testing.T) {
var cmd FlagSet var cmd FlagSet
_ = cmd.Int("a", 0, "", OptValues("1", "2")) _ = cmd.Int("a", 0, "", predict.OptValues("1", "2"))
complete.Test(t, complete.FlagSet((*flag.FlagSet)(&cmd)), "-a ", []string{"1", "2"}) complete.Test(t, complete.FlagSet((*flag.FlagSet)(&cmd)), "-a ", []string{"1", "2"})
complete.Test(t, complete.FlagSet((*flag.FlagSet)(&cmd)), "-a=", []string{"1", "2"}) complete.Test(t, complete.FlagSet((*flag.FlagSet)(&cmd)), "-a=", []string{"1", "2"})
complete.Test(t, complete.FlagSet((*flag.FlagSet)(&cmd)), "-a 1", []string{"1"}) complete.Test(t, complete.FlagSet((*flag.FlagSet)(&cmd)), "-a 1", []string{"1"})

View File

@ -7,13 +7,14 @@ import (
"os" "os"
"github.com/posener/complete/v2/compflag" "github.com/posener/complete/v2/compflag"
"github.com/posener/complete/v2/predict"
) )
var ( var (
// Add variables to the program. Since we are using the compflag library, we can pass options to // Add variables to the program. Since we are using the compflag library, we can pass options to
// enable bash completion to the flag values. // enable bash completion to the flag values.
name = compflag.String("name", "", "Give your name", compflag.OptValues("foo", "bar", "foo bar")) name = compflag.String("name", "", "Give your name", predict.OptValues("foo", "bar", "foo bar"))
something = compflag.String("something", "", "Expect somthing, but we don't know what, so no other completion options will be provided.", compflag.OptValues("")) something = compflag.String("something", "", "Expect somthing, but we don't know what, so no other completion options will be provided.", predict.OptValues(""))
nothing = compflag.String("nothing", "", "Expect nothing after flag, so other completion can be provided.") nothing = compflag.String("nothing", "", "Expect nothing after flag, so other completion can be provided.")
) )

65
predict/options.go Normal file
View File

@ -0,0 +1,65 @@
package predict
import (
"fmt"
"github.com/posener/complete/v2"
"strings"
)
// Option provides prediction through options pattern.
//
// Usage:
//
// func(o ...predict.Option) {
// cfg := predict.Options(o)
// // use cfg.Predict...
// }
type Option func(*Config)
// OptValues allows to set a desired set of valid values for the flag.
func OptValues(values ...string) Option {
return OptPredictor(Set(values))
}
// OptPredictor allows to set a custom predictor.
func OptPredictor(p complete.Predictor) Option {
return func(o *Config) { o.Predictor = p }
}
// OptCheck enforces the valid values on the predicted flag.
func OptCheck() Option {
return func(o *Config) { o.check = true }
}
type Config struct {
complete.Predictor
check bool
}
func Options(os ...Option) Config {
var op Config
for _, f := range os {
f(&op)
}
return op
}
func (c Config) Predict(prefix string) []string {
if c.Predictor != nil {
return c.Predictor.Predict(prefix)
}
return nil
}
func (c Config) Check(value string) error {
predictions := c.Predictor.Predict(value)
if !c.check || len(predictions) == 0 {
return nil
}
for _, vv := range predictions {
if value == vv {
return nil
}
}
return fmt.Errorf("not in allowed values: %s", strings.Join(predictions, ","))
}