Path: blob/main/vendor/github.com/spf13/pflag/time.go
2875 views
package pflag12import (3"fmt"4"strings"5"time"6)78// TimeValue adapts time.Time for use as a flag.9type timeValue struct {10*time.Time11formats []string12}1314func newTimeValue(val time.Time, p *time.Time, formats []string) *timeValue {15*p = val16return &timeValue{17Time: p,18formats: formats,19}20}2122// Set time.Time value from string based on accepted formats.23func (d *timeValue) Set(s string) error {24s = strings.TrimSpace(s)25for _, f := range d.formats {26v, err := time.Parse(f, s)27if err != nil {28continue29}30*d.Time = v31return nil32}3334formatsString := ""35for i, f := range d.formats {36if i > 0 {37formatsString += ", "38}39formatsString += fmt.Sprintf("`%s`", f)40}4142return fmt.Errorf("invalid time format `%s` must be one of: %s", s, formatsString)43}4445// Type name for time.Time flags.46func (d *timeValue) Type() string {47return "time"48}4950func (d *timeValue) String() string {51if d.Time.IsZero() {52return ""53} else {54return d.Time.Format(time.RFC3339Nano)55}56}5758// GetTime return the time value of a flag with the given name59func (f *FlagSet) GetTime(name string) (time.Time, error) {60flag := f.Lookup(name)61if flag == nil {62err := fmt.Errorf("flag accessed but not defined: %s", name)63return time.Time{}, err64}6566if flag.Value.Type() != "time" {67err := fmt.Errorf("trying to get %s value of flag of type %s", "time", flag.Value.Type())68return time.Time{}, err69}7071val, ok := flag.Value.(*timeValue)72if !ok {73return time.Time{}, fmt.Errorf("value %s is not a time", flag.Value)74}7576return *val.Time, nil77}7879// TimeVar defines a time.Time flag with specified name, default value, and usage string.80// The argument p points to a time.Time variable in which to store the value of the flag.81func (f *FlagSet) TimeVar(p *time.Time, name string, value time.Time, formats []string, usage string) {82f.TimeVarP(p, name, "", value, formats, usage)83}8485// TimeVarP is like TimeVar, but accepts a shorthand letter that can be used after a single dash.86func (f *FlagSet) TimeVarP(p *time.Time, name, shorthand string, value time.Time, formats []string, usage string) {87f.VarP(newTimeValue(value, p, formats), name, shorthand, usage)88}8990// TimeVar defines a time.Time flag with specified name, default value, and usage string.91// The argument p points to a time.Time variable in which to store the value of the flag.92func TimeVar(p *time.Time, name string, value time.Time, formats []string, usage string) {93CommandLine.TimeVarP(p, name, "", value, formats, usage)94}9596// TimeVarP is like TimeVar, but accepts a shorthand letter that can be used after a single dash.97func TimeVarP(p *time.Time, name, shorthand string, value time.Time, formats []string, usage string) {98CommandLine.VarP(newTimeValue(value, p, formats), name, shorthand, usage)99}100101// Time defines a time.Time flag with specified name, default value, and usage string.102// The return value is the address of a time.Time variable that stores the value of the flag.103func (f *FlagSet) Time(name string, value time.Time, formats []string, usage string) *time.Time {104return f.TimeP(name, "", value, formats, usage)105}106107// TimeP is like Time, but accepts a shorthand letter that can be used after a single dash.108func (f *FlagSet) TimeP(name, shorthand string, value time.Time, formats []string, usage string) *time.Time {109p := new(time.Time)110f.TimeVarP(p, name, shorthand, value, formats, usage)111return p112}113114// Time defines a time.Time flag with specified name, default value, and usage string.115// The return value is the address of a time.Time variable that stores the value of the flag.116func Time(name string, value time.Time, formats []string, usage string) *time.Time {117return CommandLine.TimeP(name, "", value, formats, usage)118}119120// TimeP is like Time, but accepts a shorthand letter that can be used after a single dash.121func TimeP(name, shorthand string, value time.Time, formats []string, usage string) *time.Time {122return CommandLine.TimeP(name, shorthand, value, formats, usage)123}124125126