github.com/puellanivis/breton@v0.2.16/lib/gnuflag/bool.go (about)

     1  package gnuflag
     2  
     3  import (
     4  	"strconv"
     5  )
     6  
     7  // -- bool Value
     8  type boolValue bool
     9  
    10  func (b *boolValue) Set(s string) error {
    11  	v, err := strconv.ParseBool(s)
    12  	*b = boolValue(v)
    13  	return err
    14  }
    15  
    16  func (b boolValue) Get() interface{} { return bool(b) }
    17  
    18  func (b boolValue) String() string { return strconv.FormatBool(bool(b)) }
    19  
    20  func (b boolValue) IsBoolFlag() bool { return true }
    21  
    22  // optional interface to indicate boolean flags that can be
    23  // supplied without "=value" text
    24  type boolFlag interface {
    25  	Value
    26  	IsBoolFlag() bool
    27  }
    28  
    29  // Bool defines a bool flag with specified name, and usage string.
    30  // The return value is the address of a bool variable that stores the value of the flag.
    31  func (f *FlagSet) Bool(name string, usage string, options ...Option) *bool {
    32  	p := new(bool)
    33  	if err := f.Var((*boolValue)(p), name, usage, options...); err != nil {
    34  		panic(err)
    35  	}
    36  	return p
    37  }
    38  
    39  // Bool defines a bool flag with specified name, and usage string.
    40  // The return value is the address of a bool variable that stores the value of the flag.
    41  func Bool(name string, usage string, options ...Option) *bool {
    42  	return CommandLine.Bool(name, usage, options...)
    43  }