aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLibravatar Terin Stock <terinjokes@gmail.com>2018-01-13 21:55:25 -0800
committerLibravatar Terin Stock <terinjokes@gmail.com>2018-01-13 21:55:25 -0800
commit4dabf654cdd8d1486e8c13bd99a7749fb299db63 (patch)
tree521edc73a974cd763fe688c179e48f22e47df2e9
parentchore(travis): upgrade Go to version 1.9.x (diff)
downloadbakelite-4dabf654cdd8d1486e8c13bd99a7749fb299db63.tar.xz
feat(main): implement []string flag valuer
Implement flag.Value to automnatically split strings into an array at their spaces. Change-Id: Ia9139e23d74a30acfc74cb65935bb7fc2b322aec
-rw-r--r--Gopkg.lock8
-rw-r--r--internal/flag/flag.go34
-rw-r--r--internal/flag/flag_test.go29
3 files changed, 70 insertions, 1 deletions
diff --git a/Gopkg.lock b/Gopkg.lock
index 86ff3bb..49c1aac 100644
--- a/Gopkg.lock
+++ b/Gopkg.lock
@@ -2,6 +2,12 @@
[[projects]]
+ name = "github.com/google/go-cmp"
+ packages = ["cmp","cmp/internal/diff","cmp/internal/function","cmp/internal/value"]
+ revision = "8099a9787ce5dc5984ed879a3bda47dc730a8e97"
+ version = "v0.1.0"
+
+[[projects]]
branch = "master"
name = "golang.org/x/net"
packages = ["context"]
@@ -16,6 +22,6 @@
[solve-meta]
analyzer-name = "dep"
analyzer-version = 1
- inputs-digest = "1b7a27daae87f4f213b3475a08fd7367804c2755750bf27adb8c32ada7c9bdae"
+ inputs-digest = "14999a24917f91009d496a5729d3a7dd84e62b29e070424212d8de53cc71b47e"
solver-name = "gps-cdcl"
solver-version = 1
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)
+ }
+ }
+}