diff options
Diffstat (limited to 'internal/flag')
-rw-r--r-- | internal/flag/flag.go | 34 | ||||
-rw-r--r-- | internal/flag/flag_test.go | 29 |
2 files changed, 63 insertions, 0 deletions
diff --git a/internal/flag/flag.go b/internal/flag/flag.go new file mode 100644 index 0000000..7c7d977 --- /dev/null +++ b/internal/flag/flag.go @@ -0,0 +1,34 @@ +package flag + +import ( + "fmt" + "strings" +) + +// A StringsValue is a command-line flag that interprets its argument +// as a space-separated list of strings. +type StringsValue []string + +// Set implements the flag.Value interface by spliting the provided string at +// spaces. +func (v *StringsValue) Set(s string) error { + if s == "" { + *v = []string{} + return nil + } + + *v = strings.Fields(s) + + return nil +} + +// Get implements the flag.Getter interface by returning the contents of this +// value. +func (v *StringsValue) Get() interface{} { + return []string(*v) +} + +// String returns a string +func (v *StringsValue) String() string { + return fmt.Sprintf("%s", *v) +} diff --git a/internal/flag/flag_test.go b/internal/flag/flag_test.go new file mode 100644 index 0000000..f5ffca4 --- /dev/null +++ b/internal/flag/flag_test.go @@ -0,0 +1,29 @@ +package flag_test + +import ( + "testing" + + "github.com/google/go-cmp/cmp" + "github.com/terinjokes/bakelite/internal/flag" +) + +func TestStringsValue(t *testing.T) { + tests := []struct { + v string + want []string + }{ + {"", []string{}}, + {"a b c", []string{"a", "b", "c"}}, + {"foo bar baz", []string{"foo", "bar", "baz"}}, + {"foo bar baz", []string{"foo", "bar", "baz"}}, + } + + for i, tt := range tests { + got := flag.StringsValue([]string{}) + got.Set(tt.v) + + if diff := cmp.Diff(tt.want, got.Get()); diff != "" { + t.Errorf("#%d: manifest differs. (-got +want):\n%s", i, diff) + } + } +} |