diff --git a/ROADMAP.md b/ROADMAP.md index 277bd3a..2880893 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -12,7 +12,7 @@ - [x] Цикл ввода-вывода - [ ] Просмотр задач в очереди - [ ] Время завершения задачи при создании -- [ ] Сообщение о выйгрыше +- [x] Сообщение о выйгрыше ## Механики diff --git a/spec/command_router_spec.cr b/spec/command_router_spec.cr index 681f001..099fb70 100644 --- a/spec/command_router_spec.cr +++ b/spec/command_router_spec.cr @@ -40,4 +40,17 @@ describe CLI::CommandRouter do router.handle "plus 1 3 6" x.should eq 10 end + + it "should match commands from begin of string" do + router = CLI::CommandRouter.new + x = 5 + router.add "plus" do |p| + x += 10 + end + router.add "mult and plus" do |p| + x = x * 2 + 10 + end + router.handle "mult and plus" + x.should eq 20 + end end diff --git a/src/cli/command_router.cr b/src/cli/command_router.cr index 6f3dd24..f5adcd5 100644 --- a/src/cli/command_router.cr +++ b/src/cli/command_router.cr @@ -25,6 +25,10 @@ class CLI::CommandRouter end private def route_to_regex(route) : Regex + Regex.new('^' + split_route route) + end + + private def split_route(route) : String vals = route.partition(/\{[a-z]+?\}/) param = vals[1].lstrip('{').rstrip('}') result = "" @@ -35,8 +39,8 @@ class CLI::CommandRouter result += sprintf "(?P<%s>.+?)", param end if vals[2] != "" - result += route_to_regex(vals[2]).source + result += split_route(vals[2]) end - Regex.new(result) + result end end