forked from cornfeedhobo/pflag
-
Notifications
You must be signed in to change notification settings - Fork 1
/
time.go
98 lines (82 loc) · 2.86 KB
/
time.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package zflag
import (
"fmt"
"strings"
"time"
)
// TimeValue adapts time.Time for use as a flag.
type TimeValue struct {
*time.Time
formats []string
}
var _ Value = (*TimeValue)(nil)
var _ Getter = (*TimeValue)(nil)
var _ Typed = (*TimeValue)(nil)
func newTimeValue(val time.Time, p *time.Time, formats []string) *TimeValue {
*p = val
return &TimeValue{
Time: p,
formats: formats,
}
}
// Set time.Time value from string based on accepted formats.
func (d *TimeValue) Set(s string) error {
s = strings.TrimSpace(s)
for _, f := range d.formats {
v, err := time.Parse(f, s)
if err != nil {
continue
}
*d.Time = v
return nil
}
formatsString := "'" + strings.Join(d.formats, "', '") + "'"
return fmt.Errorf("invalid time format '%s' must be one of: %s", s, formatsString)
}
func (d *TimeValue) Get() interface{} {
return *d.Time
}
// Type name for time.Time flags.
func (d *TimeValue) Type() string {
return "time"
}
func (d *TimeValue) String() string { return d.Time.Format(time.RFC3339Nano) }
// GetTime return the time value of a flag with the given name
func (fs *FlagSet) GetTime(name string) (time.Time, error) {
val, err := fs.getFlagValue(name, "time")
if err != nil {
return time.Time{}, err
}
return val.(time.Time), nil
}
// MustGetTime is like GetTime, but panics on error.
func (fs *FlagSet) MustGetTime(name string) time.Time {
val, err := fs.GetTime(name)
if err != nil {
panic(err)
}
return val
}
// TimeVar defines a time.Time flag with specified name, default value, and usage string.
// The argument p points to a time.Time variable in which to store the value of the flag.
func (fs *FlagSet) TimeVar(p *time.Time, name string, value time.Time, formats []string, usage string, opts ...Opt) {
fs.Var(newTimeValue(value, p, formats), name, usage, opts...)
}
// TimeVar defines a time.Time flag with specified name, default value, and usage string.
// The argument p points to a time.Time variable in which to store the value of the flag.
func TimeVar(p *time.Time, name string, value time.Time, formats []string, usage string, opts ...Opt) {
CommandLine.Var(newTimeValue(value, p, formats), name, usage, opts...)
}
// Time defines a time.Time flag with specified name, default value, and usage string.
// The return value is the address of a time.Time variable that stores the value of the flag.
func (fs *FlagSet) Time(name string, value time.Time, formats []string, usage string, opts ...Opt) *time.Time {
p := new(time.Time)
fs.TimeVar(p, name, value, formats, usage, opts...)
return p
}
// Time is like Time, but accepts a shorthand letter that can be used after a single dash.
func Time(name string, value time.Time, formats []string, usage string, opts ...Opt) *time.Time {
return CommandLine.Time(name, value, formats, usage, opts...)
}