forked from spf13/pflag
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcomplex128_slice.go
200 lines (170 loc) · 6.5 KB
/
complex128_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
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build go1.15
package pflag
import (
"fmt"
"strconv"
"strings"
)
// -- complex128Slice Value
type complex128SliceValue struct {
value *[]complex128
changed bool
}
func newComplex128SliceValue(val []complex128, p *[]complex128) *complex128SliceValue {
isv := new(complex128SliceValue)
isv.value = p
*isv.value = val
return isv
}
func (s *complex128SliceValue) Set(val string) error {
ss := strings.Split(val, ",")
out := make([]complex128, len(ss))
for i, d := range ss {
var err error
out[i], err = strconv.ParseComplex(d, 128)
if err != nil {
return err
}
}
if !s.changed {
*s.value = out
} else {
*s.value = append(*s.value, out...)
}
s.changed = true
return nil
}
func (s *complex128SliceValue) Type() string {
return "complex128Slice"
}
func (s *complex128SliceValue) String() string {
out := make([]string, len(*s.value))
for i, d := range *s.value {
out[i] = fmt.Sprintf("%f", d)
}
return "[" + strings.Join(out, ",") + "]"
}
func (s *complex128SliceValue) fromString(val string) (complex128, error) {
return strconv.ParseComplex(val, 128)
}
func (s *complex128SliceValue) toString(val complex128) string {
return fmt.Sprintf("%f", val)
}
func (s *complex128SliceValue) Append(val string) error {
i, err := s.fromString(val)
if err != nil {
return err
}
*s.value = append(*s.value, i)
return nil
}
func (s *complex128SliceValue) Replace(val []string) error {
out := make([]complex128, 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 *complex128SliceValue) GetSlice() []string {
out := make([]string, len(*s.value))
for i, d := range *s.value {
out[i] = s.toString(d)
}
return out
}
func complex128SliceConv(val string) (interface{}, error) {
val = strings.Trim(val, "[]")
// Empty string would cause a slice with one (empty) entry
if len(val) == 0 {
return []complex128{}, nil
}
ss := strings.Split(val, ",")
out := make([]complex128, len(ss))
for i, d := range ss {
var err error
out[i], err = strconv.ParseComplex(d, 128)
if err != nil {
return nil, err
}
}
return out, nil
}
// GetComplex128Slice return the []complex128 value of a flag with the given name
func (f *FlagSet) GetComplex128Slice(name string) ([]complex128, error) {
val, err := f.getFlagType(name, "complex128Slice", complex128SliceConv)
if err != nil {
return []complex128{}, err
}
return val.([]complex128), nil
}
// MustGetComplex128Slice is like GetComplex128Slice, but panics on error.
func (f *FlagSet) MustGetComplex128Slice(name string) []complex128 {
val, err := f.GetComplex128Slice(name)
if err != nil {
panic(err)
}
return val
}
// Complex128SliceVar defines a complex128Slice flag with specified name, default value, and usage string.
// The argument p points to a []complex128 variable in which to store the value of the flag.
func (f *FlagSet) Complex128SliceVar(p *[]complex128, name string, value []complex128, usage string) {
f.Complex128SliceVarP(p, name, "", value, usage)
}
// Complex128SliceVarP is like Complex128SliceVar, but accepts a shorthand letter that can be used after a single dash.
func (f *FlagSet) Complex128SliceVarP(p *[]complex128, name, shorthand string, value []complex128, usage string) {
f.VarP(newComplex128SliceValue(value, p), name, shorthand, usage)
}
// Complex128SliceVarS is like Complex128SliceVar, but accepts a shorthand letter that can be used after a single dash, alone.
func (f *FlagSet) Complex128SliceVarS(p *[]complex128, name, shorthand string, value []complex128, usage string) {
f.VarS(newComplex128SliceValue(value, p), name, shorthand, usage)
}
// Complex128SliceVar defines a complex128[] flag with specified name, default value, and usage string.
// The argument p points to a complex128[] variable in which to store the value of the flag.
func Complex128SliceVar(p *[]complex128, name string, value []complex128, usage string) {
CommandLine.Complex128SliceVar(p, name, value, usage)
}
// Complex128SliceVarP is like Complex128SliceVar, but accepts a shorthand letter that can be used after a single dash.
func Complex128SliceVarP(p *[]complex128, name, shorthand string, value []complex128, usage string) {
CommandLine.Complex128SliceVarP(p, name, shorthand, value, usage)
}
// Complex128SliceVarS is like Complex128SliceVar, but accepts a shorthand letter that can be used after a single dash, alone.
func Complex128SliceVarS(p *[]complex128, name, shorthand string, value []complex128, usage string) {
CommandLine.Complex128SliceVarS(p, name, shorthand, value, usage)
}
// Complex128Slice defines a []complex128 flag with specified name, default value, and usage string.
// The return value is the address of a []complex128 variable that stores the value of the flag.
func (f *FlagSet) Complex128Slice(name string, value []complex128, usage string) *[]complex128 {
return f.Complex128SliceP(name, "", value, usage)
}
// Complex128SliceP is like Complex128Slice, but accepts a shorthand letter that can be used after a single dash.
func (f *FlagSet) Complex128SliceP(name, shorthand string, value []complex128, usage string) *[]complex128 {
p := []complex128{}
f.Complex128SliceVarP(&p, name, shorthand, value, usage)
return &p
}
// Complex128SliceS is like Complex128Slice, but accepts a shorthand letter that can be used after a single dash, alone.
func (f *FlagSet) Complex128SliceS(name, shorthand string, value []complex128, usage string) *[]complex128 {
p := []complex128{}
f.Complex128SliceVarS(&p, name, shorthand, value, usage)
return &p
}
// Complex128Slice defines a []complex128 flag with specified name, default value, and usage string.
// The return value is the address of a []complex128 variable that stores the value of the flag.
func Complex128Slice(name string, value []complex128, usage string) *[]complex128 {
return CommandLine.Complex128Slice(name, value, usage)
}
// Complex128SliceP is like Complex128Slice, but accepts a shorthand letter that can be used after a single dash.
func Complex128SliceP(name, shorthand string, value []complex128, usage string) *[]complex128 {
return CommandLine.Complex128SliceP(name, shorthand, value, usage)
}
// Complex128SliceS is like Complex128Slice, but accepts a shorthand letter that can be used after a single dash, alone.
func Complex128SliceS(name, shorthand string, value []complex128, usage string) *[]complex128 {
return CommandLine.Complex128SliceS(name, shorthand, value, usage)
}