-
Notifications
You must be signed in to change notification settings - Fork 4
/
wrap.go
162 lines (145 loc) · 3.11 KB
/
wrap.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package flagvar
import (
"flag"
"fmt"
"strings"
)
// WrapPointer wraps a pointer to a `flag.Value`
// This can be used to switch between different argument parsers.
type WrapPointer struct {
Value *flag.Value
}
// Help returns a string suitable for inclusion in a flag help message.
func (fv *WrapPointer) Help() string {
if fv.Value == nil {
return ""
}
if helper, ok := (*fv.Value).(interface {
Help() string
}); ok {
return helper.Help()
}
return ""
}
// Set is flag.Value.Set
func (fv *WrapPointer) Set(v string) error {
return (*fv.Value).Set(v)
}
func (fv WrapPointer) String() string {
if fv.Value == nil || *fv.Value == nil {
return ""
}
return (*fv.Value).String()
}
// WrapFunc wraps a nullary function returning a `flag.Value`
// This can be used to switch between different argument parsers.
type WrapFunc func() flag.Value
// Help returns a string suitable for inclusion in a flag help message.
func (fv WrapFunc) Help() string {
if fv == nil {
return ""
}
if helper, ok := fv().(interface {
Help() string
}); ok {
return helper.Help()
}
return ""
}
// Set is flag.Value.Set
func (fv WrapFunc) Set(v string) error {
return fv().Set(v)
}
func (fv WrapFunc) String() string {
if fv == nil {
return ""
}
return fv().String()
}
// Wrap wraps a `flag.Value` and calls `Updated` each time the underlying value is set.
type Wrap struct {
Value flag.Value
Updated func()
}
// Help returns a string suitable for inclusion in a flag help message.
func (fv *Wrap) Help() string {
if fv.Value == nil {
return ""
}
if helper, ok := fv.Value.(interface {
Help() string
}); ok {
return helper.Help()
}
return ""
}
// Set is flag.Value.Set
func (fv *Wrap) Set(v string) error {
err := fv.Value.Set(v)
if err == nil {
fv.Updated()
}
return err
}
func (fv *Wrap) String() string {
if fv.Value == nil {
return ""
}
return fv.Value.String()
}
// WrapCSV wraps a `flag.Value` and calls `UpdatedOne` after each single value and `UpdatedAll` after each CSV batch.
// The `Separator` field is used instead of the comma when set.
type WrapCSV struct {
Value flag.Value
Separator string
UpdatedOne func()
UpdatedAll func()
StringFunc func() string
}
// Help returns a string suitable for inclusion in a flag help message.
func (fv *WrapCSV) Help() string {
if fv.Value == nil {
return ""
}
separator := fv.Separator
if separator == "" {
separator = ","
}
if helper, ok := fv.Value.(interface {
Help() string
}); ok {
return fmt.Sprintf("%q-separated values, each value %s", separator, helper.Help())
}
return ""
}
// Set is flag.Value.Set
func (fv *WrapCSV) Set(v string) error {
separator := fv.Separator
if separator == "" {
separator = ","
}
parts := strings.Split(v, separator)
for _, part := range parts {
part = strings.TrimSpace(part)
err := fv.Value.Set(part)
if err != nil {
return err
}
if fv.UpdatedOne != nil {
fv.UpdatedOne()
}
}
if fv.UpdatedAll != nil {
fv.UpdatedAll()
}
return nil
}
func (fv *WrapCSV) String() string {
if fv.StringFunc != nil {
return fv.StringFunc()
}
if fv.Value == nil {
return ""
}
return fv.Value.String()
}