forked from urfave/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flag_duration.go
44 lines (33 loc) · 942 Bytes
/
flag_duration.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
package cli
import (
"fmt"
"time"
)
type DurationFlag = FlagBase[time.Duration, NoConfig, durationValue]
// -- time.Duration Value
type durationValue time.Duration
// Below functions are to satisfy the ValueCreator interface
func (i durationValue) Create(val time.Duration, p *time.Duration, c NoConfig) Value {
*p = val
return (*durationValue)(p)
}
func (i durationValue) ToString(d time.Duration) string {
return fmt.Sprintf("%v", d)
}
// Below functions are to satisfy the flag.Value interface
func (d *durationValue) Set(s string) error {
v, err := time.ParseDuration(s)
if err != nil {
return err
}
*d = durationValue(v)
return err
}
func (d *durationValue) Get() any { return time.Duration(*d) }
func (d *durationValue) String() string { return (*time.Duration)(d).String() }
func (cCtx *Context) Duration(name string) time.Duration {
if v, ok := cCtx.Value(name).(time.Duration); ok {
return v
}
return 0
}