aboutsummaryrefslogtreecommitdiff
path: root/internal/flag/flag.go
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 /internal/flag/flag.go
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
Diffstat (limited to 'internal/flag/flag.go')
-rw-r--r--internal/flag/flag.go34
1 files changed, 34 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)
+}