summaryrefslogtreecommitdiff
path: root/parsefuncs.go
diff options
context:
space:
mode:
authorEthel Morgan <eth@ethulhu.co.uk>2020-06-24 12:10:57 +0100
committerEthel Morgan <eth@ethulhu.co.uk>2020-06-24 12:10:57 +0100
commitaa380da6a61f9b29ee263d95d17a2953a0528b28 (patch)
treec2915f6508ed321eeacb6ca3c7ba8de313c0ce11 /parsefuncs.go
parent02f268cc3ba056be15097a7185a367585dd05275 (diff)
import package flag from helixv0.0.1
Diffstat (limited to 'parsefuncs.go')
-rw-r--r--parsefuncs.go48
1 files changed, 48 insertions, 0 deletions
diff --git a/parsefuncs.go b/parsefuncs.go
new file mode 100644
index 0000000..0263a5a
--- /dev/null
+++ b/parsefuncs.go
@@ -0,0 +1,48 @@
+// SPDX-FileCopyrightText: 2020 Ethel Morgan
+//
+// SPDX-License-Identifier: MIT
+
+package flag
+
+import (
+ "errors"
+ "fmt"
+ "strconv"
+ "strings"
+)
+
+func RequiredString(raw string) (interface{}, error) {
+ if raw == "" {
+ return nil, errors.New("must not be empty")
+ }
+ return raw, nil
+}
+
+func StringEnum(values ...string) ParseFunc {
+ return func(raw string) (interface{}, error) {
+ for _, value := range values {
+ if value == raw {
+ return raw, nil
+ }
+ }
+ return raw, fmt.Errorf("must be one of %q", values)
+ }
+}
+
+func IntList(raw string) (interface{}, error) {
+ var ints []int
+
+ if raw == "" {
+ return ints, nil
+ }
+
+ for _, raw := range strings.Split(raw, ",") {
+ i, err := strconv.Atoi(raw)
+ if err != nil {
+ return ints, err
+ }
+ ints = append(ints, i)
+ }
+
+ return ints, nil
+}