From 1f494c5a6335e895e9baf20274ca898ab8f1f665 Mon Sep 17 00:00:00 2001 From: Anton Vakhrushev Date: Thu, 3 Oct 2019 17:48:59 +0300 Subject: [PATCH] Add naive cli command router --- spec/command_router_spec.cr | 14 ++++++++++++++ spec/resources_spec.cr | 1 + src/cli/command_router.cr | 17 +++++++++++++++++ 3 files changed, 32 insertions(+) create mode 100644 spec/command_router_spec.cr create mode 100644 spec/resources_spec.cr create mode 100644 src/cli/command_router.cr diff --git a/spec/command_router_spec.cr b/spec/command_router_spec.cr new file mode 100644 index 0000000..dfd2154 --- /dev/null +++ b/spec/command_router_spec.cr @@ -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 diff --git a/spec/resources_spec.cr b/spec/resources_spec.cr new file mode 100644 index 0000000..0ccaf75 --- /dev/null +++ b/spec/resources_spec.cr @@ -0,0 +1 @@ +require "./spec_helper" diff --git a/src/cli/command_router.cr b/src/cli/command_router.cr new file mode 100644 index 0000000..dc84da8 --- /dev/null +++ b/src/cli/command_router.cr @@ -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