提交 09d2277f 作者: Juan Batiz-Benet

f -> run, Function type.

上级 7673ce6f
......@@ -6,11 +6,17 @@ import (
"strings"
)
// Command is an object that defines a command.
// Function is the type of function that Commands use.
// It reads from the Request, and writes results to the Response.
type Function func(*Request, *Response)
// Command is a runnable command, with input arguments and options (flags).
// It can also have subcommands, to group units of work into sets.
type Command struct {
Help string
Options []Option
f func(*Request, *Response)
Help string
Options []Option
run Function
subcommands map[string]*Command
}
......@@ -51,7 +57,7 @@ func (c *Command) Call(req *Request) *Response {
}
cmd := cmds[len(cmds)-1]
if cmd.f == nil {
if cmd.run == nil {
res.SetError(ErrNotCallable, ErrClient)
return res
}
......@@ -68,7 +74,7 @@ func (c *Command) Call(req *Request) *Response {
return res
}
cmd.f(req, res)
cmd.run(req, res)
return res
}
......
......@@ -8,7 +8,7 @@ func TestOptionValidation(t *testing.T) {
Option{[]string{"b", "beep"}, Int},
Option{[]string{"B", "boop"}, String},
},
f: func(req *Request, res *Response) {},
run: func(req *Request, res *Response) {},
}
req := NewEmptyRequest()
......@@ -84,35 +84,35 @@ func TestRegistration(t *testing.T) {
Options: []Option{
Option{[]string{"beep"}, Int},
},
f: func(req *Request, res *Response) {},
run: func(req *Request, res *Response) {},
},
&Command{
Options: []Option{
Option{[]string{"boop"}, Int},
},
f: func(req *Request, res *Response) {},
run: func(req *Request, res *Response) {},
},
&Command{
Options: []Option{
Option{[]string{"boop"}, String},
},
f: func(req *Request, res *Response) {},
run: func(req *Request, res *Response) {},
},
&Command{
Options: []Option{
Option{[]string{"bop"}, String},
},
f: func(req *Request, res *Response) {},
run: func(req *Request, res *Response) {},
},
&Command{
Options: []Option{
Option{[]string{"enc"}, String},
},
f: func(req *Request, res *Response) {},
run: func(req *Request, res *Response) {},
},
}
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论