-
Notifications
You must be signed in to change notification settings - Fork 0
/
flagutil.go
53 lines (46 loc) · 1.22 KB
/
flagutil.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package cs
import (
"flag"
"os"
"path"
"strings"
)
// EnvString is a flag.Value that expands environment variables when fetched.
type EnvString string
func (s *EnvString) Set(val string) error {
*s = EnvString(val)
return nil
}
func (s *EnvString) Get() string {
return os.ExpandEnv(string(*s))
}
func (s *EnvString) String() string { return (string)(*s) }
// StringSet is a flag.Value that holds many strings.
type StringSet map[string]bool
func (s *StringSet) Set(val string) error {
val = path.Clean(val)
(*s)[val] = true
return nil
}
func (s *StringSet) String() string {
var paths []string
for p := range *s {
paths = append(paths, p)
}
return strings.Join(paths, ",")
}
// FlagVar is equivalent to flag.Var, but handles allocation of the named type.
func FlagVar[T any, PT interface {
flag.Value
*T
}](name string, value T, usage string) PT {
pv := (PT)(&value)
flag.Var(pv, name, usage)
return pv
}
// FlagFunc is equivalent to flag.Func, but handles allocation of the named type and passing it to the func.
func FlagFunc[T any, PT interface{ *T }](name string, value T, f func(PT, string) error, usage string) PT {
pv := (PT)(&value)
flag.Func(name, usage, func(s string) error { return f(pv, s) })
return pv
}