1 package pflag
2
3 import (
4 "fmt"
5 "strconv"
6 "strings"
7 )
8
9
10 type float32SliceValue struct {
11 value *[]float32
12 changed bool
13 }
14
15 func newFloat32SliceValue(val []float32, p *[]float32) *float32SliceValue {
16 isv := new(float32SliceValue)
17 isv.value = p
18 *isv.value = val
19 return isv
20 }
21
22 func (s *float32SliceValue) Set(val string) error {
23 ss := strings.Split(val, ",")
24 out := make([]float32, len(ss))
25 for i, d := range ss {
26 var err error
27 var temp64 float64
28 temp64, err = strconv.ParseFloat(d, 32)
29 if err != nil {
30 return err
31 }
32 out[i] = float32(temp64)
33
34 }
35 if !s.changed {
36 *s.value = out
37 } else {
38 *s.value = append(*s.value, out...)
39 }
40 s.changed = true
41 return nil
42 }
43
44 func (s *float32SliceValue) Type() string {
45 return "float32Slice"
46 }
47
48 func (s *float32SliceValue) String() string {
49 out := make([]string, len(*s.value))
50 for i, d := range *s.value {
51 out[i] = fmt.Sprintf("%f", d)
52 }
53 return "[" + strings.Join(out, ",") + "]"
54 }
55
56 func (s *float32SliceValue) fromString(val string) (float32, error) {
57 t64, err := strconv.ParseFloat(val, 32)
58 if err != nil {
59 return 0, err
60 }
61 return float32(t64), nil
62 }
63
64 func (s *float32SliceValue) toString(val float32) string {
65 return fmt.Sprintf("%f", val)
66 }
67
68 func (s *float32SliceValue) Append(val string) error {
69 i, err := s.fromString(val)
70 if err != nil {
71 return err
72 }
73 *s.value = append(*s.value, i)
74 return nil
75 }
76
77 func (s *float32SliceValue) Replace(val []string) error {
78 out := make([]float32, len(val))
79 for i, d := range val {
80 var err error
81 out[i], err = s.fromString(d)
82 if err != nil {
83 return err
84 }
85 }
86 *s.value = out
87 return nil
88 }
89
90 func (s *float32SliceValue) GetSlice() []string {
91 out := make([]string, len(*s.value))
92 for i, d := range *s.value {
93 out[i] = s.toString(d)
94 }
95 return out
96 }
97
98 func float32SliceConv(val string) (interface{}, error) {
99 val = strings.Trim(val, "[]")
100
101 if len(val) == 0 {
102 return []float32{}, nil
103 }
104 ss := strings.Split(val, ",")
105 out := make([]float32, len(ss))
106 for i, d := range ss {
107 var err error
108 var temp64 float64
109 temp64, err = strconv.ParseFloat(d, 32)
110 if err != nil {
111 return nil, err
112 }
113 out[i] = float32(temp64)
114
115 }
116 return out, nil
117 }
118
119
120 func (f *FlagSet) GetFloat32Slice(name string) ([]float32, error) {
121 val, err := f.getFlagType(name, "float32Slice", float32SliceConv)
122 if err != nil {
123 return []float32{}, err
124 }
125 return val.([]float32), nil
126 }
127
128
129
130 func (f *FlagSet) Float32SliceVar(p *[]float32, name string, value []float32, usage string) {
131 f.VarP(newFloat32SliceValue(value, p), name, "", usage)
132 }
133
134
135 func (f *FlagSet) Float32SliceVarP(p *[]float32, name, shorthand string, value []float32, usage string) {
136 f.VarP(newFloat32SliceValue(value, p), name, shorthand, usage)
137 }
138
139
140
141 func Float32SliceVar(p *[]float32, name string, value []float32, usage string) {
142 CommandLine.VarP(newFloat32SliceValue(value, p), name, "", usage)
143 }
144
145
146 func Float32SliceVarP(p *[]float32, name, shorthand string, value []float32, usage string) {
147 CommandLine.VarP(newFloat32SliceValue(value, p), name, shorthand, usage)
148 }
149
150
151
152 func (f *FlagSet) Float32Slice(name string, value []float32, usage string) *[]float32 {
153 p := []float32{}
154 f.Float32SliceVarP(&p, name, "", value, usage)
155 return &p
156 }
157
158
159 func (f *FlagSet) Float32SliceP(name, shorthand string, value []float32, usage string) *[]float32 {
160 p := []float32{}
161 f.Float32SliceVarP(&p, name, shorthand, value, usage)
162 return &p
163 }
164
165
166
167 func Float32Slice(name string, value []float32, usage string) *[]float32 {
168 return CommandLine.Float32SliceP(name, "", value, usage)
169 }
170
171
172 func Float32SliceP(name, shorthand string, value []float32, usage string) *[]float32 {
173 return CommandLine.Float32SliceP(name, shorthand, value, usage)
174 }
175
View as plain text