Automation of the help output

This commit is contained in:
lucaspalomodevelop 2023-05-14 21:40:27 +02:00
parent 13645f26ac
commit aed316967a
4 changed files with 48 additions and 18 deletions

View File

@ -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
{
@ -51,3 +63,13 @@ 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;
}

View File

@ -5,6 +5,14 @@
#include <string>
#include <iostream>
struct CommandInfo
{
std::string name;
std::string description;
void (*func)(int argc, char *argv[]);
};
class Command
{
public:
@ -13,6 +21,7 @@ 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[]));
// run a command
@ -20,9 +29,11 @@ public:
// check if a command is in the command map
bool isInCommands(char *name);
std::string listCommands();
private:
// map of commands
std::map<std::string, void (*)(int argc,char *argv[])> commands;
std::map<std::string, CommandInfo> commands;
// default command
void (*defaultCommand)(int argc, char *argv[]);
};

View File

@ -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);
}
@ -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;
}

View File

@ -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[]);