1package pflag 2 3import ( 4 "fmt" 5 "strconv" 6 "strings" 7) 8 9// -- intSlice Value 10type intSliceValue struct { 11 value *[]int 12 changed bool 13} 14 15func newIntSliceValue(val []int, p *[]int) *intSliceValue { 16 isv := new(intSliceValue) 17 isv.value = p 18 *isv.value = val 19 return isv 20} 21 22func (s *intSliceValue) Set(val string) error { 23 ss := strings.Split(val, ",") 24 out := make([]int, len(ss)) 25 for i, d := range ss { 26 var err error 27 out[i], err = strconv.Atoi(d) 28 if err != nil { 29 return err 30 } 31 32 } 33 if !s.changed { 34 *s.value = out 35 } else { 36 *s.value = append(*s.value, out...) 37 } 38 s.changed = true 39 return nil 40} 41 42func (s *intSliceValue) Type() string { 43 return "intSlice" 44} 45 46func (s *intSliceValue) String() string { 47 out := make([]string, len(*s.value)) 48 for i, d := range *s.value { 49 out[i] = fmt.Sprintf("%d", d) 50 } 51 return "[" + strings.Join(out, ",") + "]" 52} 53 54func (s *intSliceValue) Append(val string) error { 55 i, err := strconv.Atoi(val) 56 if err != nil { 57 return err 58 } 59 *s.value = append(*s.value, i) 60 return nil 61} 62 63func (s *intSliceValue) Replace(val []string) error { 64 out := make([]int, len(val)) 65 for i, d := range val { 66 var err error 67 out[i], err = strconv.Atoi(d) 68 if err != nil { 69 return err 70 } 71 } 72 *s.value = out 73 return nil 74} 75 76func (s *intSliceValue) GetSlice() []string { 77 out := make([]string, len(*s.value)) 78 for i, d := range *s.value { 79 out[i] = strconv.Itoa(d) 80 } 81 return out 82} 83 84func intSliceConv(val string) (interface{}, error) { 85 val = strings.Trim(val, "[]") 86 // Empty string would cause a slice with one (empty) entry 87 if len(val) == 0 { 88 return []int{}, nil 89 } 90 ss := strings.Split(val, ",") 91 out := make([]int, len(ss)) 92 for i, d := range ss { 93 var err error 94 out[i], err = strconv.Atoi(d) 95 if err != nil { 96 return nil, err 97 } 98 99 } 100 return out, nil 101} 102 103// GetIntSlice return the []int value of a flag with the given name 104func (f *FlagSet) GetIntSlice(name string) ([]int, error) { 105 val, err := f.getFlagType(name, "intSlice", intSliceConv) 106 if err != nil { 107 return []int{}, err 108 } 109 return val.([]int), nil 110} 111 112// IntSliceVar defines a intSlice flag with specified name, default value, and usage string. 113// The argument p points to a []int variable in which to store the value of the flag. 114func (f *FlagSet) IntSliceVar(p *[]int, name string, value []int, usage string) { 115 f.VarP(newIntSliceValue(value, p), name, "", usage) 116} 117 118// IntSliceVarP is like IntSliceVar, but accepts a shorthand letter that can be used after a single dash. 119func (f *FlagSet) IntSliceVarP(p *[]int, name, shorthand string, value []int, usage string) { 120 f.VarP(newIntSliceValue(value, p), name, shorthand, usage) 121} 122 123// IntSliceVar defines a int[] flag with specified name, default value, and usage string. 124// The argument p points to a int[] variable in which to store the value of the flag. 125func IntSliceVar(p *[]int, name string, value []int, usage string) { 126 CommandLine.VarP(newIntSliceValue(value, p), name, "", usage) 127} 128 129// IntSliceVarP is like IntSliceVar, but accepts a shorthand letter that can be used after a single dash. 130func IntSliceVarP(p *[]int, name, shorthand string, value []int, usage string) { 131 CommandLine.VarP(newIntSliceValue(value, p), name, shorthand, usage) 132} 133 134// IntSlice defines a []int flag with specified name, default value, and usage string. 135// The return value is the address of a []int variable that stores the value of the flag. 136func (f *FlagSet) IntSlice(name string, value []int, usage string) *[]int { 137 p := []int{} 138 f.IntSliceVarP(&p, name, "", value, usage) 139 return &p 140} 141 142// IntSliceP is like IntSlice, but accepts a shorthand letter that can be used after a single dash. 143func (f *FlagSet) IntSliceP(name, shorthand string, value []int, usage string) *[]int { 144 p := []int{} 145 f.IntSliceVarP(&p, name, shorthand, value, usage) 146 return &p 147} 148 149// IntSlice defines a []int flag with specified name, default value, and usage string. 150// The return value is the address of a []int variable that stores the value of the flag. 151func IntSlice(name string, value []int, usage string) *[]int { 152 return CommandLine.IntSliceP(name, "", value, usage) 153} 154 155// IntSliceP is like IntSlice, but accepts a shorthand letter that can be used after a single dash. 156func IntSliceP(name, shorthand string, value []int, usage string) *[]int { 157 return CommandLine.IntSliceP(name, shorthand, value, usage) 158} 159