aboutsummaryrefslogtreecommitdiff
path: root/flag.go
diff options
context:
space:
mode:
authorGrégoire Duchêne <gduchene@awhk.org>2023-02-26 13:19:02 +0000
committerGrégoire Duchêne <gduchene@awhk.org>2023-02-26 13:19:02 +0000
commitef1ec36b42350d9abae5ea533ce38bf37d013ffc (patch)
tree2e7186de5a189e77e6677d2449024b35b43cb823 /flag.go
parent1ccf64c35e6cd5b335f8b230844712b6fb026dca (diff)
Add ParseStringerEnumv0.6.0
Diffstat (limited to 'flag.go')
-rw-r--r--flag.go14
1 files changed, 14 insertions, 0 deletions
diff --git a/flag.go b/flag.go
index f9d048e..96c74d2 100644
--- a/flag.go
+++ b/flag.go
@@ -184,6 +184,20 @@ func ParseStringEnum(values ...string) ParseFunc[string] {
}
}
+// ParseStringerEnum returns a ParseFunc that will return the first
+// value having a string value matching the string passed.
+func ParseStringerEnum[T fmt.Stringer](values ...T) ParseFunc[T] {
+ return func(s string) (T, error) {
+ for _, val := range values {
+ if s == val.String() {
+ return val, nil
+ }
+ }
+ var zero T
+ return zero, UnknownEnumValueError[T]{s, values}
+ }
+}
+
// ParseTime parses a string according to the time.RFC3339 format.
func ParseTime(s string) (time.Time, error) {
return time.Parse(time.RFC3339, s)