func registerFlagInt(t reflect.Kind, fs *pflag.FlagSet, field interface{}, parts []string, vt int64, desc string) { switch t { case reflect.Int: if len(parts) == 1 { fs.IntVar(field.(*int), parts[0], int(vt), desc) } else { fs.IntVarP(field.(*int), parts[0], parts[1], int(vt), desc) } case reflect.Int8: if len(parts) == 1 { fs.Int8Var(field.(*int8), parts[0], int8(vt), desc) } else { fs.Int8VarP(field.(*int8), parts[0], parts[1], int8(vt), desc) } case reflect.Int32: if len(parts) == 1 { fs.Int32Var(field.(*int32), parts[0], int32(vt), desc) } else { fs.Int32VarP(field.(*int32), parts[0], parts[1], int32(vt), desc) } case reflect.Int64: if len(parts) == 1 { fs.Int64Var(field.(*int64), parts[0], int64(vt), desc) } else { fs.Int64VarP(field.(*int64), parts[0], parts[1], int64(vt), desc) } } }
// BindIntFlag binds a flag that expects an integer value. func (i FlagInfo) BindIntFlag(flags *pflag.FlagSet, target *int) { // assume flags with no longname are not desired if len(i.LongName) > 0 { // try to parse Default as an int. If it fails, assume 0 intVal, _ := strconv.ParseInt(i.Default, 10, 0) flags.IntVarP(target, i.LongName, i.ShortName, int(intVal), i.Description) } }
func intFlag(f *pflag.FlagSet, valPtr *int, flagInfo cliflags.FlagInfo, defaultVal int) { if flagInfo.Shorthand == "" { f.IntVar(valPtr, flagInfo.Name, defaultVal, makeUsageString(flagInfo)) } else { f.IntVarP(valPtr, flagInfo.Name, flagInfo.Shorthand, defaultVal, makeUsageString(flagInfo)) } setFlagFromEnv(f, flagInfo) }
func (conf *BasicConfig) AddFlags(flags *flag.FlagSet) { flags.Int64VarP(&conf.NumRequests, "numrequests", "n", 10000, "Number of requests.") flags.IntVarP(&conf.Concurrency, "concurrency", "c", 100, "Number of concurrent workers.") flags.StringVarP(&conf.Cluster, "cluster", "", "", "Peer nodes to use, if none, use this process.") }
func (c *Config) intVarP(fset *flag.FlagSet, name string, aof *int) *flag.Flag { i := keys[name] fset.IntVarP(aof, name, i.Shorthand, c.Viper.GetInt(name), i.Description) return fset.Lookup(name) }