Add naive cli command router
This commit is contained in:
parent
e4035008b8
commit
1f494c5a63
14
spec/command_router_spec.cr
Normal file
14
spec/command_router_spec.cr
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
require "./spec_helper"
|
||||||
|
require "../src/cli/*"
|
||||||
|
|
||||||
|
describe CLI::CommandRouter do
|
||||||
|
it "should handle simple command" do
|
||||||
|
router = CLI::CommandRouter.new
|
||||||
|
x = 10
|
||||||
|
router.add "plus" do
|
||||||
|
x += 5
|
||||||
|
end
|
||||||
|
router.handle "plus"
|
||||||
|
x.should eq 15
|
||||||
|
end
|
||||||
|
end
|
1
spec/resources_spec.cr
Normal file
1
spec/resources_spec.cr
Normal file
@ -0,0 +1 @@
|
|||||||
|
require "./spec_helper"
|
17
src/cli/command_router.cr
Normal file
17
src/cli/command_router.cr
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
class CLI::CommandRouter
|
||||||
|
def initialize
|
||||||
|
@mappings = [] of {String, Proc(Nil)}
|
||||||
|
end
|
||||||
|
|
||||||
|
def add(route, &block)
|
||||||
|
@mappings.push({route, block})
|
||||||
|
end
|
||||||
|
|
||||||
|
def handle(command)
|
||||||
|
@mappings.each do |i|
|
||||||
|
if i[0] == command
|
||||||
|
i[1].call
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in New Issue
Block a user