-
Notifications
You must be signed in to change notification settings - Fork 4
/
flag.go
69 lines (57 loc) · 931 Bytes
/
flag.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
package dads
import (
"strconv"
"time"
)
// Flag gets CLI flag values
type Flag string
// NewFlag ...
func NewFlag() *Flag {
s := Flag("")
return &s
}
// String gets string value
func (f *Flag) String() string {
if f != nil {
return string(*f)
}
return ""
}
// Set flag value
func (f *Flag) Set(val string) error {
*f = Flag(val)
return nil
}
// Bool gets flag bool value
func (f *Flag) Bool() bool {
if f != nil {
val, err := strconv.ParseBool(f.String())
if err != nil {
return false
}
return val
}
return false
}
// Int gets flag int value
func (f *Flag) Int() int {
if f != nil {
val, err := strconv.Atoi(f.String())
if err != nil {
return 0
}
return val
}
return 0
}
// Date gets flag date value
func (f *Flag) Date() *time.Time {
if f != nil {
date, err := time.Parse("2006-01-02 15:04:05", f.String())
if err != nil {
return nil
}
return &date
}
return nil
}