forked from urfave/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flag_int.go
62 lines (50 loc) · 1.09 KB
/
flag_int.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
package cli
import (
"fmt"
"strconv"
)
type IntFlag = FlagBase[int, IntegerConfig, intValue]
// IntegerConfig is the configuration for all integer type flags
type IntegerConfig struct {
Base int
}
// -- int Value
type intValue struct {
val *int
base int
}
// Below functions are to satisfy the ValueCreator interface
func (i intValue) Create(val int, p *int, c IntegerConfig) Value {
*p = val
return &intValue{
val: p,
base: c.Base,
}
}
func (i intValue) ToString(b int) string {
return fmt.Sprintf("%v", b)
}
// Below functions are to satisfy the flag.Value interface
func (i *intValue) Set(s string) error {
v, err := strconv.ParseInt(s, i.base, strconv.IntSize)
if err != nil {
return err
}
*i.val = int(v)
return err
}
func (i *intValue) Get() any { return int(*i.val) }
func (i *intValue) String() string {
if i == nil || i.val == nil {
return ""
}
return strconv.Itoa(int(*i.val))
}
// Int looks up the value of a local IntFlag, returns
// 0 if not found
func (cCtx *Context) Int(name string) int {
if v, ok := cCtx.Value(name).(int); ok {
return v
}
return 0
}