1 package pflag
2
3 import (
4 "fmt"
5 "strconv"
6 "strings"
7 )
8
9
10 type intSliceValue struct {
11 value *[]int
12 changed bool
13 }
14
15 func newIntSliceValue(val []int, p *[]int) *intSliceValue {
16 isv := new(intSliceValue)
17 isv.value = p
18 *isv.value = val
19 return isv
20 }
21
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 {
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
42 func (s *intSliceValue) Type() string {
43 return "intSlice"
44 }
45
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)
50 }
51 return "[" + strings.Join(out, ",") + "]"
52 }
53
54 func (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
63 func (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
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)
80 }
81 return out
82 }
83
84 func intSliceConv(val string) (interface{}, error) {
85 val = strings.Trim(val, "[]")
86
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
104 func (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
113
114 func (f *FlagSet) IntSliceVar(p *[]int, name string, value []int, usage string) {
115 f.VarP(newIntSliceValue(value, p), name, "", usage)
116 }
117
118
119 func (f *FlagSet) IntSliceVarP(p *[]int, name, shorthand string, value []int, usage string) {
120 f.VarP(newIntSliceValue(value, p), name, shorthand, usage)
121 }
122
123
124
125 func IntSliceVar(p *[]int, name string, value []int, usage string) {
126 CommandLine.VarP(newIntSliceValue(value, p), name, "", usage)
127 }
128
129
130 func IntSliceVarP(p *[]int, name, shorthand string, value []int, usage string) {
131 CommandLine.VarP(newIntSliceValue(value, p), name, shorthand, usage)
132 }
133
134
135
136 func (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
143 func (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
150
151 func IntSlice(name string, value []int, usage string) *[]int {
152 return CommandLine.IntSliceP(name, "", value, usage)
153 }
154
155
156 func IntSliceP(name, shorthand string, value []int, usage string) *[]int {
157 return CommandLine.IntSliceP(name, shorthand, value, usage)
158 }
159
View as plain text