...

Source file src/github.com/urfave/cli/v2/flag_uint64.go

Documentation: github.com/urfave/cli/v2

     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 *Uint64Flag) TakesValue() bool {
    11  	return true
    12  }
    13  
    14  // GetUsage returns the usage string for the flag
    15  func (f *Uint64Flag) GetUsage() string {
    16  	return f.Usage
    17  }
    18  
    19  // GetCategory returns the category for the flag
    20  func (f *Uint64Flag) GetCategory() string {
    21  	return f.Category
    22  }
    23  
    24  // Apply populates the flag given the flag set and environment
    25  func (f *Uint64Flag) 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 uint64 value from %s for flag %s: %s", val, source, f.Name, err)
    35  			}
    36  
    37  			f.Value = valInt
    38  			f.HasBeenSet = true
    39  		}
    40  	}
    41  
    42  	for _, name := range f.Names() {
    43  		if f.Destination != nil {
    44  			set.Uint64Var(f.Destination, name, f.Value, f.Usage)
    45  			continue
    46  		}
    47  		set.Uint64(name, f.Value, f.Usage)
    48  	}
    49  
    50  	return nil
    51  }
    52  
    53  // RunAction executes flag action if set
    54  func (f *Uint64Flag) RunAction(c *Context) error {
    55  	if f.Action != nil {
    56  		return f.Action(c, c.Uint64(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 *Uint64Flag) GetValue() string {
    65  	return fmt.Sprintf("%d", f.Value)
    66  }
    67  
    68  // GetDefaultText returns the default text for this flag
    69  func (f *Uint64Flag) 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 *Uint64Flag) GetEnvVars() []string {
    81  	return f.EnvVars
    82  }
    83  
    84  // Get returns the flag’s value in the given Context.
    85  func (f *Uint64Flag) Get(ctx *Context) uint64 {
    86  	return ctx.Uint64(f.Name)
    87  }
    88  
    89  // Uint64 looks up the value of a local Uint64Flag, returns
    90  // 0 if not found
    91  func (cCtx *Context) Uint64(name string) uint64 {
    92  	if fs := cCtx.lookupFlagSet(name); fs != nil {
    93  		return lookupUint64(name, fs)
    94  	}
    95  	return 0
    96  }
    97  
    98  func lookupUint64(name string, set *flag.FlagSet) uint64 {
    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 parsed
   106  	}
   107  	return 0
   108  }
   109  

View as plain text