From aed316967adcdab38183747125ec1cfa283d3484 Mon Sep 17 00:00:00 2001 From: lucaspalomodevelop Date: Sun, 14 May 2023 21:40:27 +0200 Subject: [PATCH] Automation of the help output --- src/command.cpp | 28 +++++++++++++++++++++++++--- src/command.h | 17 ++++++++++++++--- src/main.cpp | 20 ++++++++------------ src/main.h | 1 + 4 files changed, 48 insertions(+), 18 deletions(-) diff --git a/src/command.cpp b/src/command.cpp index 4388c1e..dd45437 100644 --- a/src/command.cpp +++ b/src/command.cpp @@ -1,6 +1,5 @@ #include "command.h" - Command::Command() {} Command::~Command() {} @@ -8,7 +7,20 @@ Command::~Command() {} // add a command to the command map void Command::addCommand(std::string name, void (*func)(int argc, char *argv[])) { - commands[name] = func; + CommandInfo mycommand{ + name, + "", + func}; + commands[name] = mycommand; +} + +void Command::addCommand(std::string name, std::string description, void (*func)(int argc, char *argv[])) +{ + CommandInfo mycommand{ + name, + description, + func}; + commands[name] = mycommand; } // add a default command to the command map @@ -29,7 +41,7 @@ void Command::runCommand(char *name, int argc, char *argv[]) argv[i] = argv[i + 1]; } - commands[name](argc, argv); + commands[name].func(argc, argv); } else { @@ -50,4 +62,14 @@ bool Command::isInCommands(char *name) } // std::cout << "Command not found: " << name << std::endl; return false; +} + +std::string Command::listCommands() +{ + std::string list = ""; + for (auto const &command : commands) + { + list += "\t" + command.second.name + " " + command.second.description + "\n"; + } + return list; } \ No newline at end of file diff --git a/src/command.h b/src/command.h index adb202e..80234be 100644 --- a/src/command.h +++ b/src/command.h @@ -5,6 +5,14 @@ #include #include + +struct CommandInfo +{ + std::string name; + std::string description; + void (*func)(int argc, char *argv[]); +}; + class Command { public: @@ -13,18 +21,21 @@ public: // add a command to the command map void addCommand(std::string name, void (*func)(int argc, char *argv[])); + void addCommand(std::string name, std::string description, void (*func)(int argc, char *argv[])); // add a default command to the command map - void addDefaultCommand(void (*func)(int argc,char *argv[])); + void addDefaultCommand(void (*func)(int argc, char *argv[])); // run a command void runCommand(char *name, int argc, char *argv[]); // check if a command is in the command map bool isInCommands(char *name); + std::string listCommands(); + private: // map of commands - std::map commands; + std::map commands; // default command - void (*defaultCommand)(int argc,char *argv[]); + void (*defaultCommand)(int argc, char *argv[]); }; #endif // COMMAND_H \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index bcbe4c5..6b91095 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -17,13 +17,13 @@ int main(int argc, char *argv[]) void input(int argc, char *argv[]) { InputParser input(argc, argv); - Command command = Command(); - command.addCommand("run", runScript); - command.addCommand("help", help); - command.addCommand("ls", listScripts); - command.addCommand("add", addScript); - command.addCommand("edit", editScript); + // std::cout << " [script] - Runs a script if autom has not command with that name" << std::endl; + command.addCommand("run", "[script] - Runs a script", runScript); + command.addCommand("help", "- Shows this help message", help); + command.addCommand("ls", "- Lists all scripts ", listScripts); + command.addCommand("add", "[script] - Adds a script", addScript); + command.addCommand("edit", "[script] - Edits a script", editScript); command.addDefaultCommand(runScript); command.runCommand(argv[1], argc, argv); } @@ -56,7 +56,7 @@ void addScript(int argc, char *argv[]) if (std::filesystem::exists(dir + "/" + argv[1])) { - std::cout << "Script " << argv[1] <<" already exists" << std::endl; + std::cout << "Script " << argv[1] << " already exists" << std::endl; return; } @@ -99,9 +99,5 @@ void help(int argc, char *argv[]) std::cout << "Usage: autom [command] [options]" << std::endl; std::cout << "Commands:" << std::endl; std::cout << " [script] - Runs a script if autom has not command with that name" << std::endl; - std::cout << " run [script] - Runs a script" << std::endl; - std::cout << " ls - Lists all scripts" << std::endl; - std::cout << " add [script] - Adds a script" << std::endl; - std::cout << " edit [script] - Edits a script" << std::endl; - std::cout << " help - Shows this help message" << std::endl; + std::cout << command.listCommands() << std::endl; } \ No newline at end of file diff --git a/src/main.h b/src/main.h index 8555de4..232abb5 100644 --- a/src/main.h +++ b/src/main.h @@ -18,6 +18,7 @@ std::string home = getenv("HOME"); // directory for autom scripts std::string dir = home + "/autom"; +Command command = Command(); // input function for parsing arguments and creating commands and running them void input(int argc, char *argv[]);