You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

float32_slice.go 4.9KB

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