bool_func.go raw

   1  package pflag
   2  
   3  // -- func Value
   4  type boolfuncValue func(string) error
   5  
   6  func (f boolfuncValue) Set(s string) error { return f(s) }
   7  
   8  func (f boolfuncValue) Type() string { return "boolfunc" }
   9  
  10  func (f boolfuncValue) String() string { return "" } // same behavior as stdlib 'flag' package
  11  
  12  func (f boolfuncValue) IsBoolFlag() bool { return true }
  13  
  14  // BoolFunc defines a func flag with specified name, callback function and usage string.
  15  //
  16  // The callback function will be called every time "--{name}" (or any form that matches the flag) is parsed
  17  // on the command line.
  18  func (f *FlagSet) BoolFunc(name string, usage string, fn func(string) error) {
  19  	f.BoolFuncP(name, "", usage, fn)
  20  }
  21  
  22  // BoolFuncP is like BoolFunc, but accepts a shorthand letter that can be used after a single dash.
  23  func (f *FlagSet) BoolFuncP(name, shorthand string, usage string, fn func(string) error) {
  24  	var val Value = boolfuncValue(fn)
  25  	flag := f.VarPF(val, name, shorthand, usage)
  26  	flag.NoOptDefVal = "true"
  27  }
  28  
  29  // BoolFunc defines a func flag with specified name, callback function and usage string.
  30  //
  31  // The callback function will be called every time "--{name}" (or any form that matches the flag) is parsed
  32  // on the command line.
  33  func BoolFunc(name string, usage string, fn func(string) error) {
  34  	CommandLine.BoolFuncP(name, "", usage, fn)
  35  }
  36  
  37  // BoolFuncP is like BoolFunc, but accepts a shorthand letter that can be used after a single dash.
  38  func BoolFuncP(name, shorthand string, usage string, fn func(string) error) {
  39  	CommandLine.BoolFuncP(name, shorthand, usage, fn)
  40  }
  41