Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
kardolus
GitHub Repository: kardolus/chatgpt-cli
Path: blob/main/vendor/github.com/spf13/pflag/time.go
2875 views
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 {
52
if d.Time.IsZero() {
53
return ""
54
} else {
55
return d.Time.Format(time.RFC3339Nano)
56
}
57
}
58
59
// GetTime return the time value of a flag with the given name
60
func (f *FlagSet) GetTime(name string) (time.Time, error) {
61
flag := f.Lookup(name)
62
if flag == nil {
63
err := fmt.Errorf("flag accessed but not defined: %s", name)
64
return time.Time{}, err
65
}
66
67
if flag.Value.Type() != "time" {
68
err := fmt.Errorf("trying to get %s value of flag of type %s", "time", flag.Value.Type())
69
return time.Time{}, err
70
}
71
72
val, ok := flag.Value.(*timeValue)
73
if !ok {
74
return time.Time{}, fmt.Errorf("value %s is not a time", flag.Value)
75
}
76
77
return *val.Time, nil
78
}
79
80
// TimeVar defines a time.Time flag with specified name, default value, and usage string.
81
// The argument p points to a time.Time variable in which to store the value of the flag.
82
func (f *FlagSet) TimeVar(p *time.Time, name string, value time.Time, formats []string, usage string) {
83
f.TimeVarP(p, name, "", value, formats, usage)
84
}
85
86
// TimeVarP is like TimeVar, but accepts a shorthand letter that can be used after a single dash.
87
func (f *FlagSet) TimeVarP(p *time.Time, name, shorthand string, value time.Time, formats []string, usage string) {
88
f.VarP(newTimeValue(value, p, formats), name, shorthand, usage)
89
}
90
91
// TimeVar defines a time.Time flag with specified name, default value, and usage string.
92
// The argument p points to a time.Time variable in which to store the value of the flag.
93
func TimeVar(p *time.Time, name string, value time.Time, formats []string, usage string) {
94
CommandLine.TimeVarP(p, name, "", value, formats, usage)
95
}
96
97
// TimeVarP is like TimeVar, but accepts a shorthand letter that can be used after a single dash.
98
func TimeVarP(p *time.Time, name, shorthand string, value time.Time, formats []string, usage string) {
99
CommandLine.VarP(newTimeValue(value, p, formats), name, shorthand, usage)
100
}
101
102
// Time defines a time.Time flag with specified name, default value, and usage string.
103
// The return value is the address of a time.Time variable that stores the value of the flag.
104
func (f *FlagSet) Time(name string, value time.Time, formats []string, usage string) *time.Time {
105
return f.TimeP(name, "", value, formats, usage)
106
}
107
108
// TimeP is like Time, but accepts a shorthand letter that can be used after a single dash.
109
func (f *FlagSet) TimeP(name, shorthand string, value time.Time, formats []string, usage string) *time.Time {
110
p := new(time.Time)
111
f.TimeVarP(p, name, shorthand, value, formats, usage)
112
return p
113
}
114
115
// Time defines a time.Time flag with specified name, default value, and usage string.
116
// The return value is the address of a time.Time variable that stores the value of the flag.
117
func Time(name string, value time.Time, formats []string, usage string) *time.Time {
118
return CommandLine.TimeP(name, "", value, formats, usage)
119
}
120
121
// TimeP is like Time, but accepts a shorthand letter that can be used after a single dash.
122
func TimeP(name, shorthand string, value time.Time, formats []string, usage string) *time.Time {
123
return CommandLine.TimeP(name, shorthand, value, formats, usage)
124
}
125
126