diff --git a/src/command.cpp b/src/command.cpp index fb54856..ae89ab0 100644 --- a/src/command.cpp +++ b/src/command.cpp @@ -4,7 +4,6 @@ Command::Command() {} Command::~Command() {} - // add a command alias to the command map 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; } -// add a command to the command map -void Command::addCommand(std::string name, void (*func)(int argc, char *argv[])) +// // add a command to the command map +// 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 func) { CommandInfo mycommand{ name, @@ -25,7 +42,7 @@ void Command::addCommand(std::string name, void (*func)(int argc, char *argv[])) 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 func) { CommandInfo mycommand{ name, @@ -35,7 +52,12 @@ void Command::addCommand(std::string name, std::string description, void (*func) } // 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 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 bool Command::isInCommands(std::string name) { @@ -81,7 +102,6 @@ std::string Command::listCommands() { std::string list = ""; - for (std::map::iterator it = commands.begin(); it != commands.end(); ++it) { list += "\t" + it->second.name + " " + it->second.description + "\n"; diff --git a/src/command.h b/src/command.h index ed97960..d00841f 100644 --- a/src/command.h +++ b/src/command.h @@ -4,12 +4,14 @@ #include #include #include +#include struct CommandInfo { std::string name; std::string description; - void (*func)(int argc, char *argv[]); + // void (*func)(int argc, char *argv[]); + std::function func; }; class Command @@ -21,10 +23,13 @@ public: // add a command alias to the command map void addCommandAlias(std::string name, std::string alias); // 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[])); + void addCommand(std::string name, std::function funcs); + // void addCommand(std::string name, void (*func)(int argc, char *argv[])); + void addCommand(std::string name, std::string description, std::function funcs); + // 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[])); + void addDefaultCommand(std::function funcs); // run a command void runCommand(std::string name, int argc, char *argv[]); // check if a command is in the command map @@ -36,7 +41,8 @@ private: // map of commands std::map commands; // default command - void (*defaultCommand)(int argc, char *argv[]); + // void (*defaultCommand)(int argc, char *argv[]); + std::function defaultCommand; }; #endif // COMMAND_H \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 2657add..19c1317 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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; command.addCommand("run", "[script] - Runs a script", runScript); command.addCommand("help", "- Shows this help message", help); - command.addCommandAlias("help", "h"); + command.addCommand("ls", "- Lists all scripts ", listScripts); - command.addCommandAlias("ls", "l"); command.addCommand("add", "[script] - Adds a script", addScript); - command.addCommandAlias("add", "a"); command.addCommand("edit", "[script] - Edits a script", editScript); - command.addCommandAlias("edit", "e"); command.addCommand("remove", "[script] - Remove a script", removeScript); - command.addCommandAlias("remove", "r"); command.addCommand("show", "[script] - Shows a script", showScript); - command.addCommandAlias("show", "s"); command.addCommand("config", " - Configures autom", config); command.addDefaultCommand(runScript); @@ -458,7 +453,7 @@ void addScript(int argc, char *argv[]) file.close(); - editScript(argv[1], dir); + editScript_fn(argv[1], dir); } // edit a script in the autom directory @@ -500,10 +495,10 @@ void editScript(int argc, char *argv[]) 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; diff --git a/src/main.h b/src/main.h index 20a91a5..91cea4e 100644 --- a/src/main.h +++ b/src/main.h @@ -41,7 +41,7 @@ void listScripts(int argc, char *argv[]); void addScript(int argc, char *argv[]); // edit a script in the autom directory 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 void removeScript(int argc, char *argv[]); // show a script in the autom directory