Debian Codegraph @

vendor/github.com/urfave/cli/v2/flag_float64.go

raw
1 package cli
2
3 import (
4 "flag"
5 "fmt"
6 "strconv"
7 )
8
9 // TakesValue returns true of the flag takes a value, otherwise false
10 func (f *Float64Flag) TakesValue() bool {
11 return true
12 }
13
14 // GetUsage returns the usage string for the flag
15 func (f *Float64Flag) GetUsage() string {
16 return f.Usage
17 }
18
19 // GetCategory returns the category for the flag
20 func (f *Float64Flag) GetCategory() string {
21 return f.Category
22 }
23
24 // GetValue returns the flags value as string representation and an empty
25 // string if the flag takes no value at all.
26 func (f *Float64Flag) GetValue() string {
27 return fmt.Sprintf("%v", f.Value)
28 }
29
30 // GetDefaultText returns the default text for this flag
31 func (f *Float64Flag) GetDefaultText() string {
32 if f.DefaultText != "" {
33 return f.DefaultText
34 }
35 if f.defaultValueSet {
36 return fmt.Sprintf("%v", f.defaultValue)
37 }
38 return fmt.Sprintf("%v", f.Value)
39 }
40
41 // GetEnvVars returns the env vars for this flag
42 func (f *Float64Flag) GetEnvVars() []string {
43 return f.EnvVars
44 }
45
46 // Apply populates the flag given the flag set and environment
47 func (f *Float64Flag) Apply(set *flag.FlagSet) error {
48 f.defaultValue = f.Value
49 f.defaultValueSet = true
50
51 if val, source, found := flagFromEnvOrFile(f.EnvVars, f.FilePath); found {
52 if val != "" {
53 valFloat, err := strconv.ParseFloat(val, 64)
54 if err != nil {
55 return fmt.Errorf("could not parse %q as float64 value from %s for flag %s: %s", val, source, f.Name, err)
56 }
57
58 f.Value = valFloat
59 f.HasBeenSet = true
60 }
61 }
62
63 for _, name := range f.Names() {
64 if f.Destination != nil {
65 set.Float64Var(f.Destination, name, f.Value, f.Usage)
66 continue
67 }
68 set.Float64(name, f.Value, f.Usage)
69 }
70
71 return nil
72 }
73
74 // Get returns the flag’s value in the given Context.
75 func (f *Float64Flag) Get(ctx *Context) float64 {
76 return ctx.Float64(f.Name)
77 }
78
79 // RunAction executes flag action if set
80 func (f *Float64Flag) RunAction(c *Context) error {
81 if f.Action != nil {
82 return f.Action(c, c.Float64(f.Name))
83 }
84
85 return nil
86 }
87
88 // Float64 looks up the value of a local Float64Flag, returns
89 // 0 if not found
90 func (cCtx *Context) Float64(name string) float64 {
91 if fs := cCtx.lookupFlagSet(name); fs != nil {
92 return lookupFloat64(name, fs)
93 }
94 return 0
95 }
96
97 func lookupFloat64(name string, set *flag.FlagSet) float64 {
98 f := set.Lookup(name)
99 if f != nil {
100 parsed, err := strconv.ParseFloat(f.Value.String(), 64)
101 if err != nil {
102 return 0
103 }
104 return parsed
105 }
106 return 0
107 }