-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflags.go
More file actions
84 lines (65 loc) · 1.68 KB
/
flags.go
File metadata and controls
84 lines (65 loc) · 1.68 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
74
75
76
77
78
79
80
81
82
83
84
package main
import (
"fmt"
"strconv"
)
// Our version of flags more or less mimics flag.Values but allows us to set them up with
// defaults from the user config file prior to adding them into the flagSet. There is also
// additional validating in some cases.
type flagValue interface {
String() string
Set(string) error
}
// Bool
type boolFlag struct {
v bool
}
func (bo *boolFlag) Set(s string) error {
v, err := strconv.ParseBool(s)
if err != nil {
return strconvTrimError(err)
}
bo.v = v
return nil
}
func (bo *boolFlag) String() string { return strconv.FormatBool(bo.v) }
// The existence of IsBoolFlag tells the "flag" package that no value is expected.
func (bo *boolFlag) IsBoolFlag() bool { return true }
// Uint
type uintFlag struct {
v uint
min uint // min > 0 && v < min -> error
}
func (io *uintFlag) Set(s string) error {
v, err := strconv.ParseUint(s, 0, strconv.IntSize)
if err != nil {
return strconvTrimError(err)
}
if io.min > 0 && uint(v) < io.min {
return fmt.Errorf("Less than minimum of '%d'", io.min)
}
io.v = uint(v)
return nil
}
// String
func (io *uintFlag) String() string { return strconv.Itoa(int(io.v)) }
type commaStringFlag struct {
v string
}
// Set is an append if the string starts with a "+" or a replace otherwise.
func (csf *commaStringFlag) Set(s string) error {
if len(s) == 0 || s[0] != plusAppend { // Empty or not "+", replace
csf.v = s
return nil
}
// Must be a +string
if len(csf.v) == 0 { // Append to an empty string removes "+"
csf.v = s[1:]
} else {
csf.v += "," + s[1:] // Otherwise replace "+" with ","
}
return nil
}
func (csf *commaStringFlag) String() string { return csf.v }
// Age
type ageFlag = age