diff --git a/src/classes/command.hpp b/src/classes/command.hpp index ae6be8c..6abae33 100644 --- a/src/classes/command.hpp +++ b/src/classes/command.hpp @@ -10,166 +10,174 @@ #include #include "../patterns/singleton.hpp" -struct commandInfo +namespace lpstd { - std::string name; - std::string description; - void (*func)(); - std::vector args; -}; - -class Command : public lpstd::Singleton -{ - -private: - std::list commands = {}; - std::string defaultCommandName = "help"; - - commandInfo currentCommand = {name : "", - description : "", - func : nullptr}; - -public: - Command(/* args */) - { - } - - ~Command() - { - } - - commandInfo getCurrentCommand() - { - return this->currentCommand; - } - - template - bool existsArg(Args &&...arg) + namespace classes { - for (auto &argToFind : {arg...}) + struct commandInfo { - std::vector::iterator it = std::find(this->currentCommand.args.begin(), this->currentCommand.args.end(), argToFind); - if (it != this->currentCommand.args.end()) + std::string name; + std::string description; + void (*func)(); + std::vector args; + }; + + class Command : public lpstd::Singleton + { + + private: + std::list commands = {}; + std::string defaultCommandName = "help"; + + commandInfo currentCommand = {name : "", + description : "", + func : nullptr}; + + public: + Command(/* args */) { - 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()) + ~Command() { - 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; - command.description = description; - command.func = func; - 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; }); - - for (auto &commandInfo : sortedCommands) - { - help += commandInfo.name + " - " + commandInfo.description + "\n"; - } - - return help; - } - - void callCommand(std::string command, int argc, char *argv[]) - { - std::vector args; - for (int i = 2; i < argc; i++) - { - args.push_back(argv[i]); - } - - for (auto &commandInfo : this->commands) - { - if (commandInfo.name == command) + commandInfo getCurrentCommand() { - commandInfo.args = args; - currentCommand = commandInfo; - commandInfo.func(); - return; + return this->currentCommand; } - } - std::cout << "Command not found" << std::endl; - } - void execute(int argc, char *argv[]) - { - - if (argc < 2) - { - std::cout << "Command not found \n" - << std::endl; - // throw lpstd::exceptions::ParameterException("argc < 2"); - - this->callCommand(defaultCommandName, argc, argv); - - return; - } - - std::string command = argv[1]; - std::vector args; - for (int i = 2; i < argc; i++) - { - args.push_back(argv[i]); - } - - for (auto &commandInfo : this->commands) - { - if (commandInfo.name == command) + template + bool existsArg(Args &&...arg) { - commandInfo.args = args; - currentCommand = commandInfo; - commandInfo.func(); - return; + + 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; } - } - std::cout << "Command not found" << std::endl; - } -}; + + 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; + command.description = description; + command.func = func; + 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; }); + + for (auto &commandInfo : sortedCommands) + { + help += commandInfo.name + " - " + commandInfo.description + "\n"; + } + + return help; + } + + void callCommand(std::string command, int argc, char *argv[]) + { + std::vector args; + for (int i = 2; i < argc; i++) + { + args.push_back(argv[i]); + } + + for (auto &commandInfo : this->commands) + { + if (commandInfo.name == command) + { + commandInfo.args = args; + currentCommand = commandInfo; + commandInfo.func(); + return; + } + } + std::cout << "Command not found" << std::endl; + } + + void execute(int argc, char *argv[]) + { + + if (argc < 2) + { + std::cout << "Command not found \n" + << std::endl; + // throw lpstd::exceptions::ParameterException("argc < 2"); + + this->callCommand(defaultCommandName, argc, argv); + + return; + } + + std::string command = argv[1]; + std::vector args; + for (int i = 2; i < argc; i++) + { + args.push_back(argv[i]); + } + + for (auto &commandInfo : this->commands) + { + if (commandInfo.name == command) + { + commandInfo.args = args; + currentCommand = commandInfo; + commandInfo.func(); + return; + } + } + std::cout << "Command not found" << std::endl; + } + }; + + } // namespace classes +} // namespace lpstd #endif // __LPSTD_COMMAND_H__ \ No newline at end of file