-
Notifications
You must be signed in to change notification settings - Fork 0
/
flag.gp_slice_uint64.go
391 lines (342 loc) · 11.1 KB
/
flag.gp_slice_uint64.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
///////////////////////////////////////////////////////////////////
//
// !!!!!!!!!!!! NEVER MODIFY THIS FILE MANUALLY !!!!!!!!!!!!
//
// This file was auto-generated by tool [github.com/gxlb/gogp]
// Last update at: [Sat Mar 20 2021 22:32 CST]
// Generate from:
// [github.com/gxlb/cli/internal/gp/flag.gp]
// [github.com/gxlb/cli/flag.gpg] [flag_uint64_slice]
//
// Tool [github.com/gxlb/gogp] info:
// CopyRight 2021 @Ally Dale. All rights reserved.
// Author : Ally Dale([email protected])
// Site : https://github.com/vipally
// Version : v4.0.0
//
///////////////////////////////////////////////////////////////////
// MIT License
//
// Copyright (c) 2021 Ally Dale <[email protected]>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package cli
import (
"flag"
"fmt"
"cli/internal/impl"
"cli/internal/util"
"encoding/json"
"strconv"
"strings"
)
// Uint64SliceValue wraps []uint64 to satisfy flag.Value
type Uint64SliceValue struct {
slice []uint64
hasBeenSet bool
}
// NewUint64SliceValue makes an *Uint64SliceValue with default values
func NewUint64SliceValue(defaults ...uint64) *Uint64SliceValue {
return &Uint64SliceValue{
slice: append([]uint64{}, defaults...),
hasBeenSet: false,
}
}
// clone allocate a copy of self object
func (s *Uint64SliceValue) clone() *Uint64SliceValue {
n := &Uint64SliceValue{
slice: append([]uint64{}, s.slice...),
hasBeenSet: s.hasBeenSet,
}
return n
}
// AppendValues directly append values to the list of values
func (s *Uint64SliceValue) AppendValues(values ...uint64) {
s.setValues(false, values)
}
// SetValues directly overite values to the list of values
func (s *Uint64SliceValue) SetValues(values ...uint64) {
s.setValues(true, values)
}
func (s *Uint64SliceValue) setValues(overwrite bool, values []uint64) {
if !s.hasBeenSet || overwrite {
s.Reset()
s.hasBeenSet = true
}
s.slice = append(s.slice, values...)
}
// Set parses the value and appends to the list of values
func (s *Uint64SliceValue) Set(value string) error {
if strings.HasPrefix(value, impl.SerializedPrefix) {
// Deserializing assumes overwrite
_ = json.Unmarshal([]byte(strings.Replace(value, impl.SerializedPrefix, "", 1)), &s.slice)
s.hasBeenSet = true
return nil
}
//accept multi values for slice flags
for _, val := range impl.FlagSplitMultiValues(value) {
value := strings.TrimSpace(val)
tmp, err := strconv.ParseUint(value, 0, 64)
if err != nil {
return err
}
if !s.hasBeenSet {
s.slice = []uint64{}
s.hasBeenSet = true
}
s.slice = append(s.slice, uint64(tmp))
}
return nil
}
// Reset clean the last parsed values of this slice
func (s *Uint64SliceValue) Reset() {
if s.slice == nil {
s.slice = []uint64{}
} else {
s.slice = s.slice[:0]
}
s.hasBeenSet = false
}
// String returns a readable representation of this value (for usage defaults)
func (s *Uint64SliceValue) String() string {
return fmt.Sprintf("%#v", s.slice)
}
// Serialize allows Uint64SliceSlice to fulfill Serializer
func (s *Uint64SliceValue) Serialize() string {
jsonBytes, _ := json.Marshal(s.slice)
return fmt.Sprintf("%s%s", impl.SerializedPrefix, string(jsonBytes))
}
// Value returns the slice of ints set by this flag
func (s *Uint64SliceValue) Value() []uint64 {
return s.slice
}
// Get returns the slice set by this flag
func (s *Uint64SliceValue) Get() interface{} {
return *s
}
// Uint64SliceFlag define a value of type *Uint64SliceValue
type Uint64SliceFlag struct {
//
//name related area
//
LogicName string // logic name of the flag
Name string // name of the flag
Aliases []string // aliases of the flag
Usage string // usage string
Required bool // if required
Hidden bool // hidden this flag
EnvVars []string // environment values
FilePath string // file path
//
//value related area
//
Target *Uint64SliceValue // Target value pointer outside
Default *Uint64SliceValue // Default value
DefaultText string // Default value display in help info
Enums []uint64 // Enumeration of valid values
Ranges []uint64 // {[min,max),[min,max),...} ranges of valid values
//
////////////////////////////////////////////////////////////////////////////
//area for parsing
target *Uint64SliceValue // target value pointer(maybe new(Uint64SliceValue) if Target not set)
info impl.FlagInfo // parsed info of this flag
}
// init verify and init the flag info
func (f *Uint64SliceFlag) init(namegen *util.NameGenenerator) error {
f.info.Flag = f
f.info.EnvVars = append([]string{}, f.EnvVars...)
f.info.Usage = f.Usage
f.info.DefaultText = f.DefaultText
f.info.Required = f.Required
f.info.Hidden = f.Hidden
f.info.FilePath = f.FilePath
f.info.HasBeenSet = false
f.info.Name = namegen.GetOrGenName(f.Name)
f.info.NonameFlag = f.info.Name != f.Name
f.info.LogicName = impl.FlagLogicName(f.Name, f.LogicName)
f.info.ValueName = impl.FlagValueName(f.LogicName)
impl.MergeNames(f.info.Name, f.Aliases, &f.info.Names) //use v.info.Name to enable auto-generated name
//make the target pointer
if f.Target != nil {
f.target = f.Target
} else {
f.target = NewUint64SliceValue()
}
if f.Name == "" && f.LogicName == "" { // Name & LogicName cannot both missing
return fmt.Errorf("flag missing both Name & LogicName: %v", f)
}
if f.Name == "" && len(f.Aliases) > 0 { // Noname ones must without Aliases
return fmt.Errorf("flag %s missing name, but has Aliases %v", f.info.LogicName, f.Aliases)
}
maxSliceLen := impl.MaxSliceLen
if l := len(f.Enums); l > 0 { // Enums length check
if l > maxSliceLen {
return fmt.Errorf("flag %s.Enums too long: %d/%d", f.info.LogicName, l, maxSliceLen)
}
if l > 1 {
var filter = make(map[uint64]struct{})
for _, v := range f.Enums {
if i, ok := filter[v]; !ok {
filter[v] = struct{}{}
} else {
return fmt.Errorf("flag %s.Enums error: duplicate %v at %d", f.info.LogicName, v, i)
}
}
}
}
if l := len(f.Ranges); l > 0 { // Ranges length check and [min,max) pair check
if l > maxSliceLen {
return fmt.Errorf("flag %s.Ranges too long: %d/%d", f.info.LogicName, l, maxSliceLen)
}
if l%2 != 0 {
return fmt.Errorf("flag %s.Ranges doesn't match [min,max) pairs: %d", f.info.LogicName, l)
}
for i := 0; i < l; i += 2 {
min, max := f.Ranges[i], f.Ranges[i+1]
if valid := min <= max; !valid {
return fmt.Errorf("flag %s.Ranges doesn't match [min,max): (%v,%v)", f.info.LogicName, min, max)
}
for j := 0; j < i; j += 2 { //check range overlapping
m, n := f.Ranges[j], f.Ranges[j+1]
if m >= min && m < max || n >= min && n < max {
return fmt.Errorf("flag %s.Ranges %d~[%v,%v) overlapping %d~[%v,%v) ", f.info.LogicName, i, min, max, j, m, n)
}
}
}
}
if err := f.validateValues(f.Default); err != nil { // verify default values
return fmt.Errorf("default value invalid: %s", err.Error())
}
if err := util.FiltNames(f.info.Names); err != nil { // verify name duplicate
return fmt.Errorf("flag %s.Names error: %s", f.info.LogicName, err.Error())
}
if err := util.FiltNames(f.info.EnvVars); err != nil { // verify EnvVars duplicate
return fmt.Errorf("flag %s.EnvVars error: %s", f.info.LogicName, err.Error())
}
return nil
}
// IsSet check if value was set
func (f *Uint64SliceFlag) IsSet() bool {
return f.target.hasBeenSet
}
//GetLogicName returns the logic name of the falg
func (f *Uint64SliceFlag) GetLogicName() string {
return f.info.LogicName
}
//GetValueName returns the value name of the falg
func (f *Uint64SliceFlag) GetValueName() string {
return f.info.ValueName
}
// Names returns the names of the flag
func (f *Uint64SliceFlag) Names() []string {
return f.info.Names
}
// IsRequired returns whether or not the flag is required
func (f *Uint64SliceFlag) IsRequired() bool {
return f.Required
}
// TakesValue returns true of the flag takes a value, otherwise false
func (f *Uint64SliceFlag) TakesValue() bool {
return false
}
// GetUsage returns the usage string for the flag
func (f *Uint64SliceFlag) GetUsage() string {
return f.info.Usage
}
// GetValue returns the flags value as string representation.
func (f *Uint64SliceFlag) GetValue() string {
return ""
}
// Apply coordinate the value to flagset
func (f *Uint64SliceFlag) Apply(set *flag.FlagSet) error {
return nil
}
// String return the value for view
func (f *Uint64SliceFlag) String() string {
return ""
}
// ValidateValues verify if all values are valid
func (f *Uint64SliceFlag) ValidateValues() error {
return f.validateValues(f.target)
}
// Info returns parsed info of this flag, the returned object must READ-ONLY
func (v *Uint64SliceFlag) Info() *impl.FlagInfo {
return &v.info
}
// Reset clean the last parsed value of this flag
func (f *Uint64SliceFlag) Reset() {
f.target.Reset()
f.info.HasBeenSet = false
}
// for default value verify
func (f *Uint64SliceFlag) validateValues(values *Uint64SliceValue) error {
for _, val := range values.slice {
if err := f.validValue(val); err != nil {
return err
}
}
return nil
}
// check if value if valid for this flag
func (f *Uint64SliceFlag) validValue(value uint64) error {
if len(f.Enums) > 0 {
found := false
for _, v := range f.Enums {
if value == v {
found = true
break
}
}
if !found {
return fmt.Errorf("flag %s value %v out of Enums: %v", f.info.LogicName, value, f.Enums)
}
}
if len(f.Ranges) > 0 {
found := false
for i := 0; i < len(f.Ranges); i += 2 {
min, max := f.Ranges[i], f.Ranges[i+1]
if value >= min && value < max {
found = true
break
}
}
if !found {
return fmt.Errorf("flag %s value %v out of Ranges: %v", f.info.LogicName, value, f.Enums)
}
}
return nil
}
// Uint64Slice looks up the value of a local Uint64SliceFlag
func (c *Context) Uint64Slice(name string) []uint64 {
if fs := lookupFlagSet(name, c); fs != nil {
return lookupUint64Slice(name, fs)
}
return nil
}
func lookupUint64Slice(name string, set *flag.FlagSet) []uint64 {
f := set.Lookup(name)
if f != nil {
if slice, ok := f.Value.(*Uint64SliceValue); ok {
return slice.Value()
}
}
return nil
}
var _ impl.Flag = (*Uint64SliceFlag)(nil) //for interface verification only
var _ = (*strconv.NumError)(nil) //avoid compile error