-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand.go
More file actions
61 lines (53 loc) · 1.42 KB
/
command.go
File metadata and controls
61 lines (53 loc) · 1.42 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
package cli
// Command represents an application command.
type Command struct {
name string
alias string
proxy bool
flags []*Flag
handler Handler
middleware []func(Handler) Handler
}
// Handler represents a command handler.
type Handler func(args []string) error
// NewCommand returns a new command.
func NewCommand(name string, handler Handler, flags []*Flag, opts ...CommandOption) *Command {
c := &Command{
name: name,
flags: flags,
middleware: make([]func(Handler) Handler, 0),
}
for _, option := range opts {
option(c)
}
c.build(handler)
return c
}
// build wraps h with the configured middleware.
func (c *Command) build(h Handler) {
c.handler = h
for i := len(c.middleware) - 1; i >= 0; i-- {
c.handler = c.middleware[i](c.handler)
}
}
// CommandOption represents a functional option for command configuration.
type CommandOption func(*Command)
// Alias sets the command alias.
func Alias(name string) CommandOption {
return func(c *Command) {
c.alias = name
}
}
// Proxy instructs the dispatcher to proxy the unparsed
// arguments to the command itself for further processing.
func Proxy() CommandOption {
return func(c *Command) {
c.proxy = true
}
}
// WithMiddleware appends middleware to the middleware stack.
func WithMiddleware(middleware ...func(Handler) Handler) CommandOption {
return func(c *Command) {
c.middleware = append(c.middleware, middleware...)
}
}