diff options
Diffstat (limited to '')
-rw-r--r-- | parsefuncs.go | 48 |
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 +} |