Merge pull request #3 from lucaspalomodevelop/dev

Dev
This commit is contained in:
Lucas Palomo Develop 2023-04-13 21:13:53 +02:00 committed by GitHub
commit 002b12a048
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 51 additions and 15 deletions

View File

@ -2,4 +2,14 @@
__IN DEVELOPMENT, NOT READY FOR USE__
autom is a command line tool for executing shell scripts, independent of the working directory
autom is a command line tool for executing shell scripts, independent of the working directory
## Installation-Requirments:
gcc
make
## Compile:
```bash
make
```

View File

@ -5,19 +5,22 @@ Command::Command() {}
Command::~Command() {}
// add a command to the command map
void Command::addCommand(std::string name, void (*func)(int argc, char *argv[]))
{
commands[name] = func;
}
// add a default command to the command map
void Command::addDefaultCommand(void (*func)(int argc, char *argv[]))
{
defaultCommand = func;
}
// run a command
void Command::runCommand(char *name, int argc, char *argv[])
{
std::cout << "Running command: " << name << std::endl;
// std::cout << "Running command: " << name << std::endl;
if (this->isInCommands(name))
{
@ -34,16 +37,17 @@ void Command::runCommand(char *name, int argc, char *argv[])
}
}
// check if a command is in the command map
bool Command::isInCommands(char *name)
{
for (auto const &command : commands)
{
if (command.first == name)
{
std::cout << "Command found: " << command.first << std::endl;
// std::cout << "Command found: " << command.first << std::endl;
return true;
}
}
std::cout << "Command not found: " << name << std::endl;
// std::cout << "Command not found: " << name << std::endl;
return false;
}

View File

@ -10,13 +10,20 @@ class Command
public:
Command(void);
~Command(void);
// add a command to the command map
void addCommand(std::string name, 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
void runCommand(char *name, int argc, char *argv[]);
// check if a command is in the command map
bool isInCommands(char *name);
private:
// map of commands
std::map<std::string, void (*)(int argc,char *argv[])> commands;
// default command
void (*defaultCommand)(int argc,char *argv[]);
};

View File

@ -28,10 +28,14 @@ InputParser::InputParser(int argc, char *argv[])
}
}
}
// get the value of a key
std::string InputParser::getValue(std::string key)
{
return args[key];
}
// check if a key exists
bool InputParser::hasKey(std::string key)
{
return args.count(key) > 0;

View File

@ -11,9 +11,11 @@ class InputParser
{
std::map<std::string, std::string> args;
public:
public:
InputParser(int argc, char *argv[]);
// get the value of a key
std::string getValue(std::string key);
// check if a key exists
bool hasKey(std::string key);
};

View File

@ -13,6 +13,7 @@ int main(int argc, char *argv[])
return 0;
}
// input function for parsing arguments and creating commands and running them
void input(int argc, char *argv[])
{
InputParser input(argc, argv);
@ -21,23 +22,26 @@ void input(int argc, char *argv[])
command.addCommand("run", runScript);
command.addCommand("help", help);
command.addDefaultCommand(runScript);
command.runCommand(argv[1],argc, argv);
command.runCommand(argv[1], argc, argv);
}
void runScript(int argc,char *argv[])
// run a script with is in the autom directory
void runScript(int argc, char *argv[])
{
std::cout << "Running script: " << argv[1] << std::endl;
// std::cout << "Running script: " << argv[1] << std::endl;
std::string pre_script = "cd " + dir + " && ";
std::string script = pre_script + dir + "/" + argv[1];
std::cout << "Running script: " << script << std::endl;
// std::cout << "Running script: " << script << std::endl;
system(script.c_str());
}
void help(int argc,char *argv[])
// help function for showing help message
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 << " help - Shows this help message" << std::endl;
}

View File

@ -5,6 +5,7 @@
#include "inputparser.h"
#include "command.h"
// directory for autom scripts
#ifdef _WIN32
#include <direct.h>
std::string home = getenv("USERPROFILE");
@ -12,13 +13,17 @@ std::string home = getenv("USERPROFILE");
std::string home = getenv("HOME");
#endif
// directory for autom scripts
std::string dir = home + "/autom";
// input function for parsing arguments and creating commands and running them
void input(int argc, char *argv[]);
// run a script with is in the autom directory
void runScript(int argc,char *argv[]);
// help function for showing help message
void help(int argc,char *argv[]);
void create(int argc,char *argv[]);
void remove(int argc,char *argv[]);
void list(int argc,char *argv[]);
void edit(int argc,char *argv[]);
void run(int argc,char *argv[]);
// void create(int argc,char *argv[]);
// void remove(int argc,char *argv[]);
// void list(int argc,char *argv[]);
// void edit(int argc,char *argv[]);
// void run(int argc,char *argv[]);