forked from alecthomas/kingpin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flags.go
197 lines (170 loc) · 4.04 KB
/
flags.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package kingpin
import (
"fmt"
"strings"
)
type flagGroup struct {
short map[string]*FlagClause
long map[string]*FlagClause
}
func newFlagGroup() *flagGroup {
return &flagGroup{
short: make(map[string]*FlagClause),
long: make(map[string]*FlagClause),
}
}
// Flag defines a new flag with the given long name and help.
func (f *flagGroup) Flag(name, help string) *FlagClause {
flag := newFlag(name, help)
f.long[name] = flag
return flag
}
func (f *flagGroup) init() {
for _, flag := range f.long {
flag.init()
if flag.Shorthand != 0 {
f.short[string(flag.Shorthand)] = flag
}
}
}
func (f *flagGroup) parse(tokens tokens) (tokens, error) {
remaining := make(map[string]struct{})
for k, flag := range f.long {
if flag.required {
remaining[k] = struct{}{}
}
}
var token *token
loop:
for {
token, tokens = tokens.Next()
switch token.Type {
case TokenEOF:
break loop
case TokenLong, TokenShort:
flagToken := token
defaultValue := ""
var flag *FlagClause
var ok bool
if token.Type == TokenLong {
flag, ok = f.long[token.Value]
if !ok {
flag, ok = f.long["no-"+token.Value]
if !ok {
return nil, fmt.Errorf("unknown long flag '%s'", flagToken)
}
defaultValue = "false"
}
} else {
flag, ok = f.short[token.Value]
if !ok {
return nil, fmt.Errorf("unknown short flag '%s", flagToken)
}
}
delete(remaining, flag.name)
if !flag.boolean {
token, tokens = tokens.Next()
if token.Type != TokenArg {
return nil, fmt.Errorf("expected argument for flag '%s'", flagToken)
}
defaultValue = token.Value
}
if err := flag.value.Set(defaultValue); err != nil {
return nil, err
}
if flag.dispatch != nil {
if err := flag.dispatch(); err != nil {
return nil, err
}
}
default:
tokens = tokens.Return(token)
break loop
}
}
// Check that required flags were provided.
if len(remaining) == 1 {
for k := range remaining {
return nil, fmt.Errorf("required flag --%s not provided", k)
}
} else if len(remaining) > 1 {
flags := make([]string, 0, len(remaining))
for k := range remaining {
flags = append(flags, "--"+k)
}
return nil, fmt.Errorf("required flags %s not provided", strings.Join(flags, ", "))
}
return tokens, nil
}
// FlagClause is a fluid interface used to build flags.
type FlagClause struct {
parserMixin
name string
Shorthand byte
help string
DefValue string
metavar string
boolean bool
dispatch Dispatch
}
func newFlag(name, help string) *FlagClause {
f := &FlagClause{
name: name,
help: help,
}
return f
}
func (f *FlagClause) formatMetaVar() string {
if f.metavar != "" {
return f.metavar
}
return strings.ToUpper(f.name)
}
func (f *FlagClause) init() {
if f.required && f.DefValue != "" {
panic(fmt.Sprintf("required flag '%s' with unusable default value", f.name))
}
if f.value == nil {
panic(fmt.Sprintf("no value defined for --%s", f.name))
}
if f.DefValue != "" {
if err := f.value.Set(f.DefValue); err != nil {
panic(fmt.Sprintf("default value for --%s is invalid: %s", f.name, err))
}
}
}
// Dispatch to the given function when the flag is parsed.
func (f *FlagClause) Dispatch(dispatch Dispatch) *FlagClause {
f.dispatch = dispatch
return f
}
// Default value for this flag.
func (f *FlagClause) Default(value string) *FlagClause {
f.DefValue = value
return f
}
// MetaVar sets the placeholder string used for flag values in the help.
func (f *FlagClause) MetaVar(metavar string) *FlagClause {
f.metavar = metavar
return f
}
// Required makes the flag required.
func (f *FlagClause) Required() *FlagClause {
f.required = true
return f
}
// Short sets the short flag name.
func (f *FlagClause) Short(name byte) *FlagClause {
f.Shorthand = name
return f
}
func (f *FlagClause) Bool() (target *bool) {
target = new(bool)
f.SetValue(newBoolValue(false, target))
return
}
// SetIsBoolean tells the parser that this is a boolean flag. Typically only
// used by Parser implementations.
func (f *FlagClause) SetIsBoolean() {
f.boolean = true
}