|
| 1 | +package pflag |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + "time" |
| 7 | +) |
| 8 | + |
| 9 | +// TimeValue adapts time.Time for use as a flag. |
| 10 | +type TimeValue struct { |
| 11 | + *time.Time |
| 12 | + formats []string |
| 13 | +} |
| 14 | + |
| 15 | +func newTimeValue(val time.Time, p *time.Time, formats []string) *TimeValue { |
| 16 | + *p = val |
| 17 | + return &TimeValue{ |
| 18 | + Time: p, |
| 19 | + formats: formats, |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +// Set time.Time value from string based on accepted formats. |
| 24 | +func (d *TimeValue) Set(s string) error { |
| 25 | + s = strings.TrimSpace(s) |
| 26 | + for _, f := range d.formats { |
| 27 | + v, err := time.Parse(f, s) |
| 28 | + if err != nil { |
| 29 | + continue |
| 30 | + } |
| 31 | + *d.Time = v |
| 32 | + return nil |
| 33 | + } |
| 34 | + |
| 35 | + formatsString := "" |
| 36 | + for i, f := range d.formats { |
| 37 | + if i > 0 { |
| 38 | + formatsString += ", " |
| 39 | + } |
| 40 | + formatsString += fmt.Sprintf("`%s`", f) |
| 41 | + } |
| 42 | + |
| 43 | + return fmt.Errorf("invalid time format `%s` must be one of: %s", s, formatsString) |
| 44 | +} |
| 45 | + |
| 46 | +// Type name for time.Time flags. |
| 47 | +func (d *TimeValue) Type() string { |
| 48 | + return "time" |
| 49 | +} |
| 50 | + |
| 51 | +func (d *TimeValue) String() string { return d.Time.Format(time.RFC3339Nano) } |
| 52 | + |
| 53 | +// GetTime return the time value of a flag with the given name |
| 54 | +func (f *FlagSet) GetTime(name string) (time.Time, error) { |
| 55 | + flag := f.Lookup(name) |
| 56 | + if flag == nil { |
| 57 | + err := fmt.Errorf("flag accessed but not defined: %s", name) |
| 58 | + return time.Time{}, err |
| 59 | + } |
| 60 | + |
| 61 | + if flag.Value.Type() != "time" { |
| 62 | + err := fmt.Errorf("trying to get %s value of flag of type %s", "time", flag.Value.Type()) |
| 63 | + return time.Time{}, err |
| 64 | + } |
| 65 | + |
| 66 | + val, ok := flag.Value.(*TimeValue) |
| 67 | + if !ok { |
| 68 | + return time.Time{}, fmt.Errorf("value %s is not a time", flag.Value) |
| 69 | + } |
| 70 | + |
| 71 | + return *val.Time, nil |
| 72 | +} |
| 73 | + |
| 74 | +// TimeVar defines a time.Time flag with specified name, default value, and usage string. |
| 75 | +// The argument p points to a time.Time variable in which to store the value of the flag. |
| 76 | +func (f *FlagSet) TimeVar(p *time.Time, name string, value time.Time, formats []string, usage string) { |
| 77 | + f.VarP(newTimeValue(value, p, formats), name, "", usage) |
| 78 | +} |
| 79 | + |
| 80 | +// TimeVarP is like TimeVar, but accepts a shorthand letter that can be used after a single dash. |
| 81 | +func (f *FlagSet) TimeVarP(p *time.Time, name, shorthand string, value time.Time, formats []string, usage string) { |
| 82 | + f.VarP(newTimeValue(value, p, formats), name, shorthand, usage) |
| 83 | +} |
| 84 | + |
| 85 | +// TimeVar defines a time.Time flag with specified name, default value, and usage string. |
| 86 | +// The argument p points to a time.Time variable in which to store the value of the flag. |
| 87 | +func TimeVar(p *time.Time, name string, value time.Time, formats []string, usage string) { |
| 88 | + CommandLine.VarP(newTimeValue(value, p, formats), name, "", usage) |
| 89 | +} |
| 90 | + |
| 91 | +// TimeVarP is like TimeVar, but accepts a shorthand letter that can be used after a single dash. |
| 92 | +func TimeVarP(p *time.Time, name, shorthand string, value time.Time, formats []string, usage string) { |
| 93 | + CommandLine.VarP(newTimeValue(value, p, formats), name, shorthand, usage) |
| 94 | +} |
| 95 | + |
| 96 | +// Time defines a time.Time flag with specified name, default value, and usage string. |
| 97 | +// The return value is the address of a time.Time variable that stores the value of the flag. |
| 98 | +func (f *FlagSet) Time(name string, value time.Time, formats []string, usage string) *time.Time { |
| 99 | + p := new(time.Time) |
| 100 | + f.TimeVarP(p, name, "", value, formats, usage) |
| 101 | + return p |
| 102 | +} |
| 103 | + |
| 104 | +// TimeP is like Time, but accepts a shorthand letter that can be used after a single dash. |
| 105 | +func (f *FlagSet) TimeP(name, shorthand string, value time.Time, formats []string, usage string) *time.Time { |
| 106 | + p := new(time.Time) |
| 107 | + f.TimeVarP(p, name, shorthand, value, formats, usage) |
| 108 | + return p |
| 109 | +} |
| 110 | + |
| 111 | +// Time defines a time.Time flag with specified name, default value, and usage string. |
| 112 | +// The return value is the address of a time.Time variable that stores the value of the flag. |
| 113 | +func Time(name string, value time.Time, formats []string, usage string) *time.Time { |
| 114 | + return CommandLine.TimeP(name, "", value, formats, usage) |
| 115 | +} |
| 116 | + |
| 117 | +// TimeP is like Time, but accepts a shorthand letter that can be used after a single dash. |
| 118 | +func TimeP(name, shorthand string, value time.Time, formats []string, usage string) *time.Time { |
| 119 | + return CommandLine.TimeP(name, shorthand, value, formats, usage) |
| 120 | +} |
0 commit comments