[Orxonox-commit 2572] r7277 - code/branches/consolecommands3/src/libraries/core/command

landauf at orxonox.net landauf at orxonox.net
Tue Aug 31 01:03:09 CEST 2010


Author: landauf
Date: 2010-08-31 01:03:09 +0200 (Tue, 31 Aug 2010)
New Revision: 7277

Modified:
   code/branches/consolecommands3/src/libraries/core/command/CommandExecutor.cc
   code/branches/consolecommands3/src/libraries/core/command/CommandExecutor.h
Log:
added new console command "alias" that can bind commands (including arguments) to a new name

Modified: code/branches/consolecommands3/src/libraries/core/command/CommandExecutor.cc
===================================================================
--- code/branches/consolecommands3/src/libraries/core/command/CommandExecutor.cc	2010-08-30 23:00:20 UTC (rev 7276)
+++ code/branches/consolecommands3/src/libraries/core/command/CommandExecutor.cc	2010-08-30 23:03:09 UTC (rev 7277)
@@ -45,6 +45,9 @@
     SetConsoleCommand("unhide", &CommandExecutor::unhide)
         .argumentCompleter(0, autocompletion::hiddencommand());
 
+    SetConsoleCommand("alias", &CommandExecutor::alias)
+        .argumentCompleter(1, autocompletion::command());
+
     /* static */ CommandExecutor& CommandExecutor::getInstance()
     {
         static CommandExecutor instance;
@@ -156,4 +159,36 @@
     {
         return CommandExecutor::queryMT(command);
     }
+
+    /* static */ void CommandExecutor::alias(const std::string& alias, const std::string& command)
+    {
+        CommandEvaluation evaluation = CommandExecutor::evaluate(command);
+        if (evaluation.isValid())
+        {
+            ExecutorPtr executor = new Executor(*evaluation.getConsoleCommand()->getExecutor().get());
+
+            if (!evaluation.evaluateParams())
+            {
+                for (size_t i = 0; i < MAX_FUNCTOR_ARGUMENTS; ++i)
+                    executor->setDefaultValue(i, evaluation.getEvaluatedParameter(i));
+            }
+
+            SubString tokens(alias, " ");
+
+            if ((tokens.size() == 1 && ConsoleCommand::getCommand(tokens[0])) || (tokens.size() == 2 && ConsoleCommand::getCommand(tokens[0], tokens[1])))
+            {
+                COUT(1) << "Error: A command with name \"" << alias << "\" already exists." << std::endl;
+                return;
+            }
+
+            if (tokens.size() == 1)
+                createConsoleCommand(tokens[0], executor);
+            else if (tokens.size() == 2)
+                createConsoleCommand(tokens[0], tokens[1], executor);
+            else
+                COUT(1) << "Error: \"" << alias << "\" is not a valid alias name (must have one or two words)." << std::endl;
+        }
+        else
+            COUT(1) << "Error: \"" << command << "\" is not a valid command (did you mean \"" << evaluation.getCommandSuggestion() << "\"?)." << std::endl;
+    }
 }

Modified: code/branches/consolecommands3/src/libraries/core/command/CommandExecutor.h
===================================================================
--- code/branches/consolecommands3/src/libraries/core/command/CommandExecutor.h	2010-08-30 23:00:20 UTC (rev 7276)
+++ code/branches/consolecommands3/src/libraries/core/command/CommandExecutor.h	2010-08-30 23:03:09 UTC (rev 7277)
@@ -59,6 +59,7 @@
             static const int Denied = 4;
 
             static MultiType unhide(const std::string& command);
+            static void alias(const std::string& alias, const std::string& command);
             static void _autocomplete(const std::string& group, const std::string& name) {}
 
         private:




More information about the Orxonox-commit mailing list