Path: blob/main/vendor/github.com/spf13/pflag/duration.go
2875 views
package pflag12import (3"time"4)56// -- time.Duration Value7type durationValue time.Duration89func newDurationValue(val time.Duration, p *time.Duration) *durationValue {10*p = val11return (*durationValue)(p)12}1314func (d *durationValue) Set(s string) error {15v, err := time.ParseDuration(s)16*d = durationValue(v)17return err18}1920func (d *durationValue) Type() string {21return "duration"22}2324func (d *durationValue) String() string { return (*time.Duration)(d).String() }2526func durationConv(sval string) (interface{}, error) {27return time.ParseDuration(sval)28}2930// GetDuration return the duration value of a flag with the given name31func (f *FlagSet) GetDuration(name string) (time.Duration, error) {32val, err := f.getFlagType(name, "duration", durationConv)33if err != nil {34return 0, err35}36return val.(time.Duration), nil37}3839// DurationVar defines a time.Duration flag with specified name, default value, and usage string.40// The argument p points to a time.Duration variable in which to store the value of the flag.41func (f *FlagSet) DurationVar(p *time.Duration, name string, value time.Duration, usage string) {42f.VarP(newDurationValue(value, p), name, "", usage)43}4445// DurationVarP is like DurationVar, but accepts a shorthand letter that can be used after a single dash.46func (f *FlagSet) DurationVarP(p *time.Duration, name, shorthand string, value time.Duration, usage string) {47f.VarP(newDurationValue(value, p), name, shorthand, usage)48}4950// DurationVar defines a time.Duration flag with specified name, default value, and usage string.51// The argument p points to a time.Duration variable in which to store the value of the flag.52func DurationVar(p *time.Duration, name string, value time.Duration, usage string) {53CommandLine.VarP(newDurationValue(value, p), name, "", usage)54}5556// DurationVarP is like DurationVar, but accepts a shorthand letter that can be used after a single dash.57func DurationVarP(p *time.Duration, name, shorthand string, value time.Duration, usage string) {58CommandLine.VarP(newDurationValue(value, p), name, shorthand, usage)59}6061// Duration defines a time.Duration flag with specified name, default value, and usage string.62// The return value is the address of a time.Duration variable that stores the value of the flag.63func (f *FlagSet) Duration(name string, value time.Duration, usage string) *time.Duration {64p := new(time.Duration)65f.DurationVarP(p, name, "", value, usage)66return p67}6869// DurationP is like Duration, but accepts a shorthand letter that can be used after a single dash.70func (f *FlagSet) DurationP(name, shorthand string, value time.Duration, usage string) *time.Duration {71p := new(time.Duration)72f.DurationVarP(p, name, shorthand, value, usage)73return p74}7576// Duration defines a time.Duration flag with specified name, default value, and usage string.77// The return value is the address of a time.Duration variable that stores the value of the flag.78func Duration(name string, value time.Duration, usage string) *time.Duration {79return CommandLine.DurationP(name, "", value, usage)80}8182// DurationP is like Duration, but accepts a shorthand letter that can be used after a single dash.83func DurationP(name, shorthand string, value time.Duration, usage string) *time.Duration {84return CommandLine.DurationP(name, shorthand, value, usage)85}868788