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