-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathint32.go
More file actions
73 lines (60 loc) · 1.31 KB
/
int32.go
File metadata and controls
73 lines (60 loc) · 1.31 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 int32Validators struct {
*int32Value
validators []validators.Int32
}
func (v int32Validators) Set(s string) error {
err := v.int32Value.Set(s)
if err != nil {
return err
}
for _, validator := range v.validators {
err = validator(int32(*v.int32Value))
if err != nil {
return err
}
}
return nil
}
func (v int32Validators) New(i interface{}) flag.Value {
return int32Validators{
int32Value: (*int32Value)(i.(*int32)),
validators: v.validators,
}
}
func (v int32Validators) IsNil() bool {
return v.int32Value == nil
}
type int32Value int32
func (i int32Value) String() string {
return strconv.Itoa(int(i))
}
func (i *int32Value) Set(s string) error {
v, err := strconv.ParseInt(s, 0, 32)
*i = int32Value(v)
return err
}
// Int32 creates a flag for a int32 variable.
func Int32(v *int32, flag, env, usage string, validators ...validators.Int32) *Flag {
return &Flag{
Value: int32Validators{
int32Value: (*int32Value)(v),
validators: validators,
},
Name: flag,
Env: env,
Usage: usage,
TypeHint: "int32",
}
}
// Int32Generator is the default int32 generator, to be used with Repeatable for int32 slices.
func Int32Generator() Generator {
return func() flag.Value {
return new(int32Value)
}
}