|
| 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