From 37786bc8603cce8a2252e4d9db858ddcef142bc8 Mon Sep 17 00:00:00 2001 From: lucaspalomodevelop Date: Sat, 7 Oct 2023 15:03:27 +0200 Subject: [PATCH] add helpcommand if argc < 2 --- .gitignore | 1 + src/command.h | 36 ++++++++++++++++++++++++++---------- 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index 76d0e61..b7bb79f 100644 --- a/.gitignore +++ b/.gitignore @@ -28,6 +28,7 @@ # Executables *.exe +quicknote *.out *.app diff --git a/src/command.h b/src/command.h index 77fbee2..c3f0d2f 100644 --- a/src/command.h +++ b/src/command.h @@ -22,6 +22,7 @@ class Command : public lpstd::Singleton private: std::list commands = {}; + std::string defaultCommandName = "help"; public: Command(/* args */) @@ -44,27 +45,42 @@ public: 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) + for (auto &commandInfo : this->commands) { 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.func(argc, argv); + return; + } + } + std::cout << "Command not found" << std::endl; + } + void execute(int argc, char *argv[]) { if (argc < 2) { - std::cout << "Command not found" << std::endl; - throw lpstd::exceptions::ParameterException("argc < 2"); + std::cout << "Command not found \n" << std::endl; + // throw lpstd::exceptions::ParameterException("argc < 2"); + + this->callCommand(defaultCommandName, argc, argv); + return; }