-
Notifications
You must be signed in to change notification settings - Fork 2
/
command.go
354 lines (324 loc) · 9.42 KB
/
command.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
package xflags
import (
"errors"
"flag"
"fmt"
"io"
"os"
)
// TODO: Allow packages to declare global flags that are accessible on init.
// Commander is an interface that describes any type that produces a Command.
//
// The interface is implemented by both CommandBuilder and Command so they can
// often be used interchangeably.
type Commander interface {
Command() (*Command, error)
}
// A HandlerFunc is a function that handles the invokation a command specified
// by command line arguments.
//
// Args will receive any arguments ignored by the parser after the "--"
// terminator if it is enabled.
type HandlerFunc func(args []string) int
// Command describes a command that users may invoke from the command line.
//
// Programs should not create Command directly and instead use the Command
// function to build one with proper error checking.
type Command struct {
Parent *Command
Name string
Usage string
Synopsis string
Hidden bool
WithTerminator bool
FlagGroups []*FlagGroup
Subcommands []*Command
FormatFunc FormatFunc
HandlerFunc HandlerFunc
Stdout io.Writer
Stderr io.Writer
args []string
}
// Command implements the Commander interface.
func (c *Command) Command() (*Command, error) {
flagsByName := make(map[string]*Flag)
hasUnboundedPositional := false
for _, group := range c.FlagGroups {
for _, flag := range group.Flags {
if flag.Positional {
if len(c.Subcommands) > 0 {
return nil, errorf(
"%s: cannot specify both subcommands and"+
" positional arguments",
c.Name,
)
}
if hasUnboundedPositional {
return nil, errorf(
"%s: positional arguments cannot follow unbounded"+
" positional arguments",
c.Name,
)
}
if flag.MaxCount == 0 {
hasUnboundedPositional = true
}
}
if flag.Name != "" {
key := "--" + flag.Name
if _, ok := flagsByName[key]; ok {
return nil, errorf("%s: flag already declared: %s", c.Name, key)
}
flagsByName[key] = flag
}
if flag.ShortName != "" {
key := "-" + flag.ShortName
if _, ok := flagsByName[key]; ok {
return nil, errorf("%s: flag already declared: %s", c.Name, key)
}
flagsByName[key] = flag
}
}
}
return c, nil
}
func (c *Command) String() string { return c.Name }
// Args returns any command line arguments specified after the "--" terminator
// if it was enabled. Args is only populated after the command line is
// successfully parsed.
func (c *Command) Args() []string { return c.args }
// Arg returns the i'th argument specified after the "--" terminator if it was enabled. Arg(0) is
// the first remaining argument after flags the terminator. Arg returns an empty string if the
// requested element does not exist.
func (c *Command) Arg(i int) string {
if i < 0 || i >= len(c.args) {
return ""
}
return c.args[i]
}
// Parse parses the given set of command line arguments and stores the value of
// each argument in each command flag's target. The rules for each flag are
// checked and any errors are returned.
//
// If -h or --help are specified, a HelpError will be returned containing the
// subcommand that was specified.
//
// The returned *Command will be this command or one of its subcommands if
// specified by the command line arguments.
func (c *Command) Parse(args []string) (*Command, error) {
cmd, args, err := newArgParser(c, args).Parse()
if err != nil {
return nil, err
}
cmd.args = args
return cmd, nil
}
// output returns stdout and stderr, inheriting from parents and defaulting to
// OS defaults.
func (c *Command) output() (stdout, stderr io.Writer) {
stdout, stderr = c.Stdout, c.Stderr
if stdout == nil && stderr == nil {
if c.Parent != nil {
return c.Parent.output()
}
return os.Stdout, os.Stderr
}
return
}
// Run parses the given set of command line arguments and calls the handler
// for the command or subcommand specified by the arguments.
//
// If -h or --help are specified, usage information will be printed to os.Stdout
// and the return code will be 0.
//
// If a command is invoked that has no handler, usage information will be
// printed to os.Stderr and the return code will be non-zero.
func (c *Command) Run(args []string) int {
target, err := c.Parse(args)
if err != nil {
return c.handleErr(err)
}
if target.HandlerFunc == nil {
_, stderr := target.output()
if err := target.WriteUsage(stderr); err != nil {
panic(err)
}
return 1
}
return target.HandlerFunc(target.args)
}
func (c *Command) handleErr(err error) int {
if err == nil {
return 0
}
var helpErr *HelpError
if errors.As(err, &helpErr) {
stdout, _ := helpErr.Cmd.output()
if stdout != os.Stdout {
if f, ok := stdout.(*os.File); ok {
panic(f.Name())
}
panic(stdout)
}
if err := helpErr.Cmd.WriteUsage(stdout); err != nil {
panic(err)
}
return 0
}
var argErr *ArgumentError
if errors.As(err, &argErr) {
_, stderr := argErr.Cmd.output()
fmt.Fprintf(stderr, "Argument error: %s\n", argErr.String())
return 1
}
_, stderr := c.output()
fmt.Fprintf(stderr, "Error: %v\n", errStr(err))
return 1
}
// WriteUsage prints a help message to the given Writer using the configured
// Formatter.
func (c *Command) WriteUsage(w io.Writer) error {
f := c.FormatFunc
for p := c; f == nil && p != nil; p = p.Parent {
f = p.FormatFunc
}
if f == nil {
f = Format
}
return f(w, c)
}
// CommandBuilder builds a Command which defines a command and all of its flags.
// Create a command builder with NewCommand.
// All chain methods return a pointer to the same builder.
type CommandBuilder struct {
cmd Command
flagGroups []*flagGroupBuilder
subcommands []Commander
err error
}
// NewCommand returns a CommandBuilder which can be used to define a command and
// all of its flags.
func NewCommand(name, usage string) *CommandBuilder {
c := &CommandBuilder{
cmd: Command{
Name: name,
Usage: usage,
},
flagGroups: make([]*flagGroupBuilder, 1, 8),
subcommands: make([]Commander, 0, 8),
}
c.flagGroups[0] = newFlagGroupBuilder("options", "Options")
return c
}
func (c *CommandBuilder) error(err error) *CommandBuilder {
if c.err != nil {
return c
}
c.err = err
return c
}
// Synopsis specifies the detailed help message for this command.
func (c *CommandBuilder) Synopsis(s string) *CommandBuilder {
c.cmd.Synopsis = s
return c
}
// HandleFunc registers the handler for the command. If no handler is specified
// and the command is invoked, it will print usage information to stderr.
func (c *CommandBuilder) HandleFunc(
handler func(args []string) int,
) *CommandBuilder {
if handler == nil {
return c.error(errorf("%s: nil handler", c.cmd.Name))
}
c.cmd.HandlerFunc = handler
return c
}
// Hidden hides the command from all help messages but still allows the command
// to be invoked on the command line.
func (c *CommandBuilder) Hidden() *CommandBuilder {
c.cmd.Hidden = true
return c
}
// Flag adds command line flags to the default FlagGroup for this command.
func (c *CommandBuilder) Flags(flags ...Flagger) *CommandBuilder {
c.flagGroups[0].append(flags...)
return c
}
// FlagGroup adds a group of command line flags to this command and shows them
// under a common heading in help messages.
func (c *CommandBuilder) FlagGroup(
name, usage string,
flags ...Flagger,
) *CommandBuilder {
c.flagGroups = append(c.flagGroups, newFlagGroupBuilder(name, usage, flags...))
return c
}
// FlagSet imports flags from a Flagset created using Go's flag package. All
// parsing and error handling is still managed by this package.
//
// To import any globally defined flags, import flag.CommandLine.
func (c *CommandBuilder) FlagSet(flagSet *flag.FlagSet) *CommandBuilder {
flagSet.VisitAll(func(f *flag.Flag) {
flag, err := Var(f.Value, f.Name, f.Usage).Flag()
if err != nil {
c.err = err
return
}
c = c.Flags(flag)
})
return c
}
// Subcommands adds subcommands to this command.
func (c *CommandBuilder) Subcommands(commands ...Commander) *CommandBuilder {
c.subcommands = append(c.subcommands, commands...)
return c
}
// Formatter specifies a custom Formatter for formatting help messages for this
// command.
func (c *CommandBuilder) FormatFunc(fn FormatFunc) *CommandBuilder {
c.cmd.FormatFunc = fn
return c
}
// WithTerminator specifies that any command line argument after "--" will be
// passed through to the args parameter of the command's handler without any
// further processing.
func (c *CommandBuilder) WithTerminator() *CommandBuilder {
c.cmd.WithTerminator = true
return c
}
// Output sets the destination for usage and error messages.
func (c *CommandBuilder) Output(stdout, stderr io.Writer) *CommandBuilder {
c.cmd.Stdout, c.cmd.Stderr = stdout, stderr
return c
}
// Command implements the Commander interface and produces a new Command.
func (c *CommandBuilder) Command() (*Command, error) {
if c.err != nil {
return nil, c.err
}
cmd := c.cmd
for _, groupBuilder := range c.flagGroups {
group, err := groupBuilder.FlagGroup()
if err != nil {
return nil, err
}
cmd.FlagGroups = append(cmd.FlagGroups, group)
}
for _, commandBuilder := range c.subcommands {
sub, err := commandBuilder.Command()
if err != nil {
return nil, err
}
cmd.Subcommands = append(cmd.Subcommands, sub)
sub.Parent = &cmd
}
return cmd.Command()
}
// Must is a helper that calls Command and panics if the error is non-nil.
func (c *CommandBuilder) Must() *Command {
cmd, err := c.Command()
if err != nil {
panic(err)
}
return cmd
}