Path: blob/main/vendor/github.com/spf13/pflag/int32_slice.go
2875 views
package pflag12import (3"fmt"4"strconv"5"strings"6)78// -- int32Slice Value9type int32SliceValue struct {10value *[]int3211changed bool12}1314func newInt32SliceValue(val []int32, p *[]int32) *int32SliceValue {15isv := new(int32SliceValue)16isv.value = p17*isv.value = val18return isv19}2021func (s *int32SliceValue) Set(val string) error {22ss := strings.Split(val, ",")23out := make([]int32, len(ss))24for i, d := range ss {25var err error26var temp64 int6427temp64, err = strconv.ParseInt(d, 0, 32)28if err != nil {29return err30}31out[i] = int32(temp64)3233}34if !s.changed {35*s.value = out36} else {37*s.value = append(*s.value, out...)38}39s.changed = true40return nil41}4243func (s *int32SliceValue) Type() string {44return "int32Slice"45}4647func (s *int32SliceValue) String() string {48out := make([]string, len(*s.value))49for i, d := range *s.value {50out[i] = fmt.Sprintf("%d", d)51}52return "[" + strings.Join(out, ",") + "]"53}5455func (s *int32SliceValue) fromString(val string) (int32, error) {56t64, err := strconv.ParseInt(val, 0, 32)57if err != nil {58return 0, err59}60return int32(t64), nil61}6263func (s *int32SliceValue) toString(val int32) string {64return fmt.Sprintf("%d", val)65}6667func (s *int32SliceValue) Append(val string) error {68i, err := s.fromString(val)69if err != nil {70return err71}72*s.value = append(*s.value, i)73return nil74}7576func (s *int32SliceValue) Replace(val []string) error {77out := make([]int32, len(val))78for i, d := range val {79var err error80out[i], err = s.fromString(d)81if err != nil {82return err83}84}85*s.value = out86return nil87}8889func (s *int32SliceValue) GetSlice() []string {90out := make([]string, len(*s.value))91for i, d := range *s.value {92out[i] = s.toString(d)93}94return out95}9697func int32SliceConv(val string) (interface{}, error) {98val = strings.Trim(val, "[]")99// Empty string would cause a slice with one (empty) entry100if len(val) == 0 {101return []int32{}, nil102}103ss := strings.Split(val, ",")104out := make([]int32, len(ss))105for i, d := range ss {106var err error107var temp64 int64108temp64, err = strconv.ParseInt(d, 0, 32)109if err != nil {110return nil, err111}112out[i] = int32(temp64)113114}115return out, nil116}117118// GetInt32Slice return the []int32 value of a flag with the given name119func (f *FlagSet) GetInt32Slice(name string) ([]int32, error) {120val, err := f.getFlagType(name, "int32Slice", int32SliceConv)121if err != nil {122return []int32{}, err123}124return val.([]int32), nil125}126127// Int32SliceVar defines a int32Slice flag with specified name, default value, and usage string.128// The argument p points to a []int32 variable in which to store the value of the flag.129func (f *FlagSet) Int32SliceVar(p *[]int32, name string, value []int32, usage string) {130f.VarP(newInt32SliceValue(value, p), name, "", usage)131}132133// Int32SliceVarP is like Int32SliceVar, but accepts a shorthand letter that can be used after a single dash.134func (f *FlagSet) Int32SliceVarP(p *[]int32, name, shorthand string, value []int32, usage string) {135f.VarP(newInt32SliceValue(value, p), name, shorthand, usage)136}137138// Int32SliceVar defines a int32[] flag with specified name, default value, and usage string.139// The argument p points to a int32[] variable in which to store the value of the flag.140func Int32SliceVar(p *[]int32, name string, value []int32, usage string) {141CommandLine.VarP(newInt32SliceValue(value, p), name, "", usage)142}143144// Int32SliceVarP is like Int32SliceVar, but accepts a shorthand letter that can be used after a single dash.145func Int32SliceVarP(p *[]int32, name, shorthand string, value []int32, usage string) {146CommandLine.VarP(newInt32SliceValue(value, p), name, shorthand, usage)147}148149// Int32Slice defines a []int32 flag with specified name, default value, and usage string.150// The return value is the address of a []int32 variable that stores the value of the flag.151func (f *FlagSet) Int32Slice(name string, value []int32, usage string) *[]int32 {152p := []int32{}153f.Int32SliceVarP(&p, name, "", value, usage)154return &p155}156157// Int32SliceP is like Int32Slice, but accepts a shorthand letter that can be used after a single dash.158func (f *FlagSet) Int32SliceP(name, shorthand string, value []int32, usage string) *[]int32 {159p := []int32{}160f.Int32SliceVarP(&p, name, shorthand, value, usage)161return &p162}163164// Int32Slice defines a []int32 flag with specified name, default value, and usage string.165// The return value is the address of a []int32 variable that stores the value of the flag.166func Int32Slice(name string, value []int32, usage string) *[]int32 {167return CommandLine.Int32SliceP(name, "", value, usage)168}169170// Int32SliceP is like Int32Slice, but accepts a shorthand letter that can be used after a single dash.171func Int32SliceP(name, shorthand string, value []int32, usage string) *[]int32 {172return CommandLine.Int32SliceP(name, shorthand, value, usage)173}174175176