func.go raw

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