-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintvalue.go
More file actions
43 lines (37 loc) · 894 Bytes
/
intvalue.go
File metadata and controls
43 lines (37 loc) · 894 Bytes
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
package opts
import (
"strconv"
)
// Int defines an int option with the specified name and value. The argument
// i points to an int variable that will store the value of the option. Int
// will panic if name is not valid or repeats an existing option.
func (g *Group) Int(i *int, name string, defValue int) {
if err := validateName("Int", name); err != nil {
panic(err)
}
*i = defValue
opt := &opt{
value: &value[int]{
ptr: i,
convert: toInt,
},
name: name,
isBool: false,
}
if err := g.optAlreadySet(name); err != nil {
panic(err)
}
g.opts[name] = opt
}
// IntZero is like Int but with a default value of 0.
func (g *Group) IntZero(i *int, name string) {
g.Int(i, name, 0)
}
func toInt(s string) (int, error) {
v, err := strconv.Atoi(s)
if err != nil {
// The caller will give this error more context.
return 0, numError(err)
}
return v, nil
}