forked from cornfeedhobo/pflag
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathint64.go
82 lines (67 loc) · 2.38 KB
/
int64.go
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
74
75
76
77
78
79
80
81
82
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package zflag
import (
"strconv"
"strings"
)
// -- int64 Value
type int64Value int64
var _ Value = (*int64Value)(nil)
var _ Getter = (*int64Value)(nil)
var _ Typed = (*int64Value)(nil)
func newInt64Value(val int64, p *int64) *int64Value {
*p = val
return (*int64Value)(p)
}
func (i *int64Value) Set(val string) error {
val = strings.TrimSpace(val)
v, err := strconv.ParseInt(val, 0, 64)
*i = int64Value(v)
return err
}
func (i *int64Value) Get() interface{} {
return int64(*i)
}
func (i *int64Value) Type() string {
return "int64"
}
func (i *int64Value) String() string { return strconv.FormatInt(int64(*i), 10) }
// GetInt64 return the int64 value of a flag with the given name
func (fs *FlagSet) GetInt64(name string) (int64, error) {
val, err := fs.getFlagValue(name, "int64")
if err != nil {
return 0, err
}
return val.(int64), nil
}
// MustGetInt64 is like GetInt64, but panics on error.
func (fs *FlagSet) MustGetInt64(name string) int64 {
val, err := fs.GetInt64(name)
if err != nil {
panic(err)
}
return val
}
// Int64Var defines an int64 flag with specified name, default value, and usage string.
// The argument p points to an int64 variable in which to store the value of the flag.
func (fs *FlagSet) Int64Var(p *int64, name string, value int64, usage string, opts ...Opt) {
fs.Var(newInt64Value(value, p), name, usage, opts...)
}
// Int64Var defines an int64 flag with specified name, default value, and usage string.
// The argument p points to an int64 variable in which to store the value of the flag.
func Int64Var(p *int64, name string, value int64, usage string, opts ...Opt) {
CommandLine.Int64Var(p, name, value, usage, opts...)
}
// Int64 defines an int64 flag with specified name, default value, and usage string.
// The return value is the address of an int64 variable that stores the value of the flag.
func (fs *FlagSet) Int64(name string, value int64, usage string, opts ...Opt) *int64 {
var p int64
fs.Int64Var(&p, name, value, usage, opts...)
return &p
}
// Int64 defines an int64 flag with specified name, default value, and usage string.
// The return value is the address of an int64 variable that stores the value of the flag.
func Int64(name string, value int64, usage string, opts ...Opt) *int64 {
return CommandLine.Int64(name, value, usage, opts...)
}