Path: blob/main/vendor/github.com/spf13/pflag/float32_slice.go
2875 views
package pflag12import (3"fmt"4"strconv"5"strings"6)78// -- float32Slice Value9type float32SliceValue struct {10value *[]float3211changed bool12}1314func newFloat32SliceValue(val []float32, p *[]float32) *float32SliceValue {15isv := new(float32SliceValue)16isv.value = p17*isv.value = val18return isv19}2021func (s *float32SliceValue) Set(val string) error {22ss := strings.Split(val, ",")23out := make([]float32, len(ss))24for i, d := range ss {25var err error26var temp64 float6427temp64, err = strconv.ParseFloat(d, 32)28if err != nil {29return err30}31out[i] = float32(temp64)3233}34if !s.changed {35*s.value = out36} else {37*s.value = append(*s.value, out...)38}39s.changed = true40return nil41}4243func (s *float32SliceValue) Type() string {44return "float32Slice"45}4647func (s *float32SliceValue) String() string {48out := make([]string, len(*s.value))49for i, d := range *s.value {50out[i] = fmt.Sprintf("%f", d)51}52return "[" + strings.Join(out, ",") + "]"53}5455func (s *float32SliceValue) fromString(val string) (float32, error) {56t64, err := strconv.ParseFloat(val, 32)57if err != nil {58return 0, err59}60return float32(t64), nil61}6263func (s *float32SliceValue) toString(val float32) string {64return fmt.Sprintf("%f", val)65}6667func (s *float32SliceValue) Append(val string) error {68i, err := s.fromString(val)69if err != nil {70return err71}72*s.value = append(*s.value, i)73return nil74}7576func (s *float32SliceValue) Replace(val []string) error {77out := make([]float32, len(val))78for i, d := range val {79var err error80out[i], err = s.fromString(d)81if err != nil {82return err83}84}85*s.value = out86return nil87}8889func (s *float32SliceValue) GetSlice() []string {90out := make([]string, len(*s.value))91for i, d := range *s.value {92out[i] = s.toString(d)93}94return out95}9697func float32SliceConv(val string) (interface{}, error) {98val = strings.Trim(val, "[]")99// Empty string would cause a slice with one (empty) entry100if len(val) == 0 {101return []float32{}, nil102}103ss := strings.Split(val, ",")104out := make([]float32, len(ss))105for i, d := range ss {106var err error107var temp64 float64108temp64, err = strconv.ParseFloat(d, 32)109if err != nil {110return nil, err111}112out[i] = float32(temp64)113114}115return out, nil116}117118// GetFloat32Slice return the []float32 value of a flag with the given name119func (f *FlagSet) GetFloat32Slice(name string) ([]float32, error) {120val, err := f.getFlagType(name, "float32Slice", float32SliceConv)121if err != nil {122return []float32{}, err123}124return val.([]float32), nil125}126127// Float32SliceVar defines a float32Slice flag with specified name, default value, and usage string.128// The argument p points to a []float32 variable in which to store the value of the flag.129func (f *FlagSet) Float32SliceVar(p *[]float32, name string, value []float32, usage string) {130f.VarP(newFloat32SliceValue(value, p), name, "", usage)131}132133// Float32SliceVarP is like Float32SliceVar, but accepts a shorthand letter that can be used after a single dash.134func (f *FlagSet) Float32SliceVarP(p *[]float32, name, shorthand string, value []float32, usage string) {135f.VarP(newFloat32SliceValue(value, p), name, shorthand, usage)136}137138// Float32SliceVar defines a float32[] flag with specified name, default value, and usage string.139// The argument p points to a float32[] variable in which to store the value of the flag.140func Float32SliceVar(p *[]float32, name string, value []float32, usage string) {141CommandLine.VarP(newFloat32SliceValue(value, p), name, "", usage)142}143144// Float32SliceVarP is like Float32SliceVar, but accepts a shorthand letter that can be used after a single dash.145func Float32SliceVarP(p *[]float32, name, shorthand string, value []float32, usage string) {146CommandLine.VarP(newFloat32SliceValue(value, p), name, shorthand, usage)147}148149// Float32Slice defines a []float32 flag with specified name, default value, and usage string.150// The return value is the address of a []float32 variable that stores the value of the flag.151func (f *FlagSet) Float32Slice(name string, value []float32, usage string) *[]float32 {152p := []float32{}153f.Float32SliceVarP(&p, name, "", value, usage)154return &p155}156157// Float32SliceP is like Float32Slice, but accepts a shorthand letter that can be used after a single dash.158func (f *FlagSet) Float32SliceP(name, shorthand string, value []float32, usage string) *[]float32 {159p := []float32{}160f.Float32SliceVarP(&p, name, shorthand, value, usage)161return &p162}163164// Float32Slice defines a []float32 flag with specified name, default value, and usage string.165// The return value is the address of a []float32 variable that stores the value of the flag.166func Float32Slice(name string, value []float32, usage string) *[]float32 {167return CommandLine.Float32SliceP(name, "", value, usage)168}169170// Float32SliceP is like Float32Slice, but accepts a shorthand letter that can be used after a single dash.171func Float32SliceP(name, shorthand string, value []float32, usage string) *[]float32 {172return CommandLine.Float32SliceP(name, shorthand, value, usage)173}174175176