From ab657b1e13910141c56a318aa6bfc4fb81f691be Mon Sep 17 00:00:00 2001 From: lucaspalomodevelop Date: Sat, 7 Oct 2023 18:19:05 +0200 Subject: [PATCH] modify command class --- src/command.hpp | 80 ++++++++++++++++++++++++++++++++++++++++++------ src/commands.hpp | 55 +++++++++++++++++++-------------- 2 files changed, 103 insertions(+), 32 deletions(-) diff --git a/src/command.hpp b/src/command.hpp index ca7b367..519da68 100644 --- a/src/command.hpp +++ b/src/command.hpp @@ -14,7 +14,8 @@ struct commandInfo { std::string name; std::string description; - void (*func)(int argc, char *argv[]); + void (*func)(); + std::vector args; }; class Command : public lpstd::Singleton @@ -24,6 +25,10 @@ private: std::list commands = {}; std::string defaultCommandName = "help"; + commandInfo currentCommand = {name : "", + description : "", + func : nullptr}; + public: Command(/* args */) { @@ -33,7 +38,51 @@ public: { } - void addCommand(std::string name, std::string description, void (*func)(int argc, char *argv[])) + commandInfo getCurrentCommand() + { + return this->currentCommand; + } + + template + bool existsArg(Args &&...arg) + { + + for (auto &argToFind : {arg...}) + { + std::vector::iterator it = std::find(this->currentCommand.args.begin(), this->currentCommand.args.end(), argToFind); + if (it != this->currentCommand.args.end()) + { + return true; + } + } + + return false; + } + + template + std::string getArg(Args &&...arg) + { + + if (this->currentCommand.args.size() == 0) + { + return ""; + } + + std::vector args = this->currentCommand.args; + std::vector argsToFind = {arg...}; + std::vector values = {}; + for (auto &argToFind : argsToFind) + { + std::vector::iterator it = std::find(args.begin(), args.end(), argToFind); + if (it != args.end()) + { + values.push_back(*(it + 1)); + } + } + return values.size() > 0 ? values[0] : ""; + } + + void addCommand(std::string name, std::string description, void (*func)(void)) { commandInfo command; command.name = name; @@ -42,14 +91,22 @@ public: this->commands.push_back(command); } + // void addCommand(std::string name, std::string description, void (*func)(int argc, char *argv[])) + // { + // commandInfo command; + // command.name = name; + // command.description = description; + // command.func = func; + // this->commands.push_back(command); + // } + std::string getHelpAsString() { std::string help = ""; std::list sortedCommands = this->commands; - sortedCommands.sort([](const commandInfo &a, const commandInfo &b) { - return a.name < b.name; - }); + sortedCommands.sort([](const commandInfo &a, const commandInfo &b) + { return a.name < b.name; }); for (auto &commandInfo : sortedCommands) { @@ -59,7 +116,7 @@ public: return help; } - void callCommand(std::string command,int argc, char *argv[] ) + void callCommand(std::string command, int argc, char *argv[]) { std::vector args; for (int i = 2; i < argc; i++) @@ -71,7 +128,9 @@ public: { if (commandInfo.name == command) { - commandInfo.func(argc, argv); + commandInfo.args = args; + currentCommand = commandInfo; + commandInfo.func(); return; } } @@ -83,7 +142,8 @@ public: if (argc < 2) { - std::cout << "Command not found \n" << std::endl; + std::cout << "Command not found \n" + << std::endl; // throw lpstd::exceptions::ParameterException("argc < 2"); this->callCommand(defaultCommandName, argc, argv); @@ -102,7 +162,9 @@ public: { if (commandInfo.name == command) { - commandInfo.func(argc, argv); + commandInfo.args = args; + currentCommand = commandInfo; + commandInfo.func(); return; } } diff --git a/src/commands.hpp b/src/commands.hpp index 5281062..0913d9d 100644 --- a/src/commands.hpp +++ b/src/commands.hpp @@ -11,52 +11,61 @@ namespace commands Command &command = Command::Instance(); - void add(int argc, char *argv[]) + void NotImplemented() { - std::cout << "add" << std::endl; + std::cout << "command: '" << command.getCurrentCommand().name << "' is not implemented" << std::endl; throw NotImplementedException(); } - void list(int argc, char *argv[]) + void add() { - std::cout << "list" << std::endl; - throw NotImplementedException(); + std::cout << command.getCurrentCommand().name << std::endl; + + if (command.existsArg("-n", "--namespace")) + { + std::cout << "namespace: " << command.getArg("-n", "--namespace") << std::endl; + } + else + { + std::cout << "namespace: " + << "default" << std::endl; + } + + // throw NotImplementedException(); } - void view(int argc, char *argv[]) + void list() { - std::cout << "view" << std::endl; - throw NotImplementedException(); + NotImplemented(); } - void edit(int argc, char *argv[]) + void view() { - std::cout << "edit" << std::endl; - throw NotImplementedException(); + NotImplemented(); } - void remove(int argc, char *argv[]) + void edit() { - std::cout << "remove" << std::endl; - throw NotImplementedException(); + NotImplemented(); } - void search(int argc, char *argv[]) + void remove() { - std::cout << "search" << std::endl; - throw NotImplementedException(); + NotImplemented(); } - // not implemented - void collect(int argc, char *argv[]) + void search() { - std::cout << "collect" << std::endl; - throw NotImplementedException(); + NotImplemented(); } - void help(int argc, char *argv[]) + void collect() + { + NotImplemented(); + } + + void help() { - // std::cout << "help" << std::endl; std::cout << command.getHelpAsString() << std::endl; }