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.

flag_float64.go 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package cli
  2. import (
  3. "flag"
  4. "fmt"
  5. "strconv"
  6. )
  7. // Float64Flag is a flag with type float64
  8. type Float64Flag struct {
  9. Name string
  10. Usage string
  11. EnvVar string
  12. FilePath string
  13. Required bool
  14. Hidden bool
  15. Value float64
  16. Destination *float64
  17. }
  18. // String returns a readable representation of this value
  19. // (for usage defaults)
  20. func (f Float64Flag) String() string {
  21. return FlagStringer(f)
  22. }
  23. // GetName returns the name of the flag
  24. func (f Float64Flag) GetName() string {
  25. return f.Name
  26. }
  27. // IsRequired returns whether or not the flag is required
  28. func (f Float64Flag) IsRequired() bool {
  29. return f.Required
  30. }
  31. // TakesValue returns true of the flag takes a value, otherwise false
  32. func (f Float64Flag) TakesValue() bool {
  33. return true
  34. }
  35. // GetUsage returns the usage string for the flag
  36. func (f Float64Flag) GetUsage() string {
  37. return f.Usage
  38. }
  39. // GetValue returns the flags value as string representation and an empty
  40. // string if the flag takes no value at all.
  41. func (f Float64Flag) GetValue() string {
  42. return fmt.Sprintf("%f", f.Value)
  43. }
  44. // Float64 looks up the value of a local Float64Flag, returns
  45. // 0 if not found
  46. func (c *Context) Float64(name string) float64 {
  47. return lookupFloat64(name, c.flagSet)
  48. }
  49. // GlobalFloat64 looks up the value of a global Float64Flag, returns
  50. // 0 if not found
  51. func (c *Context) GlobalFloat64(name string) float64 {
  52. if fs := lookupGlobalFlagSet(name, c); fs != nil {
  53. return lookupFloat64(name, fs)
  54. }
  55. return 0
  56. }
  57. // Apply populates the flag given the flag set and environment
  58. // Ignores errors
  59. func (f Float64Flag) Apply(set *flag.FlagSet) {
  60. _ = f.ApplyWithError(set)
  61. }
  62. // ApplyWithError populates the flag given the flag set and environment
  63. func (f Float64Flag) ApplyWithError(set *flag.FlagSet) error {
  64. if envVal, ok := flagFromFileEnv(f.FilePath, f.EnvVar); ok {
  65. envValFloat, err := strconv.ParseFloat(envVal, 10)
  66. if err != nil {
  67. return fmt.Errorf("could not parse %s as float64 value for flag %s: %s", envVal, f.Name, err)
  68. }
  69. f.Value = envValFloat
  70. }
  71. eachName(f.Name, func(name string) {
  72. if f.Destination != nil {
  73. set.Float64Var(f.Destination, name, f.Value, f.Usage)
  74. return
  75. }
  76. set.Float64(name, f.Value, f.Usage)
  77. })
  78. return nil
  79. }
  80. func lookupFloat64(name string, set *flag.FlagSet) float64 {
  81. f := set.Lookup(name)
  82. if f != nil {
  83. parsed, err := strconv.ParseFloat(f.Value.String(), 64)
  84. if err != nil {
  85. return 0
  86. }
  87. return parsed
  88. }
  89. return 0
  90. }