mirror of
https://github.com/lucaspalomodevelop/autom.git
synced 2026-03-12 23:27:21 +00:00
commit
44543717c1
@ -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;
|
||||
}
|
||||
@ -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,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<std::string, void (*)(int argc,char *argv[])> commands;
|
||||
std::map<std::string, CommandInfo> commands;
|
||||
// default command
|
||||
void (*defaultCommand)(int argc,char *argv[]);
|
||||
void (*defaultCommand)(int argc, char *argv[]);
|
||||
};
|
||||
|
||||
#endif // COMMAND_H
|
||||
20
src/main.cpp
20
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;
|
||||
}
|
||||
@ -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[]);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user