-
Notifications
You must be signed in to change notification settings - Fork 0
/
int.go
96 lines (83 loc) · 1.52 KB
/
int.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
package cli
import (
"fmt"
"strconv"
)
type Int struct {
target *int
defval *int
}
func (v *Int) Default(value int) {
v.defval = &value
}
type intValue struct {
inner *Int
set bool
}
func (v *intValue) verify(displayName string) error {
if v.set {
return nil
} else if v.inner.defval != nil {
*v.inner.target = *v.inner.defval
return nil
}
return fmt.Errorf("missing %s", displayName)
}
func (v *intValue) Set(val string) error {
n, err := strconv.Atoi(val)
if err != nil {
return err
}
*v.inner.target = n
v.set = true
return nil
}
func (v *intValue) String() string {
if v.inner == nil {
return ""
} else if v.set {
return strconv.Itoa(*v.inner.target)
} else if v.inner.defval != nil {
return strconv.Itoa(*v.inner.defval)
}
return ""
}
type OptionalInt struct {
target **int
defval *int
}
func (v *OptionalInt) Default(value int) {
v.defval = &value
}
type optionalIntValue struct {
inner *OptionalInt
set bool
}
func (v *optionalIntValue) verify(displayName string) error {
if v.set {
return nil
} else if v.inner.defval != nil {
*v.inner.target = v.inner.defval
return nil
}
return nil
}
func (v *optionalIntValue) Set(val string) error {
n, err := strconv.Atoi(val)
if err != nil {
return err
}
*v.inner.target = &n
v.set = true
return nil
}
func (v *optionalIntValue) String() string {
if v.inner == nil {
return ""
} else if v.set {
return strconv.Itoa(**v.inner.target)
} else if v.inner.defval != nil {
return strconv.Itoa(*v.inner.defval)
}
return ""
}