Add naive cli command router

This commit is contained in:
Anton Vakhrushev 2019-10-03 17:48:59 +03:00
parent e4035008b8
commit 1f494c5a63
3 changed files with 32 additions and 0 deletions

View 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
View File

@ -0,0 +1 @@
require "./spec_helper"

17
src/cli/command_router.cr Normal file
View 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