From aa380da6a61f9b29ee263d95d17a2953a0528b28 Mon Sep 17 00:00:00 2001 From: Ethel Morgan Date: Wed, 24 Jun 2020 12:10:57 +0100 Subject: import package flag from helix --- parsefuncs.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 parsefuncs.go (limited to 'parsefuncs.go') 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 +} -- cgit v1.2.3