From d24cca1bd7aea1be159eb24fdc84ff515be8d611 Mon Sep 17 00:00:00 2001 From: lucaspalomodevelop Date: Fri, 3 Nov 2023 16:15:02 +0100 Subject: [PATCH] add command aliases --- src/classes/command.hpp | 45 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/src/classes/command.hpp b/src/classes/command.hpp index 9f472bc..41df298 100644 --- a/src/classes/command.hpp +++ b/src/classes/command.hpp @@ -20,6 +20,7 @@ namespace lpstd std::string name; std::string description; void (*func)(); + std::vector aliases; std::vector args; }; @@ -101,6 +102,18 @@ namespace lpstd command.func = func; this->commands.push_back(command); } + void addCommandAlias(std::string name, std::string alias) + { + for (auto &commandInfo : this->commands) + { + if (commandInfo.name == name) + { + commandInfo.aliases.push_back(alias); + return; + } + } + std::cout << "Command not found" << std::endl; + } // void addCommand(std::string name, std::string description, void (*func)(int argc, char *argv[])) // { @@ -121,7 +134,26 @@ namespace lpstd for (auto &commandInfo : sortedCommands) { - help += commandInfo.name + " - " + commandInfo.description + "\n"; + if (commandInfo.aliases.size() == 0) + { + help += commandInfo.name + " - " + commandInfo.description + "\n"; + continue; + } + std::string aliases = ""; + + for (auto &alias : commandInfo.aliases) + { + if (aliases != "") + { + aliases += ", " + alias; + } + else + { + aliases += alias; + } + } + + help += commandInfo.name + " - " + commandInfo.description + " [ " + aliases + " ]\n"; } return help; @@ -178,6 +210,17 @@ namespace lpstd commandInfo.func(); return; } + + for (auto &alias : commandInfo.aliases) + { + if (alias == command) + { + commandInfo.args = args; + currentCommand = commandInfo; + commandInfo.func(); + return; + } + } } std::cout << "Command not found" << std::endl; }