-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple.go
More file actions
68 lines (55 loc) · 1.65 KB
/
simple.go
File metadata and controls
68 lines (55 loc) · 1.65 KB
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
62
63
64
65
66
67
68
package flagutils
import "github.com/spf13/pflag"
type VarPFunc[V any] = func(fs *pflag.FlagSet, p *V, name, shorthand string, value V, usage string)
////////////////////////////////////////////////////////////////////////////////
type SimpleOption[V any, T Options] struct {
self T
value V
setter VarPFunc[V]
long string
short string
desc string
}
func NewSimpleOption[V any, T Options](self T, def V, long, short, desc string) SimpleOption[V, T] {
return SimpleOption[V, T]{self: self, setter: VarPFuncFor[V](), value: def, long: long, short: short, desc: desc}
}
func NewSimpleOptionWithSetter[V any, T Options](self T, setter VarPFunc[V], def V, long, short, desc string) SimpleOption[V, T] {
return SimpleOption[V, T]{self: self, setter: setter, value: def, long: long, short: short, desc: desc}
}
func (o *SimpleOption[V, T]) AddFlags(fs *pflag.FlagSet) {
o.setter(fs, &o.value, o.long, o.short, o.value, o.desc)
}
func (o *SimpleOption[V, T]) Value() V {
return o.value
}
func (o *SimpleOption[V, T]) Set(v V) T {
o.value = v
return o.self
}
func (o *SimpleOption[V, T]) WithNames(l, s string) T {
o.long = l
o.short = s
return o.self
}
func (o *SimpleOption[V, T]) WithDescription(s string) T {
o.desc = s
return o.self
}
////////////////////////////////////////////////////////////////////////////////
func VarPFuncFor[T any]() VarPFunc[T] {
var v T
var r any
switch any(v).(type) {
case string:
r = (*pflag.FlagSet).StringVarP
case int:
r = (*pflag.FlagSet).IntVarP
case bool:
r = (*pflag.FlagSet).BoolVarP
case []string:
r = (*pflag.FlagSet).StringSliceVarP
default:
panic("unsupported type")
}
return r.(VarPFunc[T])
}