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