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