podman
163 строки · 5.0 Кб
1package pflag
2
3import (
4"bytes"
5"encoding/csv"
6"strings"
7)
8
9// -- stringSlice Value
10type stringSliceValue struct {
11value *[]string
12changed bool
13}
14
15func newStringSliceValue(val []string, p *[]string) *stringSliceValue {
16ssv := new(stringSliceValue)
17ssv.value = p
18*ssv.value = val
19return ssv
20}
21
22func readAsCSV(val string) ([]string, error) {
23if val == "" {
24return []string{}, nil
25}
26stringReader := strings.NewReader(val)
27csvReader := csv.NewReader(stringReader)
28return csvReader.Read()
29}
30
31func writeAsCSV(vals []string) (string, error) {
32b := &bytes.Buffer{}
33w := csv.NewWriter(b)
34err := w.Write(vals)
35if err != nil {
36return "", err
37}
38w.Flush()
39return strings.TrimSuffix(b.String(), "\n"), nil
40}
41
42func (s *stringSliceValue) Set(val string) error {
43v, err := readAsCSV(val)
44if err != nil {
45return err
46}
47if !s.changed {
48*s.value = v
49} else {
50*s.value = append(*s.value, v...)
51}
52s.changed = true
53return nil
54}
55
56func (s *stringSliceValue) Type() string {
57return "stringSlice"
58}
59
60func (s *stringSliceValue) String() string {
61str, _ := writeAsCSV(*s.value)
62return "[" + str + "]"
63}
64
65func (s *stringSliceValue) Append(val string) error {
66*s.value = append(*s.value, val)
67return nil
68}
69
70func (s *stringSliceValue) Replace(val []string) error {
71*s.value = val
72return nil
73}
74
75func (s *stringSliceValue) GetSlice() []string {
76return *s.value
77}
78
79func stringSliceConv(sval string) (interface{}, error) {
80sval = sval[1 : len(sval)-1]
81// An empty string would cause a slice with one (empty) string
82if len(sval) == 0 {
83return []string{}, nil
84}
85return readAsCSV(sval)
86}
87
88// GetStringSlice return the []string value of a flag with the given name
89func (f *FlagSet) GetStringSlice(name string) ([]string, error) {
90val, err := f.getFlagType(name, "stringSlice", stringSliceConv)
91if err != nil {
92return []string{}, err
93}
94return val.([]string), nil
95}
96
97// StringSliceVar defines a string flag with specified name, default value, and usage string.
98// The argument p points to a []string variable in which to store the value of the flag.
99// Compared to StringArray flags, StringSlice flags take comma-separated value as arguments and split them accordingly.
100// For example:
101// --ss="v1,v2" --ss="v3"
102// will result in
103// []string{"v1", "v2", "v3"}
104func (f *FlagSet) StringSliceVar(p *[]string, name string, value []string, usage string) {
105f.VarP(newStringSliceValue(value, p), name, "", usage)
106}
107
108// StringSliceVarP is like StringSliceVar, but accepts a shorthand letter that can be used after a single dash.
109func (f *FlagSet) StringSliceVarP(p *[]string, name, shorthand string, value []string, usage string) {
110f.VarP(newStringSliceValue(value, p), name, shorthand, usage)
111}
112
113// StringSliceVar defines a string flag with specified name, default value, and usage string.
114// The argument p points to a []string variable in which to store the value of the flag.
115// Compared to StringArray flags, StringSlice flags take comma-separated value as arguments and split them accordingly.
116// For example:
117// --ss="v1,v2" --ss="v3"
118// will result in
119// []string{"v1", "v2", "v3"}
120func StringSliceVar(p *[]string, name string, value []string, usage string) {
121CommandLine.VarP(newStringSliceValue(value, p), name, "", usage)
122}
123
124// StringSliceVarP is like StringSliceVar, but accepts a shorthand letter that can be used after a single dash.
125func StringSliceVarP(p *[]string, name, shorthand string, value []string, usage string) {
126CommandLine.VarP(newStringSliceValue(value, p), name, shorthand, usage)
127}
128
129// StringSlice defines a string flag with specified name, default value, and usage string.
130// The return value is the address of a []string variable that stores the value of the flag.
131// Compared to StringArray flags, StringSlice flags take comma-separated value as arguments and split them accordingly.
132// For example:
133// --ss="v1,v2" --ss="v3"
134// will result in
135// []string{"v1", "v2", "v3"}
136func (f *FlagSet) StringSlice(name string, value []string, usage string) *[]string {
137p := []string{}
138f.StringSliceVarP(&p, name, "", value, usage)
139return &p
140}
141
142// StringSliceP is like StringSlice, but accepts a shorthand letter that can be used after a single dash.
143func (f *FlagSet) StringSliceP(name, shorthand string, value []string, usage string) *[]string {
144p := []string{}
145f.StringSliceVarP(&p, name, shorthand, value, usage)
146return &p
147}
148
149// StringSlice defines a string flag with specified name, default value, and usage string.
150// The return value is the address of a []string variable that stores the value of the flag.
151// Compared to StringArray flags, StringSlice flags take comma-separated value as arguments and split them accordingly.
152// For example:
153// --ss="v1,v2" --ss="v3"
154// will result in
155// []string{"v1", "v2", "v3"}
156func StringSlice(name string, value []string, usage string) *[]string {
157return CommandLine.StringSliceP(name, "", value, usage)
158}
159
160// StringSliceP is like StringSlice, but accepts a shorthand letter that can be used after a single dash.
161func StringSliceP(name, shorthand string, value []string, usage string) *[]string {
162return CommandLine.StringSliceP(name, shorthand, value, usage)
163}
164