-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathint.go
56 lines (46 loc) · 1.34 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
package optparse
import (
"strconv"
)
// IntValue represents a signed integer value
type IntValue int
// IntValueAssertion defines an assertion if the Value is an IntValue
type IntValueAssertion interface {
Value
IsInt() bool
}
// NewIntValue creates a new IntValue
func NewIntValue(val int, p *int) *IntValue {
*p = val
return (*IntValue)(p)
}
// Set sets the value
func (i *IntValue) Set(val string) error {
v, err := strconv.ParseInt(val, 0, 64)
*i = IntValue(v)
return err
}
// IsInt returns true if the Value is an IntValue
func (i *IntValue) IsInt() bool {
return true
}
// IntVar defines a signed integer option with a pointer to its value
func (o *OptionParser) IntVar(p *int, long string, short rune, def int) {
opt := &Option{long, short, NewIntValue(def, p)}
o.Options = append(o.Options, opt)
}
// IntVar defines a signed integer option with a pointer to its value
func IntVar(p *int, long string, short rune, def int) {
CommandLine.IntVar(p, long, short, def)
}
// Int defines a signed integer option and returns a pointer to its value
func (o *OptionParser) Int(long string, short rune, def int) (p *int) {
p = new(int)
o.IntVar(p, long, short, def)
return
}
// Int defines a signed integer option and returns a pointer to its value
func Int(long string, short rune, def int) (p *int) {
p = CommandLine.Int(long, short, def)
return
}