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 intSliceConv(val string) (interface{}, error) {
55 val = strings.Trim(val, "[]")
56 // Empty string would cause a slice with one (empty) entry
60 ss := strings.Split(val, ",")
61 out := make([]int, len(ss))
62 for i, d := range ss {
64 out[i], err = strconv.Atoi(d)
73 // GetIntSlice return the []int value of a flag with the given name
74 func (f *FlagSet) GetIntSlice(name string) ([]int, error) {
75 val, err := f.getFlagType(name, "intSlice", intSliceConv)
79 return val.([]int), nil
82 // IntSliceVar defines a intSlice flag with specified name, default value, and usage string.
83 // The argument p points to a []int variable in which to store the value of the flag.
84 func (f *FlagSet) IntSliceVar(p *[]int, name string, value []int, usage string) {
85 f.VarP(newIntSliceValue(value, p), name, "", usage)
88 // IntSliceVarP is like IntSliceVar, but accepts a shorthand letter that can be used after a single dash.
89 func (f *FlagSet) IntSliceVarP(p *[]int, name, shorthand string, value []int, usage string) {
90 f.VarP(newIntSliceValue(value, p), name, shorthand, usage)
93 // IntSliceVar defines a int[] flag with specified name, default value, and usage string.
94 // The argument p points to a int[] variable in which to store the value of the flag.
95 func IntSliceVar(p *[]int, name string, value []int, usage string) {
96 CommandLine.VarP(newIntSliceValue(value, p), name, "", usage)
99 // IntSliceVarP is like IntSliceVar, but accepts a shorthand letter that can be used after a single dash.
100 func IntSliceVarP(p *[]int, name, shorthand string, value []int, usage string) {
101 CommandLine.VarP(newIntSliceValue(value, p), name, shorthand, usage)
104 // IntSlice defines a []int flag with specified name, default value, and usage string.
105 // The return value is the address of a []int variable that stores the value of the flag.
106 func (f *FlagSet) IntSlice(name string, value []int, usage string) *[]int {
108 f.IntSliceVarP(&p, name, "", value, usage)
112 // IntSliceP is like IntSlice, but accepts a shorthand letter that can be used after a single dash.
113 func (f *FlagSet) IntSliceP(name, shorthand string, value []int, usage string) *[]int {
115 f.IntSliceVarP(&p, name, shorthand, value, usage)
119 // IntSlice defines a []int flag with specified name, default value, and usage string.
120 // The return value is the address of a []int variable that stores the value of the flag.
121 func IntSlice(name string, value []int, usage string) *[]int {
122 return CommandLine.IntSliceP(name, "", value, usage)
125 // IntSliceP is like IntSlice, but accepts a shorthand letter that can be used after a single dash.
126 func IntSliceP(name, shorthand string, value []int, usage string) *[]int {
127 return CommandLine.IntSliceP(name, shorthand, value, usage)