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