Improve command handling
This commit is contained in:
parent
927ca47b04
commit
3669e31c66
@ -12,7 +12,7 @@
|
||||
- [x] Цикл ввода-вывода
|
||||
- [ ] Просмотр задач в очереди
|
||||
- [ ] Время завершения задачи при создании
|
||||
- [ ] Сообщение о выйгрыше
|
||||
- [x] Сообщение о выйгрыше
|
||||
|
||||
## Механики
|
||||
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user