OSDN Git Service

Hulk did something
[bytom/vapor.git] / vendor / github.com / spf13 / pflag / int_slice.go
1 package pflag
2
3 import (
4         "fmt"
5         "strconv"
6         "strings"
7 )
8
9 // -- intSlice Value
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 intSliceConv(val string) (interface{}, error) {
55         val = strings.Trim(val, "[]")
56         // Empty string would cause a slice with one (empty) entry
57         if len(val) == 0 {
58                 return []int{}, nil
59         }
60         ss := strings.Split(val, ",")
61         out := make([]int, len(ss))
62         for i, d := range ss {
63                 var err error
64                 out[i], err = strconv.Atoi(d)
65                 if err != nil {
66                         return nil, err
67                 }
68
69         }
70         return out, nil
71 }
72
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)
76         if err != nil {
77                 return []int{}, err
78         }
79         return val.([]int), nil
80 }
81
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)
86 }
87
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)
91 }
92
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)
97 }
98
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)
102 }
103
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 {
107         p := []int{}
108         f.IntSliceVarP(&p, name, "", value, usage)
109         return &p
110 }
111
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 {
114         p := []int{}
115         f.IntSliceVarP(&p, name, shorthand, value, usage)
116         return &p
117 }
118
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)
123 }
124
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)
128 }