Path: blob/main/vendor/github.com/spf13/pflag/duration_slice.go
2875 views
package pflag12import (3"fmt"4"strings"5"time"6)78// -- durationSlice Value9type durationSliceValue struct {10value *[]time.Duration11changed bool12}1314func newDurationSliceValue(val []time.Duration, p *[]time.Duration) *durationSliceValue {15dsv := new(durationSliceValue)16dsv.value = p17*dsv.value = val18return dsv19}2021func (s *durationSliceValue) Set(val string) error {22ss := strings.Split(val, ",")23out := make([]time.Duration, len(ss))24for i, d := range ss {25var err error26out[i], err = time.ParseDuration(d)27if err != nil {28return err29}3031}32if !s.changed {33*s.value = out34} else {35*s.value = append(*s.value, out...)36}37s.changed = true38return nil39}4041func (s *durationSliceValue) Type() string {42return "durationSlice"43}4445func (s *durationSliceValue) String() string {46out := make([]string, len(*s.value))47for i, d := range *s.value {48out[i] = fmt.Sprintf("%s", d)49}50return "[" + strings.Join(out, ",") + "]"51}5253func (s *durationSliceValue) fromString(val string) (time.Duration, error) {54return time.ParseDuration(val)55}5657func (s *durationSliceValue) toString(val time.Duration) string {58return fmt.Sprintf("%s", val)59}6061func (s *durationSliceValue) Append(val string) error {62i, err := s.fromString(val)63if err != nil {64return err65}66*s.value = append(*s.value, i)67return nil68}6970func (s *durationSliceValue) Replace(val []string) error {71out := make([]time.Duration, len(val))72for i, d := range val {73var err error74out[i], err = s.fromString(d)75if err != nil {76return err77}78}79*s.value = out80return nil81}8283func (s *durationSliceValue) GetSlice() []string {84out := make([]string, len(*s.value))85for i, d := range *s.value {86out[i] = s.toString(d)87}88return out89}9091func durationSliceConv(val string) (interface{}, error) {92val = strings.Trim(val, "[]")93// Empty string would cause a slice with one (empty) entry94if len(val) == 0 {95return []time.Duration{}, nil96}97ss := strings.Split(val, ",")98out := make([]time.Duration, len(ss))99for i, d := range ss {100var err error101out[i], err = time.ParseDuration(d)102if err != nil {103return nil, err104}105106}107return out, nil108}109110// GetDurationSlice returns the []time.Duration value of a flag with the given name111func (f *FlagSet) GetDurationSlice(name string) ([]time.Duration, error) {112val, err := f.getFlagType(name, "durationSlice", durationSliceConv)113if err != nil {114return []time.Duration{}, err115}116return val.([]time.Duration), nil117}118119// DurationSliceVar defines a durationSlice flag with specified name, default value, and usage string.120// The argument p points to a []time.Duration variable in which to store the value of the flag.121func (f *FlagSet) DurationSliceVar(p *[]time.Duration, name string, value []time.Duration, usage string) {122f.VarP(newDurationSliceValue(value, p), name, "", usage)123}124125// DurationSliceVarP is like DurationSliceVar, but accepts a shorthand letter that can be used after a single dash.126func (f *FlagSet) DurationSliceVarP(p *[]time.Duration, name, shorthand string, value []time.Duration, usage string) {127f.VarP(newDurationSliceValue(value, p), name, shorthand, usage)128}129130// DurationSliceVar defines a duration[] flag with specified name, default value, and usage string.131// The argument p points to a duration[] variable in which to store the value of the flag.132func DurationSliceVar(p *[]time.Duration, name string, value []time.Duration, usage string) {133CommandLine.VarP(newDurationSliceValue(value, p), name, "", usage)134}135136// DurationSliceVarP is like DurationSliceVar, but accepts a shorthand letter that can be used after a single dash.137func DurationSliceVarP(p *[]time.Duration, name, shorthand string, value []time.Duration, usage string) {138CommandLine.VarP(newDurationSliceValue(value, p), name, shorthand, usage)139}140141// DurationSlice defines a []time.Duration flag with specified name, default value, and usage string.142// The return value is the address of a []time.Duration variable that stores the value of the flag.143func (f *FlagSet) DurationSlice(name string, value []time.Duration, usage string) *[]time.Duration {144p := []time.Duration{}145f.DurationSliceVarP(&p, name, "", value, usage)146return &p147}148149// DurationSliceP is like DurationSlice, but accepts a shorthand letter that can be used after a single dash.150func (f *FlagSet) DurationSliceP(name, shorthand string, value []time.Duration, usage string) *[]time.Duration {151p := []time.Duration{}152f.DurationSliceVarP(&p, name, shorthand, value, usage)153return &p154}155156// DurationSlice defines a []time.Duration flag with specified name, default value, and usage string.157// The return value is the address of a []time.Duration variable that stores the value of the flag.158func DurationSlice(name string, value []time.Duration, usage string) *[]time.Duration {159return CommandLine.DurationSliceP(name, "", value, usage)160}161162// DurationSliceP is like DurationSlice, but accepts a shorthand letter that can be used after a single dash.163func DurationSliceP(name, shorthand string, value []time.Duration, usage string) *[]time.Duration {164return CommandLine.DurationSliceP(name, shorthand, value, usage)165}166167168