-
Notifications
You must be signed in to change notification settings - Fork 10
/
flag.go
105 lines (86 loc) · 2.04 KB
/
flag.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package configure
import (
"errors"
"fmt"
"os"
"strconv"
"strings"
)
// NewFlag returns a new instance of the Flag Checker, using os.Args as its
// flag source.
func NewFlag() *Flag {
f := &Flag{
args: os.Args,
defaults: defaultsPrinter,
}
return f
}
// NewFlagWithUsage returns a new instance of the Flag Checker with a custom
// usage printer. It uses os.Args as its flag source.
func NewFlagWithUsage(defaulter func() string) *Flag {
f := &Flag{
args: os.Args,
defaults: defaulter,
}
return f
}
// Flag is an ULTRA simple flag parser for strings, ints and bools.
// It expects flags in the format --x=2 (where x is the var name
// and 2 is the value). It can be provided with a "usage printer",
// which by default will be printed when the "-h" flag is used.
type Flag struct {
args []string
defaults func() string
}
func (f Flag) Setup() error {
for _, arg := range f.args {
if arg == "-h" {
fmt.Fprintf(os.Stderr, f.defaults())
os.Exit(0)
}
}
return nil
}
func (f Flag) value(name string) (string, error) {
for _, arg := range f.args {
// --x=2 -> x=2
ass := strings.TrimPrefix(arg, "--")
if ass == arg {
continue
}
// x=2 -> 2
val := strings.TrimPrefix(ass, fmt.Sprintf("%v=", name))
if val == ass {
continue
}
return val, nil
}
return "", errors.New("could not find that value")
}
// Int returns an int if it exists in the set flags.
func (f Flag) Int(name string) (int, error) {
v, err := f.value(name)
if err != nil {
return 0, err
}
i, err := strconv.Atoi(v)
if err != nil {
return 0, err
}
return i, nil
}
// Bool returns a bool if it exists in the set flags.
func (f *Flag) Bool(name string) (bool, error) {
v, err := f.value(name)
if err != nil {
return false, err
}
return strconv.ParseBool(v)
}
// String returns a string if it exists in the set flags.
func (f Flag) String(name string) (string, error) {
return f.value(name)
}
func defaultsPrinter() string {
return "This is the default \"defaults\" message from configure. You should probably change it\n"
}