10
type intSliceValue struct {
15
func newIntSliceValue(val []int, p *[]int) *intSliceValue {
16
isv := new(intSliceValue)
22
func (s *intSliceValue) Set(val string) error {
23
ss := strings.Split(val, ",")
24
out := make([]int, len(ss))
25
for i, d := range ss {
27
out[i], err = strconv.Atoi(d)
36
*s.value = append(*s.value, out...)
42
func (s *intSliceValue) Type() string {
46
func (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)
51
return "[" + strings.Join(out, ",") + "]"
54
func (s *intSliceValue) Append(val string) error {
55
i, err := strconv.Atoi(val)
59
*s.value = append(*s.value, i)
63
func (s *intSliceValue) Replace(val []string) error {
64
out := make([]int, len(val))
65
for i, d := range val {
67
out[i], err = strconv.Atoi(d)
76
func (s *intSliceValue) GetSlice() []string {
77
out := make([]string, len(*s.value))
78
for i, d := range *s.value {
79
out[i] = strconv.Itoa(d)
84
func intSliceConv(val string) (interface{}, error) {
85
val = strings.Trim(val, "[]")
86
// Empty string would cause a slice with one (empty) entry
90
ss := strings.Split(val, ",")
91
out := make([]int, len(ss))
92
for i, d := range ss {
94
out[i], err = strconv.Atoi(d)
103
// GetIntSlice return the []int value of a flag with the given name
104
func (f *FlagSet) GetIntSlice(name string) ([]int, error) {
105
val, err := f.getFlagType(name, "intSlice", intSliceConv)
109
return val.([]int), nil
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.
114
func (f *FlagSet) IntSliceVar(p *[]int, name string, value []int, usage string) {
115
f.VarP(newIntSliceValue(value, p), name, "", usage)
118
// IntSliceVarP is like IntSliceVar, but accepts a shorthand letter that can be used after a single dash.
119
func (f *FlagSet) IntSliceVarP(p *[]int, name, shorthand string, value []int, usage string) {
120
f.VarP(newIntSliceValue(value, p), name, shorthand, usage)
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.
125
func IntSliceVar(p *[]int, name string, value []int, usage string) {
126
CommandLine.VarP(newIntSliceValue(value, p), name, "", usage)
129
// IntSliceVarP is like IntSliceVar, but accepts a shorthand letter that can be used after a single dash.
130
func IntSliceVarP(p *[]int, name, shorthand string, value []int, usage string) {
131
CommandLine.VarP(newIntSliceValue(value, p), name, shorthand, usage)
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.
136
func (f *FlagSet) IntSlice(name string, value []int, usage string) *[]int {
138
f.IntSliceVarP(&p, name, "", value, usage)
142
// IntSliceP is like IntSlice, but accepts a shorthand letter that can be used after a single dash.
143
func (f *FlagSet) IntSliceP(name, shorthand string, value []int, usage string) *[]int {
145
f.IntSliceVarP(&p, name, shorthand, value, usage)
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.
151
func IntSlice(name string, value []int, usage string) *[]int {
152
return CommandLine.IntSliceP(name, "", value, usage)
155
// IntSliceP is like IntSlice, but accepts a shorthand letter that can be used after a single dash.
156
func IntSliceP(name, shorthand string, value []int, usage string) *[]int {
157
return CommandLine.IntSliceP(name, shorthand, value, usage)