NAT archiveController handle running commands by key

This commit is contained in:
2021-08-29 13:51:36 -06:00
parent a90455cea9
commit a4f98c7ac0
2 changed files with 14 additions and 5 deletions

View File

@@ -95,7 +95,13 @@
(lambda :Void []
(_collectAndValidateArg arg ->:Void [:Dynamic argValue] {(collectedArgs.push argValue) (lastCollector)})))
(method :Void runCommand [:Command command]
(method :Void tryRunCommand [:String commandName]
(let [lowerCommandName (commandName.toLowerCase)]
(if (commands.exists lowerCommandName)
(_runCommand (dictGet commands lowerCommandName))
(ui.displayMessage "$commandName is not a valid command"))))
(method :Void _runCommand [:Command command]
(let [collectedArgs
[]
&mut lastCollector
@@ -126,7 +132,12 @@
`(object name ,(symbolName name) type ,type))]
`{
(method ,name [,@methodArgs] ,@body)
(dictSet commands ,(symbolName name) (object args [,@commandArgs] handler (the Function ,name)))}))
// Preserve the capitalization of the command name for pretty help message
(commandNames.push ,(symbolName name))
// Store the command name without capitalization for forgiving call conventions
(dictSet commands ,(ReaderExp.StrExp (.toLowerCase (symbolNameValue name))) (object args [,@commandArgs] handler (the Function ,name)))}))
(var :Array<String> commandNames [])
(defNew [&prop :Archive archive
&prop :ArchiveUI ui]

View File

@@ -10,9 +10,7 @@
(Sys.print ">> ")
(let [command
(.trim (.toString (.readLine (Sys.stdin))))]
(if (controller.commands.exists command)
(controller.runCommand (dictGet controller.commands command))
(Sys.println "$command is not a valid command")))))
(controller.tryRunCommand command))))
(defNew [])