float32.go 3.1 KB

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