vanity

vanity - A tiny server for golang vanity redirects
Log | Files | Refs | README | LICENSE

float32_slice.go (5042B)


      1 package pflag
      2 
      3 import (
      4 	"fmt"
      5 	"strconv"
      6 	"strings"
      7 )
      8 
      9 // -- float32Slice Value
     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 	// Empty string would cause a slice with one (empty) entry
    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 // GetFloat32Slice return the []float32 value of a flag with the given name
    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 // 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.
    130 func (f *FlagSet) Float32SliceVar(p *[]float32, name string, value []float32, usage string) {
    131 	f.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.
    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 // 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.
    141 func Float32SliceVar(p *[]float32, name string, value []float32, usage string) {
    142 	CommandLine.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.
    146 func Float32SliceVarP(p *[]float32, name, shorthand string, value []float32, usage string) {
    147 	CommandLine.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.
    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 // Float32SliceP is like Float32Slice, but accepts a shorthand letter that can be used after a single dash.
    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 // 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.
    167 func Float32Slice(name string, value []float32, usage string) *[]float32 {
    168 	return 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.
    172 func Float32SliceP(name, shorthand string, value []float32, usage string) *[]float32 {
    173 	return CommandLine.Float32SliceP(name, shorthand, value, usage)
    174 }