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