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