...

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

Documentation: github.com/urfave/cli/v2

     1  package cli
     2  
     3  import (
     4  	"errors"
     5  	"flag"
     6  	"fmt"
     7  	"strconv"
     8  )
     9  
    10  // boolValue needs to implement the boolFlag internal interface in flag
    11  // to be able to capture bool fields and values
    12  //
    13  //	type boolFlag interface {
    14  //		  Value
    15  //		  IsBoolFlag() bool
    16  //	}
    17  type boolValue struct {
    18  	destination *bool
    19  	count       *int
    20  }
    21  
    22  func newBoolValue(val bool, p *bool, count *int) *boolValue {
    23  	*p = val
    24  	return &boolValue{
    25  		destination: p,
    26  		count:       count,
    27  	}
    28  }
    29  
    30  func (b *boolValue) Set(s string) error {
    31  	v, err := strconv.ParseBool(s)
    32  	if err != nil {
    33  		err = errors.New("parse error")
    34  		return err
    35  	}
    36  	*b.destination = v
    37  	if b.count != nil {
    38  		*b.count = *b.count + 1
    39  	}
    40  	return err
    41  }
    42  
    43  func (b *boolValue) Get() interface{} { return *b.destination }
    44  
    45  func (b *boolValue) String() string {
    46  	if b.destination != nil {
    47  		return strconv.FormatBool(*b.destination)
    48  	}
    49  	return strconv.FormatBool(false)
    50  }
    51  
    52  func (b *boolValue) IsBoolFlag() bool { return true }
    53  
    54  func (b *boolValue) Count() int {
    55  	if b.count != nil && *b.count > 0 {
    56  		return *b.count
    57  	}
    58  	return 0
    59  }
    60  
    61  // TakesValue returns true of the flag takes a value, otherwise false
    62  func (f *BoolFlag) TakesValue() bool {
    63  	return false
    64  }
    65  
    66  // GetUsage returns the usage string for the flag
    67  func (f *BoolFlag) GetUsage() string {
    68  	return f.Usage
    69  }
    70  
    71  // GetCategory returns the category for the flag
    72  func (f *BoolFlag) GetCategory() string {
    73  	return f.Category
    74  }
    75  
    76  // GetValue returns the flags value as string representation and an empty
    77  // string if the flag takes no value at all.
    78  func (f *BoolFlag) GetValue() string {
    79  	return ""
    80  }
    81  
    82  // GetDefaultText returns the default text for this flag
    83  func (f *BoolFlag) GetDefaultText() string {
    84  	if f.DefaultText != "" {
    85  		return f.DefaultText
    86  	}
    87  	if f.defaultValueSet {
    88  		return fmt.Sprintf("%v", f.defaultValue)
    89  	}
    90  	return fmt.Sprintf("%v", f.Value)
    91  }
    92  
    93  // GetEnvVars returns the env vars for this flag
    94  func (f *BoolFlag) GetEnvVars() []string {
    95  	return f.EnvVars
    96  }
    97  
    98  // RunAction executes flag action if set
    99  func (f *BoolFlag) RunAction(c *Context) error {
   100  	if f.Action != nil {
   101  		return f.Action(c, c.Bool(f.Name))
   102  	}
   103  
   104  	return nil
   105  }
   106  
   107  // Apply populates the flag given the flag set and environment
   108  func (f *BoolFlag) Apply(set *flag.FlagSet) error {
   109  	// set default value so that environment wont be able to overwrite it
   110  	f.defaultValue = f.Value
   111  	f.defaultValueSet = true
   112  
   113  	if val, source, found := flagFromEnvOrFile(f.EnvVars, f.FilePath); found {
   114  		if val != "" {
   115  			valBool, err := strconv.ParseBool(val)
   116  
   117  			if err != nil {
   118  				return fmt.Errorf("could not parse %q as bool value from %s for flag %s: %s", val, source, f.Name, err)
   119  			}
   120  
   121  			f.Value = valBool
   122  		} else {
   123  			// empty value implies that the env is defined but set to empty string, we have to assume that this is
   124  			// what the user wants. If user doesnt want this then the env needs to be deleted or the flag removed from
   125  			// file
   126  			f.Value = false
   127  		}
   128  		f.HasBeenSet = true
   129  	}
   130  
   131  	count := f.Count
   132  	dest := f.Destination
   133  
   134  	if count == nil {
   135  		count = new(int)
   136  	}
   137  
   138  	// since count will be incremented for each alias as well
   139  	// subtract number of aliases from overall count
   140  	*count -= len(f.Aliases)
   141  
   142  	if dest == nil {
   143  		dest = new(bool)
   144  	}
   145  
   146  	for _, name := range f.Names() {
   147  		value := newBoolValue(f.Value, dest, count)
   148  		set.Var(value, name, f.Usage)
   149  	}
   150  
   151  	return nil
   152  }
   153  
   154  // Get returns the flag’s value in the given Context.
   155  func (f *BoolFlag) Get(ctx *Context) bool {
   156  	return ctx.Bool(f.Name)
   157  }
   158  
   159  // Bool looks up the value of a local BoolFlag, returns
   160  // false if not found
   161  func (cCtx *Context) Bool(name string) bool {
   162  	if fs := cCtx.lookupFlagSet(name); fs != nil {
   163  		return lookupBool(name, fs)
   164  	}
   165  	return false
   166  }
   167  
   168  func lookupBool(name string, set *flag.FlagSet) bool {
   169  	f := set.Lookup(name)
   170  	if f != nil {
   171  		parsed, err := strconv.ParseBool(f.Value.String())
   172  		if err != nil {
   173  			return false
   174  		}
   175  		return parsed
   176  	}
   177  	return false
   178  }
   179  

View as plain text