-
Notifications
You must be signed in to change notification settings - Fork 13
/
flag.go
343 lines (290 loc) · 7.25 KB
/
flag.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
package captain
import (
"bytes"
"fmt"
"reflect"
"strconv"
"strings"
"github.com/ActiveState/cli/internal/errs"
"github.com/spf13/pflag"
)
type FlagMarshaler pflag.Value
// Flag is used to define flags in our Command struct
type Flag struct {
Name string
Shorthand string
Description string
Persist bool
Value interface{}
Hidden bool
OnUse func()
}
func (c *Command) setFlags(flags []*Flag) error {
c.flags = flags
for _, flag := range flags {
flagSetter := c.cobra.Flags
if flag.Persist {
flagSetter = c.cobra.PersistentFlags
}
switch v := flag.Value.(type) {
case nil:
return errs.New("flag value must not be nil (%v)", flag)
case *[]string:
flagSetter().StringSliceVarP(
v, flag.Name, flag.Shorthand, *v, flag.Description,
)
case *string:
flagSetter().StringVarP(
v, flag.Name, flag.Shorthand, *v, flag.Description,
)
case *int:
flagSetter().IntVarP(
v, flag.Name, flag.Shorthand, *v, flag.Description,
)
case *bool:
flagSetter().BoolVarP(
v, flag.Name, flag.Shorthand, *v, flag.Description,
)
case FlagMarshaler:
flagSetter().VarP(
v, flag.Name, flag.Shorthand, flag.Description,
)
default:
return errs.New(
fmt.Sprintf("Unknown type for flag %s: %s (%v)", flag.Name, reflect.TypeOf(v).Name(), v),
)
}
if flag.Hidden {
if err := flagSetter().MarkHidden(flag.Name); err != nil {
return errs.Wrap(err, "markFlagHidden %s failed", flag.Name)
}
}
}
return nil
}
type NullString struct {
s string
isSet bool
}
func (s *NullString) String() string {
if s.isSet {
return s.s
}
return ""
}
func (s *NullString) Set(v string) error {
s.s = v
s.isSet = true
return nil
}
func (s *NullString) Type() string {
return "null string"
}
func (s *NullString) IsSet() bool {
return s.isSet
}
func (s *NullString) AsPtrTo() *string {
if !s.isSet {
return nil
}
v := s.s
return &v
}
type NullInt struct {
n int
isSet bool
}
func (n *NullInt) String() string {
if n.isSet {
return strconv.FormatInt(int64(n.n), 10)
}
return ""
}
func (n *NullInt) Set(v string) error {
x, err := strconv.Atoi(v)
if err != nil {
return fmt.Errorf("null int set: %w", err)
}
n.n = x
n.isSet = true
return nil
}
func (n *NullInt) Type() string {
return "null int"
}
func (n *NullInt) IsSet() bool {
return n.isSet
}
func (n *NullInt) AsPtrTo() *int {
if !n.isSet {
return nil
}
v := n.n
return &v
}
type NullBool struct {
b bool
isSet bool
}
func (b *NullBool) String() string {
if b.isSet {
return fmt.Sprintf("%t", b.b)
}
return ""
}
func (b *NullBool) Set(v string) error {
b.b = strings.ToLower(v) == "true"
if !b.b && strings.ToLower(v) != "false" {
return fmt.Errorf("null bool set: %q is not a valid value", v)
}
b.isSet = true
return nil
}
func (b *NullBool) Type() string {
return "null bool"
}
func (b *NullBool) IsSet() bool {
return b.isSet
}
func (b *NullBool) AsPtrTo() *bool {
if !b.isSet {
return nil
}
v := b.b
return &v
}
// LocalFlagUsages is a substitute for c.cobra.LocalFlags().FlagUsages() to replace Cobra's
// intuited flag value placeholders with "<value>".
func (c *Command) LocalFlagUsages() string {
return flagUsagesWrapped(c.cobra.LocalFlags(), 0)
}
// InheritedFlagUsages is a substitute for c.cobra.InheritedFlags().FlagUsages() to replace Cobra's
// intuited flag value placeholders with "<value>".
func (c *Command) InheritedFlagUsages() string {
return flagUsagesWrapped(c.cobra.InheritedFlags(), 0)
}
// flagUsagesWrapped is a copy of pflag.FlagSet.FlagUsagesWrapped(), but ignores Cobra's intuited
// flag value placeholders and uses "<value>" instead.
func flagUsagesWrapped(f *pflag.FlagSet, cols int) string {
buf := new(bytes.Buffer)
lines := make([]string, 0)
maxlen := 0
f.VisitAll(func(flag *pflag.Flag) {
if flag.Hidden {
return
}
line := ""
if flag.Shorthand != "" && flag.ShorthandDeprecated == "" {
line = fmt.Sprintf(" -%s, --%s", flag.Shorthand, flag.Name)
} else {
line = fmt.Sprintf(" --%s", flag.Name)
}
// varname, usage := UnquoteUsage(flag)
varname := ""
if flag.Value.Type() != "bool" {
varname = "<value>"
}
usage := flag.Usage
if varname != "" {
line += " " + varname
}
if flag.NoOptDefVal != "" {
switch flag.Value.Type() {
case "string":
line += fmt.Sprintf("[=\"%s\"]", flag.NoOptDefVal)
case "bool":
if flag.NoOptDefVal != "true" {
line += fmt.Sprintf("[=%s]", flag.NoOptDefVal)
}
case "count":
if flag.NoOptDefVal != "+1" {
line += fmt.Sprintf("[=%s]", flag.NoOptDefVal)
}
default:
line += fmt.Sprintf("[=%s]", flag.NoOptDefVal)
}
}
// This special character will be replaced with spacing once the
// correct alignment is calculated
line += "\x00"
if len(line) > maxlen {
maxlen = len(line)
}
line += usage
//if !flag.defaultIsZeroValue() {
// if flag.Value.Type() == "string" {
// line += fmt.Sprintf(" (default %q)", flag.DefValue)
// } else {
// line += fmt.Sprintf(" (default %s)", flag.DefValue)
// }
//}
if len(flag.Deprecated) != 0 {
line += fmt.Sprintf(" (DEPRECATED: %s)", flag.Deprecated)
}
lines = append(lines, line)
})
for _, line := range lines {
sidx := strings.Index(line, "\x00")
spacing := strings.Repeat(" ", maxlen-sidx)
// maxlen + 2 comes from + 1 for the \x00 and + 1 for the (deliberate) off-by-one in maxlen-sidx
fmt.Fprintln(buf, line[:sidx], spacing, wrap(maxlen+2, cols, line[sidx+1:]))
}
return buf.String()
}
// Splits the string `s` on whitespace into an initial substring up to
// `i` runes in length and the remainder. Will go `slop` over `i` if
// that encompasses the entire string (which allows the caller to
// avoid short orphan words on the final line).
func wrapN(i, slop int, s string) (string, string) {
if i+slop > len(s) {
return s, ""
}
w := strings.LastIndexAny(s[:i], " \t\n")
if w <= 0 {
return s, ""
}
nlPos := strings.LastIndex(s[:i], "\n")
if nlPos > 0 && nlPos < w {
return s[:nlPos], s[nlPos+1:]
}
return s[:w], s[w+1:]
}
// Wraps the string `s` to a maximum width `w` with leading indent
// `i`. The first line is not indented (this is assumed to be done by
// caller). Pass `w` == 0 to do no wrapping
func wrap(i, w int, s string) string {
if w == 0 {
return strings.Replace(s, "\n", "\n"+strings.Repeat(" ", i), -1)
}
// space between indent i and end of line width w into which
// we should wrap the text.
wrap := w - i
var r, l string
// Not enough space for sensible wrapping. Wrap as a block on
// the next line instead.
if wrap < 24 {
i = 16
wrap = w - i
r += "\n" + strings.Repeat(" ", i)
}
// If still not enough space then don't even try to wrap.
if wrap < 24 {
return strings.Replace(s, "\n", r, -1)
}
// Try to avoid short orphan words on the final line, by
// allowing wrapN to go a bit over if that would fit in the
// remainder of the line.
slop := 5
wrap = wrap - slop
// Handle first line, which is indented by the caller (or the
// special case above)
l, s = wrapN(wrap, slop, s)
r = r + strings.Replace(l, "\n", "\n"+strings.Repeat(" ", i), -1)
// Now wrap the rest
for s != "" {
var t string
t, s = wrapN(wrap, slop, s)
r = r + "\n" + strings.Repeat(" ", i) + strings.Replace(t, "\n", "\n"+strings.Repeat(" ", i), -1)
}
return r
}