float64.go 3.0 KB

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