-
Notifications
You must be signed in to change notification settings - Fork 0
/
flag_func.go
61 lines (54 loc) · 1.18 KB
/
flag_func.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
54
55
56
57
58
59
60
61
package cliff
import (
"errors"
"flag"
"github.com/spf13/pflag"
)
// tGoFlag represents all info about a CLI flag except its name.
type tFuncFlag[T any] struct {
tar *T
parser func(string) (T, error)
short string // short alias for the flag
help string // usage message
}
// FuncFlag creates a new flag that is parsed by the given function.
func FuncFlag[T any](
tar *T,
short Short,
def T,
parser func(string) (T, error),
help Help,
) Flag {
shortStr := ""
if short != 0 {
shortStr = string(short)
}
setter := tFuncFlag[T]{
tar: tar,
parser: parser,
short: shortStr,
help: string(help),
}
return Flag{setter: setter}
}
func (f tFuncFlag[T]) AddTo(fs *pflag.FlagSet, name string) error {
if f.short != "" && !isAlNum(f.short) {
return errors.New("flag short name must be an alpha-numeric ASCII character")
}
patched := func(raw string) error {
val, err := f.parser(raw)
if err != nil {
return err
}
*f.tar = val
return nil
}
gfs := flag.NewFlagSet("", flag.ContinueOnError)
gfs.Func(name, f.help, patched)
goflag := gfs.Lookup(name)
pf := pflag.PFlagFromGoFlag(goflag)
pf.Name = name
pf.Shorthand = f.short
fs.AddFlag(pf)
return nil
}