no pointerfunctions in command

This commit is contained in:
lucaspalomodevelop 2024-03-22 21:45:42 +01:00
parent d91e80525d
commit a634b13f72
4 changed files with 43 additions and 22 deletions

View File

@ -4,7 +4,6 @@ Command::Command() {}
Command::~Command() {} Command::~Command() {}
// add a command alias to the command map // add a command alias to the command map
void Command::addCommandAlias(std::string name, std::string alias) void Command::addCommandAlias(std::string name, std::string alias)
{ {
@ -15,8 +14,26 @@ void Command::addCommandAlias(std::string name, std::string alias)
commands[alias] = mycommand; commands[alias] = mycommand;
} }
// add a command to the command map // // add a command to the command map
void Command::addCommand(std::string name, void (*func)(int argc, char *argv[])) // void Command::addCommand(std::string name, void (*func)(int argc, char *argv[]))
// {
// 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;
// }
void Command::addCommand(std::string name, std::function<void(int argc, char *argv[])> func)
{ {
CommandInfo mycommand{ CommandInfo mycommand{
name, name,
@ -25,7 +42,7 @@ void Command::addCommand(std::string name, void (*func)(int argc, char *argv[]))
commands[name] = mycommand; commands[name] = mycommand;
} }
void Command::addCommand(std::string name, std::string description, void (*func)(int argc, char *argv[])) void Command::addCommand(std::string name, std::string description, std::function<void(int argc, char *argv[])> func)
{ {
CommandInfo mycommand{ CommandInfo mycommand{
name, name,
@ -35,7 +52,12 @@ void Command::addCommand(std::string name, std::string description, void (*func)
} }
// add a default command to the command map // add a default command to the command map
void Command::addDefaultCommand(void (*func)(int argc, char *argv[])) // void Command::addDefaultCommand(void (*func)(int argc, char *argv[]))
// {
// defaultCommand = func;
// }
void Command::addDefaultCommand(std::function<void(int argc, char *argv[])> func)
{ {
defaultCommand = func; defaultCommand = func;
} }
@ -61,7 +83,6 @@ void Command::runCommand(std::string name, int argc, char *argv[])
} }
} }
// check if a command is in the command map // check if a command is in the command map
bool Command::isInCommands(std::string name) bool Command::isInCommands(std::string name)
{ {
@ -81,7 +102,6 @@ std::string Command::listCommands()
{ {
std::string list = ""; std::string list = "";
for (std::map<std::string, CommandInfo>::iterator it = commands.begin(); it != commands.end(); ++it) for (std::map<std::string, CommandInfo>::iterator it = commands.begin(); it != commands.end(); ++it)
{ {
list += "\t" + it->second.name + " " + it->second.description + "\n"; list += "\t" + it->second.name + " " + it->second.description + "\n";

View File

@ -4,12 +4,14 @@
#include <map> #include <map>
#include <string> #include <string>
#include <iostream> #include <iostream>
#include <functional>
struct CommandInfo struct CommandInfo
{ {
std::string name; std::string name;
std::string description; std::string description;
void (*func)(int argc, char *argv[]); // void (*func)(int argc, char *argv[]);
std::function<void(int argc, char *argv[])> func;
}; };
class Command class Command
@ -21,10 +23,13 @@ public:
// add a command alias to the command map // add a command alias to the command map
void addCommandAlias(std::string name, std::string alias); void addCommandAlias(std::string name, std::string alias);
// add a command to the command map // add a command to the command map
void addCommand(std::string name, void (*func)(int argc, char *argv[])); void addCommand(std::string name, std::function<void(int argc, char *argv[])> funcs);
void addCommand(std::string name, std::string description, void (*func)(int argc, char *argv[])); // void addCommand(std::string name, void (*func)(int argc, char *argv[]));
void addCommand(std::string name, std::string description, std::function<void(int argc, char *argv[])> funcs);
// void addCommand(std::string name, std::string description, void (*func)(int argc, char *argv[]));
// add a default command to the command map // add a default command to the command map
void addDefaultCommand(void (*func)(int argc, char *argv[])); // void addDefaultCommand(void (*func)(int argc, char *argv[]));
void addDefaultCommand(std::function<void(int argc, char *argv[])> funcs);
// run a command // run a command
void runCommand(std::string name, int argc, char *argv[]); void runCommand(std::string name, int argc, char *argv[]);
// check if a command is in the command map // check if a command is in the command map
@ -36,7 +41,8 @@ private:
// map of commands // map of commands
std::map<std::string, CommandInfo> commands; std::map<std::string, CommandInfo> commands;
// default command // default command
void (*defaultCommand)(int argc, char *argv[]); // void (*defaultCommand)(int argc, char *argv[]);
std::function<void(int argc, char *argv[])> defaultCommand;
}; };
#endif // COMMAND_H #endif // COMMAND_H

View File

@ -20,17 +20,12 @@ void input(int argc, char *argv[])
// std::cout << " [script] - Runs a script if autom has not command with that name" << std::endl; // 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("run", "[script] - Runs a script", runScript);
command.addCommand("help", "- Shows this help message", help); command.addCommand("help", "- Shows this help message", help);
command.addCommandAlias("help", "h");
command.addCommand("ls", "- Lists all scripts ", listScripts); command.addCommand("ls", "- Lists all scripts ", listScripts);
command.addCommandAlias("ls", "l");
command.addCommand("add", "[script] - Adds a script", addScript); command.addCommand("add", "[script] - Adds a script", addScript);
command.addCommandAlias("add", "a");
command.addCommand("edit", "[script] - Edits a script", editScript); command.addCommand("edit", "[script] - Edits a script", editScript);
command.addCommandAlias("edit", "e");
command.addCommand("remove", "[script] - Remove a script", removeScript); command.addCommand("remove", "[script] - Remove a script", removeScript);
command.addCommandAlias("remove", "r");
command.addCommand("show", "[script] - Shows a script", showScript); command.addCommand("show", "[script] - Shows a script", showScript);
command.addCommandAlias("show", "s");
command.addCommand("config", "<command> - Configures autom", config); command.addCommand("config", "<command> - Configures autom", config);
command.addDefaultCommand(runScript); command.addDefaultCommand(runScript);
@ -458,7 +453,7 @@ void addScript(int argc, char *argv[])
file.close(); file.close();
editScript(argv[1], dir); editScript_fn(argv[1], dir);
} }
// edit a script in the autom directory // edit a script in the autom directory
@ -500,10 +495,10 @@ void editScript(int argc, char *argv[])
dir = dir_options[num]; dir = dir_options[num];
} }
editScript(argv[1], dir); editScript_fn(argv[1], dir);
} }
void editScript(std::string name, std::string dir) void editScript_fn(std::string name, std::string dir)
{ {
std::string script = dir + "/" + name; std::string script = dir + "/" + name;

View File

@ -41,7 +41,7 @@ void listScripts(int argc, char *argv[]);
void addScript(int argc, char *argv[]); void addScript(int argc, char *argv[]);
// edit a script in the autom directory // edit a script in the autom directory
void editScript(int argc, char *argv[]); void editScript(int argc, char *argv[]);
void editScript(std::string name, std::string dir); void editScript_fn(std::string name, std::string dir);
// remove a script in the autom directory // remove a script in the autom directory
void removeScript(int argc, char *argv[]); void removeScript(int argc, char *argv[]);
// show a script in the autom directory // show a script in the autom directory