float64.go 3.0 KB

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