...

Source file src/github.com/urfave/cli/v2/flag_int64.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 *Int64Flag) TakesValue() bool {
    11  	return true
    12  }
    13  
    14  // GetUsage returns the usage string for the flag
    15  func (f *Int64Flag) GetUsage() string {
    16  	return f.Usage
    17  }
    18  
    19  // GetCategory returns the category for the flag
    20  func (f *Int64Flag) 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 *Int64Flag) GetValue() string {
    27  	return fmt.Sprintf("%d", f.Value)
    28  }
    29  
    30  // GetDefaultText returns the default text for this flag
    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  // GetEnvVars returns the env vars for this flag
    42  func (f *Int64Flag) GetEnvVars() []string {
    43  	return f.EnvVars
    44  }
    45  
    46  // Apply populates the flag given the flag set and environment
    47  func (f *Int64Flag) Apply(set *flag.FlagSet) error {
    48  	// set default value so that environment wont be able to overwrite it
    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  // Get returns the flag’s value in the given Context.
    76  func (f *Int64Flag) Get(ctx *Context) int64 {
    77  	return ctx.Int64(f.Name)
    78  }
    79  
    80  // RunAction executes flag action if set
    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  // Int64 looks up the value of a local Int64Flag, returns
    90  // 0 if not found
    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