-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathint.go
More file actions
73 lines (60 loc) · 1.27 KB
/
int.go
File metadata and controls
73 lines (60 loc) · 1.27 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
64
65
66
67
68
69
70
71
72
73
package rig
import (
"flag"
"strconv"
"github.com/Pimmr/rig/validators"
)
type intValidators struct {
*intValue
validators []validators.Int
}
func (v intValidators) Set(s string) error {
err := v.intValue.Set(s)
if err != nil {
return err
}
for _, validator := range v.validators {
err = validator(int(*v.intValue))
if err != nil {
return err
}
}
return nil
}
func (v intValidators) New(i interface{}) flag.Value {
return intValidators{
intValue: (*intValue)(i.(*int)),
validators: v.validators,
}
}
func (v intValidators) IsNil() bool {
return v.intValue == nil
}
type intValue int
func (i intValue) String() string {
return strconv.Itoa(int(i))
}
func (i *intValue) Set(s string) error {
v, err := strconv.ParseInt(s, 0, strconv.IntSize)
*i = intValue(v)
return err
}
// Int creates a flag for a int variable.
func Int(v *int, flag, env, usage string, validators ...validators.Int) *Flag {
return &Flag{
Value: intValidators{
intValue: (*intValue)(v),
validators: validators,
},
Name: flag,
Env: env,
Usage: usage,
TypeHint: "int",
}
}
// IntGenerator is the default int generator, to be used with Repeatable for int slices.
func IntGenerator() Generator {
return func() flag.Value {
return new(intValue)
}
}