-
Notifications
You must be signed in to change notification settings - Fork 1
/
nvalue.go
46 lines (37 loc) · 970 Bytes
/
nvalue.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
//go:generate stringer -type NValue .
package argh
const (
OneOrMoreValue NValue = -2
ZeroOrMoreValue NValue = -1
ZeroValue NValue = 0
)
var (
zeroValuePtr = func() *NValue {
v := ZeroValue
return &v
}()
)
type NValue int
// Required returns whether the NValue represents a number that
// must be provided in order to be considered valid, which will
// always be true for OneOrMoreValue and will always be false for
// ZeroOrMoreValue.
func (nv NValue) Required() bool {
if nv == OneOrMoreValue {
return true
}
return int(nv) >= 1
}
// Contains returns whether the given *index* is within the range
// of the NValue, which will always be false for negative integers
// and will always be true for OneOrMoreValue or ZeroOrMoreValue.
func (nv NValue) Contains(i int) bool {
tracef("NValue.Contains(%v)", i)
if i < int(ZeroValue) {
return false
}
if nv == OneOrMoreValue || nv == ZeroOrMoreValue {
return true
}
return int(nv) > i
}