vanity

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

float64_slice.go (4879B)


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