-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathint.go
More file actions
63 lines (51 loc) · 1.14 KB
/
Copy pathint.go
File metadata and controls
63 lines (51 loc) · 1.14 KB
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
package flags
import (
"github.com/cstockton/go-conv"
)
// IntValue flag type.
type IntValue struct {
Value
V *int
}
// Set converts and sets a value
func (val *IntValue) Set(v interface{}) error {
converted, err := conv.Int(v)
if err != nil {
return err
}
*val.V = converted
return nil
}
// Int creates new Int flag.
// Accepts a list of additional resolvers that are evaluated in sequence and
// the first one to yield a valid value is chosen.
// If no resolver yileds a valid value the default flag value is used.
// If flag is provided as a cli arg it will take precedence over all resolvers and the default value.
func (fs *FlagSet) Int(name, usage string, val int, r ...ResolverFunc) *int {
fs.initFlagSet()
v := IntValue{
Value: Value{
name: name,
resolvers: r,
},
V: fs.fs.Int(name, val, usage),
}
fs.Values = append(fs.Values, v)
return v.V
}
func (fs *FlagSet) parseIntVals() {
for i, val := range fs.Values {
intVal, ok := val.(IntValue)
if !ok {
continue
}
if fs.hasArg(intVal.name) {
continue
}
for _, r := range intVal.resolvers {
if r(fs, intVal.name, 0, i) {
break
}
}
}
}