1package cli 2 3import ( 4 "flag" 5 "fmt" 6 "strconv" 7) 8 9// Int64Flag is a flag with type int64 10type Int64Flag struct { 11 Name string 12 Aliases []string 13 Usage string 14 EnvVars []string 15 FilePath string 16 Required bool 17 Hidden bool 18 Value int64 19 DefaultText string 20 Destination *int64 21 HasBeenSet bool 22} 23 24// IsSet returns whether or not the flag has been set through env or file 25func (f *Int64Flag) IsSet() bool { 26 return f.HasBeenSet 27} 28 29// String returns a readable representation of this value 30// (for usage defaults) 31func (f *Int64Flag) String() string { 32 return FlagStringer(f) 33} 34 35// Names returns the names of the flag 36func (f *Int64Flag) Names() []string { 37 return flagNames(f.Name, f.Aliases) 38} 39 40// IsRequired returns whether or not the flag is required 41func (f *Int64Flag) IsRequired() bool { 42 return f.Required 43} 44 45// TakesValue returns true of the flag takes a value, otherwise false 46func (f *Int64Flag) TakesValue() bool { 47 return true 48} 49 50// GetUsage returns the usage string for the flag 51func (f *Int64Flag) GetUsage() string { 52 return f.Usage 53} 54 55// GetValue returns the flags value as string representation and an empty 56// string if the flag takes no value at all. 57func (f *Int64Flag) GetValue() string { 58 return fmt.Sprintf("%d", f.Value) 59} 60 61// Apply populates the flag given the flag set and environment 62func (f *Int64Flag) Apply(set *flag.FlagSet) error { 63 if val, ok := flagFromEnvOrFile(f.EnvVars, f.FilePath); ok { 64 if val != "" { 65 valInt, err := strconv.ParseInt(val, 0, 64) 66 67 if err != nil { 68 return fmt.Errorf("could not parse %q as int value for flag %s: %s", val, f.Name, err) 69 } 70 71 f.Value = valInt 72 f.HasBeenSet = true 73 } 74 } 75 76 for _, name := range f.Names() { 77 if f.Destination != nil { 78 set.Int64Var(f.Destination, name, f.Value, f.Usage) 79 continue 80 } 81 set.Int64(name, f.Value, f.Usage) 82 } 83 return nil 84} 85 86// Int64 looks up the value of a local Int64Flag, returns 87// 0 if not found 88func (c *Context) Int64(name string) int64 { 89 if fs := lookupFlagSet(name, c); fs != nil { 90 return lookupInt64(name, fs) 91 } 92 return 0 93} 94 95func lookupInt64(name string, set *flag.FlagSet) int64 { 96 f := set.Lookup(name) 97 if f != nil { 98 parsed, err := strconv.ParseInt(f.Value.String(), 0, 64) 99 if err != nil { 100 return 0 101 } 102 return parsed 103 } 104 return 0 105} 106