Golang flag.Value type examples

package flag

Value is the interface to the dynamic value stored in a flag. (The default value is represented as a string.)

If a Value has an IsBoolFlag() bool method returning true, the command-line parser makes -name equivalent to -name=true rather than using the next command-line argument.

Golang flag.Value type usage examples

 func lookupStringSlice(name string, set *flag.FlagSet) []string {
  f := set.Lookup(name)
  if f != nil {
 return (f.Value.(*StringSlice)).Value()
  }
  return nil
 }

 func lookupIntSlice(name string, set *flag.FlagSet) []int {
  f := set.Lookup(name)
  if f != nil {
 return (f.Value.(*IntSlice)).Value()
  }
  return nil
 }

 func lookupGeneric(name string, set *flag.FlagSet) interface{} {
  f := set.Lookup(name)
  if f != nil {
 return f.Value
  }
  return nil
 }

 func lookupBool(name string, set *flag.FlagSet) bool {
  f := set.Lookup(name)
  if f != nil {
 val, err := strconv.ParseBool(f.Value.String())
 if err != nil {
 return false
 }
 return val
  }
  return false
 }

Reference :

http://golang.org/pkg/flag/#Value

Advertisement