forked from spf13/pflag
-
Notifications
You must be signed in to change notification settings - Fork 2
/
uint16_slice.go
206 lines (177 loc) · 6.18 KB
/
uint16_slice.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
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package pflag
import (
"fmt"
"strconv"
"strings"
)
// -- uint16Slice Value
type uint16SliceValue struct {
value *[]uint16
changed bool
}
func newUint16SliceValue(val []uint16, p *[]uint16) *uint16SliceValue {
isv := new(uint16SliceValue)
isv.value = p
*isv.value = val
return isv
}
func (s *uint16SliceValue) Set(val string) error {
ss := strings.Split(val, ",")
out := make([]uint16, len(ss))
for i, d := range ss {
var err error
var temp64 uint64
temp64, err = strconv.ParseUint(d, 0, 16)
if err != nil {
return err
}
out[i] = uint16(temp64)
}
if !s.changed {
*s.value = out
} else {
*s.value = append(*s.value, out...)
}
s.changed = true
return nil
}
func (s *uint16SliceValue) Type() string {
return "uint16Slice"
}
func (s *uint16SliceValue) String() string {
out := make([]string, len(*s.value))
for i, d := range *s.value {
out[i] = fmt.Sprintf("%d", d)
}
return "[" + strings.Join(out, ",") + "]"
}
func (s *uint16SliceValue) fromString(val string) (uint16, error) {
t64, err := strconv.ParseUint(val, 0, 16)
if err != nil {
return 0, err
}
return uint16(t64), nil
}
func (s *uint16SliceValue) toString(val uint16) string {
return fmt.Sprintf("%d", val)
}
func (s *uint16SliceValue) Append(val string) error {
i, err := s.fromString(val)
if err != nil {
return err
}
*s.value = append(*s.value, i)
return nil
}
func (s *uint16SliceValue) Replace(val []string) error {
out := make([]uint16, len(val))
for i, d := range val {
var err error
out[i], err = s.fromString(d)
if err != nil {
return err
}
}
*s.value = out
return nil
}
func (s *uint16SliceValue) GetSlice() []string {
out := make([]string, len(*s.value))
for i, d := range *s.value {
out[i] = s.toString(d)
}
return out
}
func uint16SliceConv(val string) (interface{}, error) {
val = strings.Trim(val, "[]")
// Empty string would cause a slice with one (empty) entry
if len(val) == 0 {
return []uint16{}, nil
}
ss := strings.Split(val, ",")
out := make([]uint16, len(ss))
for i, d := range ss {
var err error
var temp64 uint64
temp64, err = strconv.ParseUint(d, 0, 16)
if err != nil {
return nil, err
}
out[i] = uint16(temp64)
}
return out, nil
}
// GetUint16Slice return the []uint16 value of a flag with the given name
func (f *FlagSet) GetUint16Slice(name string) ([]uint16, error) {
val, err := f.getFlagType(name, "uint16Slice", uint16SliceConv)
if err != nil {
return []uint16{}, err
}
return val.([]uint16), nil
}
// MustGetUint16Slice is like GetUint16Slice, but panics on error.
func (f *FlagSet) MustGetUint16Slice(name string) []uint16 {
val, err := f.GetUint16Slice(name)
if err != nil {
panic(err)
}
return val
}
// Uint16SliceVar defines a uint16Slice flag with specified name, default value, and usage string.
// The argument p pouints to a []uint16 variable in which to store the value of the flag.
func (f *FlagSet) Uint16SliceVar(p *[]uint16, name string, value []uint16, usage string) {
f.Uint16SliceVarP(p, name, "", value, usage)
}
// Uint16SliceVarP is like Uint16SliceVar, but accepts a shorthand letter that can be used after a single dash.
func (f *FlagSet) Uint16SliceVarP(p *[]uint16, name, shorthand string, value []uint16, usage string) {
f.VarP(newUint16SliceValue(value, p), name, shorthand, usage)
}
// Uint16SliceVarS is like Uint16SliceVar, but accepts a shorthand letter that can be used after a single dash, alone.
func (f *FlagSet) Uint16SliceVarS(p *[]uint16, name, shorthand string, value []uint16, usage string) {
f.VarS(newUint16SliceValue(value, p), name, shorthand, usage)
}
// Uint16SliceVar defines a uint16[] flag with specified name, default value, and usage string.
// The argument p pouints to a uint16[] variable in which to store the value of the flag.
func Uint16SliceVar(p *[]uint16, name string, value []uint16, usage string) {
CommandLine.Uint16SliceVar(p, name, value, usage)
}
// Uint16SliceVarP is like Uint16SliceVar, but accepts a shorthand letter that can be used after a single dash.
func Uint16SliceVarP(p *[]uint16, name, shorthand string, value []uint16, usage string) {
CommandLine.Uint16SliceVarP(p, name, shorthand, value, usage)
}
// Uint16SliceVarS is like Uint16SliceVar, but accepts a shorthand letter that can be used after a single dash, alone.
func Uint16SliceVarS(p *[]uint16, name, shorthand string, value []uint16, usage string) {
CommandLine.Uint16SliceVarS(p, name, shorthand, value, usage)
}
// Uint16Slice defines a []uint16 flag with specified name, default value, and usage string.
// The return value is the address of a []uint16 variable that stores the value of the flag.
func (f *FlagSet) Uint16Slice(name string, value []uint16, usage string) *[]uint16 {
return f.Uint16SliceP(name, "", value, usage)
}
// Uint16SliceP is like Uint16Slice, but accepts a shorthand letter that can be used after a single dash.
func (f *FlagSet) Uint16SliceP(name, shorthand string, value []uint16, usage string) *[]uint16 {
p := []uint16{}
f.Uint16SliceVarP(&p, name, shorthand, value, usage)
return &p
}
// Uint16SliceS is like Uint16Slice, but accepts a shorthand letter that can be used after a single dash, alone.
func (f *FlagSet) Uint16SliceS(name, shorthand string, value []uint16, usage string) *[]uint16 {
p := []uint16{}
f.Uint16SliceVarS(&p, name, shorthand, value, usage)
return &p
}
// Uint16Slice defines a []uint16 flag with specified name, default value, and usage string.
// The return value is the address of a []uint16 variable that stores the value of the flag.
func Uint16Slice(name string, value []uint16, usage string) *[]uint16 {
return CommandLine.Uint16Slice(name, value, usage)
}
// Uint16SliceP is like Uint16Slice, but accepts a shorthand letter that can be used after a single dash.
func Uint16SliceP(name, shorthand string, value []uint16, usage string) *[]uint16 {
return CommandLine.Uint16SliceP(name, shorthand, value, usage)
}
// Uint16SliceS is like Uint16Slice, but accepts a shorthand letter that can be used after a single dash, alone.
func Uint16SliceS(name, shorthand string, value []uint16, usage string) *[]uint16 {
return CommandLine.Uint16SliceS(name, shorthand, value, usage)
}