-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
flag_float.go
48 lines (36 loc) · 1.11 KB
/
flag_float.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package cli
import (
"strconv"
)
type FloatFlag = FlagBase[float64, NoConfig, floatValue]
// -- float64 Value
type floatValue float64
// Below functions are to satisfy the ValueCreator interface
func (f floatValue) Create(val float64, p *float64, c NoConfig) Value {
*p = val
return (*floatValue)(p)
}
func (f floatValue) ToString(b float64) string {
return strconv.FormatFloat(b, 'g', -1, 64)
}
// Below functions are to satisfy the flag.Value interface
func (f *floatValue) Set(s string) error {
v, err := strconv.ParseFloat(s, 64)
if err != nil {
return err
}
*f = floatValue(v)
return err
}
func (f *floatValue) Get() any { return float64(*f) }
func (f *floatValue) String() string { return strconv.FormatFloat(float64(*f), 'g', -1, 64) }
// Float looks up the value of a local FloatFlag, returns
// 0 if not found
func (cmd *Command) Float(name string) float64 {
if v, ok := cmd.Value(name).(float64); ok {
tracef("float available for flag name %[1]q with value=%[2]v (cmd=%[3]q)", name, v, cmd.Name)
return v
}
tracef("float NOT available for flag name %[1]q (cmd=%[2]q)", name, cmd.Name)
return 0
}