f -> run, Function type.
Juan Batiz-Benet committed
Oct 20, 2014 at 07:35 UTC
09d2277f0a2345d04091de363cc1c158a2628a8f
2 files changed
+18
-12
commands/command.go
+12
-6
@@ -6,11 +6,17 @@ import (
6
"strings"
7
)
8
9
-// Command is an object that defines a command.
9
+// Function is the type of function that Commands use.
10
+// It reads from the Request, and writes results to the Response.
11
+type Function func(*Request, *Response)
12
+
13
+// Command is a runnable command, with input arguments and options (flags).
14
+// It can also have subcommands, to group units of work into sets.
15
type Command struct {
11
- Help string
12
- Options []Option
13
- f func(*Request, *Response)
16
+ Help string
17
+ Options []Option
18
+
19
+ run Function
20
subcommands map[string]*Command
21
}
22
@@ -51,7 +57,7 @@ func (c *Command) Call(req *Request) *Response {
57
}
58
cmd := cmds[len(cmds)-1]
59
54
- if cmd.f == nil {
60
+ if cmd.run == nil {
61
res.SetError(ErrNotCallable, ErrClient)
62
return res
63
}
@@ -68,7 +74,7 @@ func (c *Command) Call(req *Request) *Response {
74
return res
75
}
76
71
- cmd.f(req, res)
77
+ cmd.run(req, res)
78
79
return res
80
}
commands/command_test.go
+6
-6
@@ -8,7 +8,7 @@ func TestOptionValidation(t *testing.T) {
8
Option{[]string{"b", "beep"}, Int},
9
Option{[]string{"B", "boop"}, String},
10
},
11
- f: func(req *Request, res *Response) {},
11
+ run: func(req *Request, res *Response) {},
12
}
13
14
req := NewEmptyRequest()
@@ -84,35 +84,35 @@ func TestRegistration(t *testing.T) {
84
Options: []Option{
85
Option{[]string{"beep"}, Int},
86
},
87
- f: func(req *Request, res *Response) {},
87
+ run: func(req *Request, res *Response) {},
88
},
89
90
&Command{
91
Options: []Option{
92
Option{[]string{"boop"}, Int},
93
},
94
- f: func(req *Request, res *Response) {},
94
+ run: func(req *Request, res *Response) {},
95
},
96
97
&Command{
98
Options: []Option{
99
Option{[]string{"boop"}, String},
100
},
101
- f: func(req *Request, res *Response) {},
101
+ run: func(req *Request, res *Response) {},
102
},
103
104
&Command{
105
Options: []Option{
106
Option{[]string{"bop"}, String},
107
},
108
- f: func(req *Request, res *Response) {},
108
+ run: func(req *Request, res *Response) {},
109
},
110
111
&Command{
112
Options: []Option{
113
Option{[]string{"enc"}, String},
114
},
115
- f: func(req *Request, res *Response) {},
115
+ run: func(req *Request, res *Response) {},
116
},
117
}
118