Skip to content

Commit 6ad51b0

Browse files
authored
Add support for go1.15 complex128 type (#19)
1 parent 6ce2386 commit 6ad51b0

File tree

4 files changed

+511
-0
lines changed

4 files changed

+511
-0
lines changed

complex128.go

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
// Use of this source code is governed by a BSD-style
2+
// license that can be found in the LICENSE file.
3+
4+
// +build go1.15
5+
6+
package pflag
7+
8+
import "strconv"
9+
10+
// -- complex128 Value
11+
type complex128Value complex128
12+
13+
func newComplex128Value(val complex128, p *complex128) *complex128Value {
14+
*p = val
15+
return (*complex128Value)(p)
16+
}
17+
18+
func (f *complex128Value) Set(s string) error {
19+
v, err := strconv.ParseComplex(s, 128)
20+
*f = complex128Value(v)
21+
return err
22+
}
23+
24+
func (f *complex128Value) Type() string {
25+
return "complex128"
26+
}
27+
28+
func (f *complex128Value) String() string { return strconv.FormatComplex(complex128(*f), 'g', -1, 128) }
29+
30+
func complex128Conv(sval string) (interface{}, error) {
31+
return strconv.ParseComplex(sval, 128)
32+
}
33+
34+
// GetComplex128 return the complex128 value of a flag with the given name
35+
func (f *FlagSet) GetComplex128(name string) (complex128, error) {
36+
val, err := f.getFlagType(name, "complex128", complex128Conv)
37+
if err != nil {
38+
return 0, err
39+
}
40+
return val.(complex128), nil
41+
}
42+
43+
// MustGetComplex128 is like GetComplex128, but panics on error.
44+
func (f *FlagSet) MustGetComplex128(name string) complex128 {
45+
val, err := f.GetComplex128(name)
46+
if err != nil {
47+
panic(err)
48+
}
49+
return val
50+
}
51+
52+
// Complex128Var defines a complex128 flag with specified name, default value, and usage string.
53+
// The argument p points to a complex128 variable in which to store the value of the flag.
54+
func (f *FlagSet) Complex128Var(p *complex128, name string, value complex128, usage string) {
55+
f.Complex128VarP(p, name, "", value, usage)
56+
}
57+
58+
// Complex128VarP is like Complex128Var, but accepts a shorthand letter that can be used after a single dash.
59+
func (f *FlagSet) Complex128VarP(p *complex128, name, shorthand string, value complex128, usage string) {
60+
f.VarP(newComplex128Value(value, p), name, shorthand, usage)
61+
}
62+
63+
// Complex128VarS is like Complex128Var, but accepts a shorthand letter that can be used after a single dash, alone.
64+
func (f *FlagSet) Complex128VarS(p *complex128, name, shorthand string, value complex128, usage string) {
65+
f.VarS(newComplex128Value(value, p), name, shorthand, usage)
66+
}
67+
68+
// Complex128Var defines a complex128 flag with specified name, default value, and usage string.
69+
// The argument p points to a complex128 variable in which to store the value of the flag.
70+
func Complex128Var(p *complex128, name string, value complex128, usage string) {
71+
CommandLine.Complex128Var(p, name, value, usage)
72+
}
73+
74+
// Complex128VarP is like Complex128Var, but accepts a shorthand letter that can be used after a single dash.
75+
func Complex128VarP(p *complex128, name, shorthand string, value complex128, usage string) {
76+
CommandLine.Complex128VarP(p, name, shorthand, value, usage)
77+
}
78+
79+
// Complex128VarS is like Complex128Var, but accepts a shorthand letter that can be used after a single dash, alone.
80+
func Complex128VarS(p *complex128, name, shorthand string, value complex128, usage string) {
81+
CommandLine.Complex128VarS(p, name, shorthand, value, usage)
82+
}
83+
84+
// Complex128 defines a complex128 flag with specified name, default value, and usage string.
85+
// The return value is the address of a complex128 variable that stores the value of the flag.
86+
func (f *FlagSet) Complex128(name string, value complex128, usage string) *complex128 {
87+
return f.Complex128P(name, "", value, usage)
88+
}
89+
90+
// Complex128P is like Complex128, but accepts a shorthand letter that can be used after a single dash.
91+
func (f *FlagSet) Complex128P(name, shorthand string, value complex128, usage string) *complex128 {
92+
p := new(complex128)
93+
f.Complex128VarP(p, name, shorthand, value, usage)
94+
return p
95+
}
96+
97+
// Complex128S is like Complex128, but accepts a shorthand letter that can be used after a single dash, alone.
98+
func (f *FlagSet) Complex128S(name, shorthand string, value complex128, usage string) *complex128 {
99+
p := new(complex128)
100+
f.Complex128VarS(p, name, shorthand, value, usage)
101+
return p
102+
}
103+
104+
// Complex128 defines a complex128 flag with specified name, default value, and usage string.
105+
// The return value is the address of a complex128 variable that stores the value of the flag.
106+
func Complex128(name string, value complex128, usage string) *complex128 {
107+
return CommandLine.Complex128(name, value, usage)
108+
}
109+
110+
// Complex128P is like Complex128, but accepts a shorthand letter that can be used after a single dash.
111+
func Complex128P(name, shorthand string, value complex128, usage string) *complex128 {
112+
return CommandLine.Complex128P(name, shorthand, value, usage)
113+
}
114+
115+
// Complex128S is like Complex128, but accepts a shorthand letter that can be used after a single dash, alone.
116+
func Complex128S(name, shorthand string, value complex128, usage string) *complex128 {
117+
return CommandLine.Complex128S(name, shorthand, value, usage)
118+
}

complex128_slice.go

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
// Use of this source code is governed by a BSD-style
2+
// license that can be found in the LICENSE file.
3+
4+
// +build go1.15
5+
6+
package pflag
7+
8+
import (
9+
"fmt"
10+
"strconv"
11+
"strings"
12+
)
13+
14+
// -- complex128Slice Value
15+
type complex128SliceValue struct {
16+
value *[]complex128
17+
changed bool
18+
}
19+
20+
func newComplex128SliceValue(val []complex128, p *[]complex128) *complex128SliceValue {
21+
isv := new(complex128SliceValue)
22+
isv.value = p
23+
*isv.value = val
24+
return isv
25+
}
26+
27+
func (s *complex128SliceValue) Set(val string) error {
28+
ss := strings.Split(val, ",")
29+
out := make([]complex128, len(ss))
30+
for i, d := range ss {
31+
var err error
32+
out[i], err = strconv.ParseComplex(d, 128)
33+
if err != nil {
34+
return err
35+
}
36+
37+
}
38+
if !s.changed {
39+
*s.value = out
40+
} else {
41+
*s.value = append(*s.value, out...)
42+
}
43+
s.changed = true
44+
return nil
45+
}
46+
47+
func (s *complex128SliceValue) Type() string {
48+
return "complex128Slice"
49+
}
50+
51+
func (s *complex128SliceValue) String() string {
52+
out := make([]string, len(*s.value))
53+
for i, d := range *s.value {
54+
out[i] = fmt.Sprintf("%f", d)
55+
}
56+
return "[" + strings.Join(out, ",") + "]"
57+
}
58+
59+
func (s *complex128SliceValue) fromString(val string) (complex128, error) {
60+
return strconv.ParseComplex(val, 128)
61+
}
62+
63+
func (s *complex128SliceValue) toString(val complex128) string {
64+
return fmt.Sprintf("%f", val)
65+
}
66+
67+
func (s *complex128SliceValue) Append(val string) error {
68+
i, err := s.fromString(val)
69+
if err != nil {
70+
return err
71+
}
72+
*s.value = append(*s.value, i)
73+
return nil
74+
}
75+
76+
func (s *complex128SliceValue) Replace(val []string) error {
77+
out := make([]complex128, len(val))
78+
for i, d := range val {
79+
var err error
80+
out[i], err = s.fromString(d)
81+
if err != nil {
82+
return err
83+
}
84+
}
85+
*s.value = out
86+
return nil
87+
}
88+
89+
func (s *complex128SliceValue) GetSlice() []string {
90+
out := make([]string, len(*s.value))
91+
for i, d := range *s.value {
92+
out[i] = s.toString(d)
93+
}
94+
return out
95+
}
96+
97+
func complex128SliceConv(val string) (interface{}, error) {
98+
val = strings.Trim(val, "[]")
99+
// Empty string would cause a slice with one (empty) entry
100+
if len(val) == 0 {
101+
return []complex128{}, nil
102+
}
103+
ss := strings.Split(val, ",")
104+
out := make([]complex128, len(ss))
105+
for i, d := range ss {
106+
var err error
107+
out[i], err = strconv.ParseComplex(d, 128)
108+
if err != nil {
109+
return nil, err
110+
}
111+
112+
}
113+
return out, nil
114+
}
115+
116+
// GetComplex128Slice return the []complex128 value of a flag with the given name
117+
func (f *FlagSet) GetComplex128Slice(name string) ([]complex128, error) {
118+
val, err := f.getFlagType(name, "complex128Slice", complex128SliceConv)
119+
if err != nil {
120+
return []complex128{}, err
121+
}
122+
return val.([]complex128), nil
123+
}
124+
125+
// MustGetComplex128Slice is like GetComplex128Slice, but panics on error.
126+
func (f *FlagSet) MustGetComplex128Slice(name string) []complex128 {
127+
val, err := f.GetComplex128Slice(name)
128+
if err != nil {
129+
panic(err)
130+
}
131+
return val
132+
}
133+
134+
// Complex128SliceVar defines a complex128Slice flag with specified name, default value, and usage string.
135+
// The argument p points to a []complex128 variable in which to store the value of the flag.
136+
func (f *FlagSet) Complex128SliceVar(p *[]complex128, name string, value []complex128, usage string) {
137+
f.Complex128SliceVarP(p, name, "", value, usage)
138+
}
139+
140+
// Complex128SliceVarP is like Complex128SliceVar, but accepts a shorthand letter that can be used after a single dash.
141+
func (f *FlagSet) Complex128SliceVarP(p *[]complex128, name, shorthand string, value []complex128, usage string) {
142+
f.VarP(newComplex128SliceValue(value, p), name, shorthand, usage)
143+
}
144+
145+
// Complex128SliceVarS is like Complex128SliceVar, but accepts a shorthand letter that can be used after a single dash, alone.
146+
func (f *FlagSet) Complex128SliceVarS(p *[]complex128, name, shorthand string, value []complex128, usage string) {
147+
f.VarS(newComplex128SliceValue(value, p), name, shorthand, usage)
148+
}
149+
150+
// Complex128SliceVar defines a complex128[] flag with specified name, default value, and usage string.
151+
// The argument p points to a complex128[] variable in which to store the value of the flag.
152+
func Complex128SliceVar(p *[]complex128, name string, value []complex128, usage string) {
153+
CommandLine.Complex128SliceVar(p, name, value, usage)
154+
}
155+
156+
// Complex128SliceVarP is like Complex128SliceVar, but accepts a shorthand letter that can be used after a single dash.
157+
func Complex128SliceVarP(p *[]complex128, name, shorthand string, value []complex128, usage string) {
158+
CommandLine.Complex128SliceVarP(p, name, shorthand, value, usage)
159+
}
160+
161+
// Complex128SliceVarS is like Complex128SliceVar, but accepts a shorthand letter that can be used after a single dash, alone.
162+
func Complex128SliceVarS(p *[]complex128, name, shorthand string, value []complex128, usage string) {
163+
CommandLine.Complex128SliceVarS(p, name, shorthand, value, usage)
164+
}
165+
166+
// Complex128Slice defines a []complex128 flag with specified name, default value, and usage string.
167+
// The return value is the address of a []complex128 variable that stores the value of the flag.
168+
func (f *FlagSet) Complex128Slice(name string, value []complex128, usage string) *[]complex128 {
169+
return f.Complex128SliceP(name, "", value, usage)
170+
}
171+
172+
// Complex128SliceP is like Complex128Slice, but accepts a shorthand letter that can be used after a single dash.
173+
func (f *FlagSet) Complex128SliceP(name, shorthand string, value []complex128, usage string) *[]complex128 {
174+
p := []complex128{}
175+
f.Complex128SliceVarP(&p, name, shorthand, value, usage)
176+
return &p
177+
}
178+
179+
// Complex128SliceS is like Complex128Slice, but accepts a shorthand letter that can be used after a single dash, alone.
180+
func (f *FlagSet) Complex128SliceS(name, shorthand string, value []complex128, usage string) *[]complex128 {
181+
p := []complex128{}
182+
f.Complex128SliceVarS(&p, name, shorthand, value, usage)
183+
return &p
184+
}
185+
186+
// Complex128Slice defines a []complex128 flag with specified name, default value, and usage string.
187+
// The return value is the address of a []complex128 variable that stores the value of the flag.
188+
func Complex128Slice(name string, value []complex128, usage string) *[]complex128 {
189+
return CommandLine.Complex128Slice(name, value, usage)
190+
}
191+
192+
// Complex128SliceP is like Complex128Slice, but accepts a shorthand letter that can be used after a single dash.
193+
func Complex128SliceP(name, shorthand string, value []complex128, usage string) *[]complex128 {
194+
return CommandLine.Complex128SliceP(name, shorthand, value, usage)
195+
}
196+
197+
// Complex128SliceS is like Complex128Slice, but accepts a shorthand letter that can be used after a single dash, alone.
198+
func Complex128SliceS(name, shorthand string, value []complex128, usage string) *[]complex128 {
199+
return CommandLine.Complex128SliceS(name, shorthand, value, usage)
200+
}

0 commit comments

Comments
 (0)