podman

Форк
0
166 строк · 5.0 Кб
1
package pflag
2

3
import (
4
	"fmt"
5
	"strings"
6
	"time"
7
)
8

9
// -- durationSlice Value
10
type durationSliceValue struct {
11
	value   *[]time.Duration
12
	changed bool
13
}
14

15
func newDurationSliceValue(val []time.Duration, p *[]time.Duration) *durationSliceValue {
16
	dsv := new(durationSliceValue)
17
	dsv.value = p
18
	*dsv.value = val
19
	return dsv
20
}
21

22
func (s *durationSliceValue) Set(val string) error {
23
	ss := strings.Split(val, ",")
24
	out := make([]time.Duration, len(ss))
25
	for i, d := range ss {
26
		var err error
27
		out[i], err = time.ParseDuration(d)
28
		if err != nil {
29
			return err
30
		}
31

32
	}
33
	if !s.changed {
34
		*s.value = out
35
	} else {
36
		*s.value = append(*s.value, out...)
37
	}
38
	s.changed = true
39
	return nil
40
}
41

42
func (s *durationSliceValue) Type() string {
43
	return "durationSlice"
44
}
45

46
func (s *durationSliceValue) String() string {
47
	out := make([]string, len(*s.value))
48
	for i, d := range *s.value {
49
		out[i] = fmt.Sprintf("%s", d)
50
	}
51
	return "[" + strings.Join(out, ",") + "]"
52
}
53

54
func (s *durationSliceValue) fromString(val string) (time.Duration, error) {
55
	return time.ParseDuration(val)
56
}
57

58
func (s *durationSliceValue) toString(val time.Duration) string {
59
	return fmt.Sprintf("%s", val)
60
}
61

62
func (s *durationSliceValue) Append(val string) error {
63
	i, err := s.fromString(val)
64
	if err != nil {
65
		return err
66
	}
67
	*s.value = append(*s.value, i)
68
	return nil
69
}
70

71
func (s *durationSliceValue) Replace(val []string) error {
72
	out := make([]time.Duration, len(val))
73
	for i, d := range val {
74
		var err error
75
		out[i], err = s.fromString(d)
76
		if err != nil {
77
			return err
78
		}
79
	}
80
	*s.value = out
81
	return nil
82
}
83

84
func (s *durationSliceValue) GetSlice() []string {
85
	out := make([]string, len(*s.value))
86
	for i, d := range *s.value {
87
		out[i] = s.toString(d)
88
	}
89
	return out
90
}
91

92
func durationSliceConv(val string) (interface{}, error) {
93
	val = strings.Trim(val, "[]")
94
	// Empty string would cause a slice with one (empty) entry
95
	if len(val) == 0 {
96
		return []time.Duration{}, nil
97
	}
98
	ss := strings.Split(val, ",")
99
	out := make([]time.Duration, len(ss))
100
	for i, d := range ss {
101
		var err error
102
		out[i], err = time.ParseDuration(d)
103
		if err != nil {
104
			return nil, err
105
		}
106

107
	}
108
	return out, nil
109
}
110

111
// GetDurationSlice returns the []time.Duration value of a flag with the given name
112
func (f *FlagSet) GetDurationSlice(name string) ([]time.Duration, error) {
113
	val, err := f.getFlagType(name, "durationSlice", durationSliceConv)
114
	if err != nil {
115
		return []time.Duration{}, err
116
	}
117
	return val.([]time.Duration), nil
118
}
119

120
// DurationSliceVar defines a durationSlice flag with specified name, default value, and usage string.
121
// The argument p points to a []time.Duration variable in which to store the value of the flag.
122
func (f *FlagSet) DurationSliceVar(p *[]time.Duration, name string, value []time.Duration, usage string) {
123
	f.VarP(newDurationSliceValue(value, p), name, "", usage)
124
}
125

126
// DurationSliceVarP is like DurationSliceVar, but accepts a shorthand letter that can be used after a single dash.
127
func (f *FlagSet) DurationSliceVarP(p *[]time.Duration, name, shorthand string, value []time.Duration, usage string) {
128
	f.VarP(newDurationSliceValue(value, p), name, shorthand, usage)
129
}
130

131
// DurationSliceVar defines a duration[] flag with specified name, default value, and usage string.
132
// The argument p points to a duration[] variable in which to store the value of the flag.
133
func DurationSliceVar(p *[]time.Duration, name string, value []time.Duration, usage string) {
134
	CommandLine.VarP(newDurationSliceValue(value, p), name, "", usage)
135
}
136

137
// DurationSliceVarP is like DurationSliceVar, but accepts a shorthand letter that can be used after a single dash.
138
func DurationSliceVarP(p *[]time.Duration, name, shorthand string, value []time.Duration, usage string) {
139
	CommandLine.VarP(newDurationSliceValue(value, p), name, shorthand, usage)
140
}
141

142
// DurationSlice defines a []time.Duration flag with specified name, default value, and usage string.
143
// The return value is the address of a []time.Duration variable that stores the value of the flag.
144
func (f *FlagSet) DurationSlice(name string, value []time.Duration, usage string) *[]time.Duration {
145
	p := []time.Duration{}
146
	f.DurationSliceVarP(&p, name, "", value, usage)
147
	return &p
148
}
149

150
// DurationSliceP is like DurationSlice, but accepts a shorthand letter that can be used after a single dash.
151
func (f *FlagSet) DurationSliceP(name, shorthand string, value []time.Duration, usage string) *[]time.Duration {
152
	p := []time.Duration{}
153
	f.DurationSliceVarP(&p, name, shorthand, value, usage)
154
	return &p
155
}
156

157
// DurationSlice defines a []time.Duration flag with specified name, default value, and usage string.
158
// The return value is the address of a []time.Duration variable that stores the value of the flag.
159
func DurationSlice(name string, value []time.Duration, usage string) *[]time.Duration {
160
	return CommandLine.DurationSliceP(name, "", value, usage)
161
}
162

163
// DurationSliceP is like DurationSlice, but accepts a shorthand letter that can be used after a single dash.
164
func DurationSliceP(name, shorthand string, value []time.Duration, usage string) *[]time.Duration {
165
	return CommandLine.DurationSliceP(name, shorthand, value, usage)
166
}
167

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.