...
1 package cli
2
3 import (
4 "flag"
5 "fmt"
6 "strconv"
7 )
8
9
10 func (f *Int64Flag) TakesValue() bool {
11 return true
12 }
13
14
15 func (f *Int64Flag) GetUsage() string {
16 return f.Usage
17 }
18
19
20 func (f *Int64Flag) GetCategory() string {
21 return f.Category
22 }
23
24
25
26 func (f *Int64Flag) GetValue() string {
27 return fmt.Sprintf("%d", f.Value)
28 }
29
30
31 func (f *Int64Flag) GetDefaultText() string {
32 if f.DefaultText != "" {
33 return f.DefaultText
34 }
35 if f.defaultValueSet {
36 return fmt.Sprintf("%d", f.defaultValue)
37 }
38 return fmt.Sprintf("%d", f.Value)
39 }
40
41
42 func (f *Int64Flag) GetEnvVars() []string {
43 return f.EnvVars
44 }
45
46
47 func (f *Int64Flag) Apply(set *flag.FlagSet) error {
48
49 f.defaultValue = f.Value
50 f.defaultValueSet = true
51
52 if val, source, found := flagFromEnvOrFile(f.EnvVars, f.FilePath); found {
53 if val != "" {
54 valInt, err := strconv.ParseInt(val, f.Base, 64)
55
56 if err != nil {
57 return fmt.Errorf("could not parse %q as int value from %s for flag %s: %s", val, source, f.Name, err)
58 }
59
60 f.Value = valInt
61 f.HasBeenSet = true
62 }
63 }
64
65 for _, name := range f.Names() {
66 if f.Destination != nil {
67 set.Int64Var(f.Destination, name, f.Value, f.Usage)
68 continue
69 }
70 set.Int64(name, f.Value, f.Usage)
71 }
72 return nil
73 }
74
75
76 func (f *Int64Flag) Get(ctx *Context) int64 {
77 return ctx.Int64(f.Name)
78 }
79
80
81 func (f *Int64Flag) RunAction(c *Context) error {
82 if f.Action != nil {
83 return f.Action(c, c.Int64(f.Name))
84 }
85
86 return nil
87 }
88
89
90
91 func (cCtx *Context) Int64(name string) int64 {
92 if fs := cCtx.lookupFlagSet(name); fs != nil {
93 return lookupInt64(name, fs)
94 }
95 return 0
96 }
97
98 func lookupInt64(name string, set *flag.FlagSet) int64 {
99 f := set.Lookup(name)
100 if f != nil {
101 parsed, err := strconv.ParseInt(f.Value.String(), 0, 64)
102 if err != nil {
103 return 0
104 }
105 return parsed
106 }
107 return 0
108 }
109
View as plain text