From 0e86eff5fa98b12cd3d36ad2b92925f7dc6d1cfb Mon Sep 17 00:00:00 2001 From: Nat Quayle Nelson Date: Sun, 29 Aug 2021 13:51:36 -0600 Subject: [PATCH] NAT archiveController handle running commands by key --- src/nat/ArchiveController.kiss | 15 +++++++++++++-- src/nat/CLI.kiss | 4 +--- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/nat/ArchiveController.kiss b/src/nat/ArchiveController.kiss index 6aaf7a3..eeacbd7 100644 --- a/src/nat/ArchiveController.kiss +++ b/src/nat/ArchiveController.kiss @@ -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 commandNames []) (defNew [&prop :Archive archive &prop :ArchiveUI ui] diff --git a/src/nat/CLI.kiss b/src/nat/CLI.kiss index 1a96c65..51ceeb0 100644 --- a/src/nat/CLI.kiss +++ b/src/nat/CLI.kiss @@ -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 [])