podman
174 строки · 4.7 Кб
1package pflag2
3import (4"fmt"5"strconv"6"strings"7)
8
9// -- int32Slice Value
10type int32SliceValue struct {11value *[]int3212changed bool13}
14
15func newInt32SliceValue(val []int32, p *[]int32) *int32SliceValue {16isv := new(int32SliceValue)17isv.value = p18*isv.value = val19return isv20}
21
22func (s *int32SliceValue) Set(val string) error {23ss := strings.Split(val, ",")24out := make([]int32, len(ss))25for i, d := range ss {26var err error27var temp64 int6428temp64, err = strconv.ParseInt(d, 0, 32)29if err != nil {30return err31}32out[i] = int32(temp64)33
34}35if !s.changed {36*s.value = out37} else {38*s.value = append(*s.value, out...)39}40s.changed = true41return nil42}
43
44func (s *int32SliceValue) Type() string {45return "int32Slice"46}
47
48func (s *int32SliceValue) String() string {49out := make([]string, len(*s.value))50for i, d := range *s.value {51out[i] = fmt.Sprintf("%d", d)52}53return "[" + strings.Join(out, ",") + "]"54}
55
56func (s *int32SliceValue) fromString(val string) (int32, error) {57t64, err := strconv.ParseInt(val, 0, 32)58if err != nil {59return 0, err60}61return int32(t64), nil62}
63
64func (s *int32SliceValue) toString(val int32) string {65return fmt.Sprintf("%d", val)66}
67
68func (s *int32SliceValue) Append(val string) error {69i, err := s.fromString(val)70if err != nil {71return err72}73*s.value = append(*s.value, i)74return nil75}
76
77func (s *int32SliceValue) Replace(val []string) error {78out := make([]int32, len(val))79for i, d := range val {80var err error81out[i], err = s.fromString(d)82if err != nil {83return err84}85}86*s.value = out87return nil88}
89
90func (s *int32SliceValue) GetSlice() []string {91out := make([]string, len(*s.value))92for i, d := range *s.value {93out[i] = s.toString(d)94}95return out96}
97
98func int32SliceConv(val string) (interface{}, error) {99val = strings.Trim(val, "[]")100// Empty string would cause a slice with one (empty) entry101if len(val) == 0 {102return []int32{}, nil103}104ss := strings.Split(val, ",")105out := make([]int32, len(ss))106for i, d := range ss {107var err error108var temp64 int64109temp64, err = strconv.ParseInt(d, 0, 32)110if err != nil {111return nil, err112}113out[i] = int32(temp64)114
115}116return out, nil117}
118
119// GetInt32Slice return the []int32 value of a flag with the given name
120func (f *FlagSet) GetInt32Slice(name string) ([]int32, error) {121val, err := f.getFlagType(name, "int32Slice", int32SliceConv)122if err != nil {123return []int32{}, err124}125return val.([]int32), nil126}
127
128// Int32SliceVar defines a int32Slice flag with specified name, default value, and usage string.
129// The argument p points to a []int32 variable in which to store the value of the flag.
130func (f *FlagSet) Int32SliceVar(p *[]int32, name string, value []int32, usage string) {131f.VarP(newInt32SliceValue(value, p), name, "", usage)132}
133
134// Int32SliceVarP is like Int32SliceVar, but accepts a shorthand letter that can be used after a single dash.
135func (f *FlagSet) Int32SliceVarP(p *[]int32, name, shorthand string, value []int32, usage string) {136f.VarP(newInt32SliceValue(value, p), name, shorthand, usage)137}
138
139// Int32SliceVar defines a int32[] flag with specified name, default value, and usage string.
140// The argument p points to a int32[] variable in which to store the value of the flag.
141func Int32SliceVar(p *[]int32, name string, value []int32, usage string) {142CommandLine.VarP(newInt32SliceValue(value, p), name, "", usage)143}
144
145// Int32SliceVarP is like Int32SliceVar, but accepts a shorthand letter that can be used after a single dash.
146func Int32SliceVarP(p *[]int32, name, shorthand string, value []int32, usage string) {147CommandLine.VarP(newInt32SliceValue(value, p), name, shorthand, usage)148}
149
150// Int32Slice defines a []int32 flag with specified name, default value, and usage string.
151// The return value is the address of a []int32 variable that stores the value of the flag.
152func (f *FlagSet) Int32Slice(name string, value []int32, usage string) *[]int32 {153p := []int32{}154f.Int32SliceVarP(&p, name, "", value, usage)155return &p156}
157
158// Int32SliceP is like Int32Slice, but accepts a shorthand letter that can be used after a single dash.
159func (f *FlagSet) Int32SliceP(name, shorthand string, value []int32, usage string) *[]int32 {160p := []int32{}161f.Int32SliceVarP(&p, name, shorthand, value, usage)162return &p163}
164
165// Int32Slice defines a []int32 flag with specified name, default value, and usage string.
166// The return value is the address of a []int32 variable that stores the value of the flag.
167func Int32Slice(name string, value []int32, usage string) *[]int32 {168return CommandLine.Int32SliceP(name, "", value, usage)169}
170
171// Int32SliceP is like Int32Slice, but accepts a shorthand letter that can be used after a single dash.
172func Int32SliceP(name, shorthand string, value []int32, usage string) *[]int32 {173return CommandLine.Int32SliceP(name, shorthand, value, usage)174}
175