float32.go 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package pflag
  2. import "strconv"
  3. // -- float32 Value
  4. type float32Value float32
  5. func newFloat32Value(val float32, p *float32) *float32Value {
  6. *p = val
  7. return (*float32Value)(p)
  8. }
  9. func (f *float32Value) Set(s string) error {
  10. v, err := strconv.ParseFloat(s, 32)
  11. *f = float32Value(v)
  12. return err
  13. }
  14. func (f *float32Value) Type() string {
  15. return "float32"
  16. }
  17. func (f *float32Value) String() string { return strconv.FormatFloat(float64(*f), 'g', -1, 32) }
  18. func float32Conv(sval string) (interface{}, error) {
  19. v, err := strconv.ParseFloat(sval, 32)
  20. if err != nil {
  21. return 0, err
  22. }
  23. return float32(v), nil
  24. }
  25. // GetFloat32 return the float32 value of a flag with the given name
  26. func (f *FlagSet) GetFloat32(name string) (float32, error) {
  27. val, err := f.getFlagType(name, "float32", float32Conv)
  28. if err != nil {
  29. return 0, err
  30. }
  31. return val.(float32), nil
  32. }
  33. // Float32Var defines a float32 flag with specified name, default value, and usage string.
  34. // The argument p points to a float32 variable in which to store the value of the flag.
  35. func (f *FlagSet) Float32Var(p *float32, name string, value float32, usage string) {
  36. f.VarP(newFloat32Value(value, p), name, "", usage)
  37. }
  38. // Float32VarP is like Float32Var, but accepts a shorthand letter that can be used after a single dash.
  39. func (f *FlagSet) Float32VarP(p *float32, name, shorthand string, value float32, usage string) {
  40. f.VarP(newFloat32Value(value, p), name, shorthand, usage)
  41. }
  42. // Float32Var defines a float32 flag with specified name, default value, and usage string.
  43. // The argument p points to a float32 variable in which to store the value of the flag.
  44. func Float32Var(p *float32, name string, value float32, usage string) {
  45. CommandLine.VarP(newFloat32Value(value, p), name, "", usage)
  46. }
  47. // Float32VarP is like Float32Var, but accepts a shorthand letter that can be used after a single dash.
  48. func Float32VarP(p *float32, name, shorthand string, value float32, usage string) {
  49. CommandLine.VarP(newFloat32Value(value, p), name, shorthand, usage)
  50. }
  51. // Float32 defines a float32 flag with specified name, default value, and usage string.
  52. // The return value is the address of a float32 variable that stores the value of the flag.
  53. func (f *FlagSet) Float32(name string, value float32, usage string) *float32 {
  54. p := new(float32)
  55. f.Float32VarP(p, name, "", value, usage)
  56. return p
  57. }
  58. // Float32P is like Float32, but accepts a shorthand letter that can be used after a single dash.
  59. func (f *FlagSet) Float32P(name, shorthand string, value float32, usage string) *float32 {
  60. p := new(float32)
  61. f.Float32VarP(p, name, shorthand, value, usage)
  62. return p
  63. }
  64. // Float32 defines a float32 flag with specified name, default value, and usage string.
  65. // The return value is the address of a float32 variable that stores the value of the flag.
  66. func Float32(name string, value float32, usage string) *float32 {
  67. return CommandLine.Float32P(name, "", value, usage)
  68. }
  69. // Float32P is like Float32, but accepts a shorthand letter that can be used after a single dash.
  70. func Float32P(name, shorthand string, value float32, usage string) *float32 {
  71. return CommandLine.Float32P(name, shorthand, value, usage)
  72. }