mirror of
https://github.com/lucaspalomodevelop/autom.git
synced 2026-03-12 23:27:21 +00:00
add Commands (basic)
This commit is contained in:
parent
f00154f91c
commit
fd5b63991c
3
.gitignore
vendored
3
.gitignore
vendored
@ -1,3 +1,6 @@
|
||||
|
||||
/bin
|
||||
|
||||
# Prerequisites
|
||||
*.d
|
||||
|
||||
|
||||
53
.vscode/settings.json
vendored
Normal file
53
.vscode/settings.json
vendored
Normal file
@ -0,0 +1,53 @@
|
||||
{
|
||||
"files.associations": {
|
||||
"*.html": "html",
|
||||
"*.jsste": "plaintext",
|
||||
"string": "cpp",
|
||||
"array": "cpp",
|
||||
"atomic": "cpp",
|
||||
"bit": "cpp",
|
||||
"*.tcc": "cpp",
|
||||
"cctype": "cpp",
|
||||
"clocale": "cpp",
|
||||
"cmath": "cpp",
|
||||
"cstdarg": "cpp",
|
||||
"cstddef": "cpp",
|
||||
"cstdint": "cpp",
|
||||
"cstdio": "cpp",
|
||||
"cstdlib": "cpp",
|
||||
"cstring": "cpp",
|
||||
"ctime": "cpp",
|
||||
"cwchar": "cpp",
|
||||
"cwctype": "cpp",
|
||||
"deque": "cpp",
|
||||
"map": "cpp",
|
||||
"unordered_map": "cpp",
|
||||
"vector": "cpp",
|
||||
"exception": "cpp",
|
||||
"algorithm": "cpp",
|
||||
"functional": "cpp",
|
||||
"iterator": "cpp",
|
||||
"memory": "cpp",
|
||||
"memory_resource": "cpp",
|
||||
"numeric": "cpp",
|
||||
"optional": "cpp",
|
||||
"random": "cpp",
|
||||
"string_view": "cpp",
|
||||
"system_error": "cpp",
|
||||
"tuple": "cpp",
|
||||
"type_traits": "cpp",
|
||||
"utility": "cpp",
|
||||
"fstream": "cpp",
|
||||
"initializer_list": "cpp",
|
||||
"iosfwd": "cpp",
|
||||
"iostream": "cpp",
|
||||
"istream": "cpp",
|
||||
"limits": "cpp",
|
||||
"new": "cpp",
|
||||
"ostream": "cpp",
|
||||
"sstream": "cpp",
|
||||
"stdexcept": "cpp",
|
||||
"streambuf": "cpp",
|
||||
"typeinfo": "cpp"
|
||||
}
|
||||
}
|
||||
7
makefile
7
makefile
@ -1,3 +1,6 @@
|
||||
all:
|
||||
g++ -std=c++11 ./src/main.cpp -o ./src/autom
|
||||
./src/autom test
|
||||
mkdir -p bin
|
||||
g++ -c ./src/main.cpp
|
||||
g++ -c ./src/command.cpp
|
||||
g++ -std=c++11 main.o command.o -o ./bin/autom
|
||||
./bin/autom help
|
||||
29
src/command.cpp
Normal file
29
src/command.cpp
Normal file
@ -0,0 +1,29 @@
|
||||
#include "command.h"
|
||||
#include <iostream>
|
||||
|
||||
Command::Command() {}
|
||||
|
||||
Command::~Command() {}
|
||||
|
||||
void Command::addCommand(std::string name, void (*func)(char *argv[]))
|
||||
{
|
||||
commands[name] = func;
|
||||
}
|
||||
|
||||
void Command::addDefaultCommand(void (*func)(char *argv[]))
|
||||
{
|
||||
defaultCommand = func;
|
||||
}
|
||||
|
||||
void Command::runCommand(char *name, char *argv[])
|
||||
{
|
||||
std::cout << "Running command: " << name << std::endl;
|
||||
if (commands.count(name) > 0)
|
||||
{
|
||||
commands[name](argv);
|
||||
}
|
||||
else
|
||||
{
|
||||
defaultCommand(argv);
|
||||
}
|
||||
}
|
||||
21
src/command.h
Normal file
21
src/command.h
Normal file
@ -0,0 +1,21 @@
|
||||
#ifndef COMMAND_H
|
||||
#define COMMAND_H
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
|
||||
class Command
|
||||
{
|
||||
public:
|
||||
Command(void);
|
||||
~Command(void);
|
||||
void addCommand(std::string name, void (*func)(char *argv[]));
|
||||
void addDefaultCommand(void (*func)(char *argv[]));
|
||||
void runCommand(char *name, char *argv[]);
|
||||
|
||||
private:
|
||||
std::map<std::string, void (*)(char *argv[])> commands;
|
||||
void (*defaultCommand)(char *argv[]);
|
||||
};
|
||||
|
||||
#endif // COMMAND_H
|
||||
37
src/inputparser.h
Normal file
37
src/inputparser.h
Normal file
@ -0,0 +1,37 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
#include <map>
|
||||
|
||||
|
||||
class InputParser
|
||||
{
|
||||
std::map<std::string, std::string> args;
|
||||
public:
|
||||
InputParser(int argc, char *argv[]) {
|
||||
for (int i = 1; i < argc; i++) {
|
||||
std::string arg(argv[i]);
|
||||
if (arg.find("-") == 0) {
|
||||
std::string key = arg.substr(1);
|
||||
if (i + 1 < argc) {
|
||||
std::string value(argv[i + 1]);
|
||||
if (value.find("-") != 0) {
|
||||
args[key] = value;
|
||||
i++;
|
||||
} else {
|
||||
args[key] = "";
|
||||
}
|
||||
} else {
|
||||
args[key] = "";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
std::string getValue(std::string key) {
|
||||
return args[key];
|
||||
}
|
||||
bool hasKey(std::string key) {
|
||||
return args.count(key) > 0;
|
||||
}
|
||||
};
|
||||
48
src/main.cpp
48
src/main.cpp
@ -1,25 +1,35 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <sys/stat.h>
|
||||
#include "main.h"
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
|
||||
// make dir in ~/autom
|
||||
std::string home = getenv("HOME");
|
||||
std::string dir = home + "/autom";
|
||||
// std::cout << "dir: " << dir << std::endl;
|
||||
mkdir(dir.c_str(), 0777);
|
||||
|
||||
if (argc > 1 && argv[1][0] != '-')
|
||||
{
|
||||
std::string pre_script = "cd " + dir + " && ";
|
||||
std::string script = pre_script + dir + "/" + argv[1];
|
||||
system(script.c_str());
|
||||
}
|
||||
// else{
|
||||
// std::cout << "Usage: autom <script>" << std::endl;
|
||||
// }
|
||||
|
||||
input(argc, argv);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
void input(int argc, char *argv[])
|
||||
{
|
||||
InputParser input(argc, argv);
|
||||
Command command = Command();
|
||||
|
||||
command.addCommand("run", runScript);
|
||||
command.addCommand("help", help);
|
||||
command.addDefaultCommand(runScript);
|
||||
command.runCommand(argv[1], argv);
|
||||
}
|
||||
|
||||
void runScript(char *argv[])
|
||||
{
|
||||
std::string pre_script = "cd " + dir + " && ";
|
||||
std::string script = pre_script + dir + "/" + argv[2];
|
||||
system(script.c_str());
|
||||
}
|
||||
|
||||
void help(char *argv[])
|
||||
{
|
||||
std::cout << "Usage: autom [command] [options]" << std::endl;
|
||||
std::cout << "Commands:" << std::endl;
|
||||
std::cout << " run [script] - Runs a script" << std::endl;
|
||||
std::cout << " help - Shows this help message" << std::endl;
|
||||
}
|
||||
18
src/main.h
Normal file
18
src/main.h
Normal file
@ -0,0 +1,18 @@
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <sys/stat.h>
|
||||
#include "inputparser.h"
|
||||
#include "command.h"
|
||||
|
||||
std::string home = getenv("HOME");
|
||||
std::string dir = home + "/autom";
|
||||
|
||||
void input(int argc, char *argv[]);
|
||||
void runScript(char *argv[]);
|
||||
void help(char *argv[]);
|
||||
void create(char *argv[]);
|
||||
void remove(char *argv[]);
|
||||
void list(char *argv[]);
|
||||
void edit(char *argv[]);
|
||||
void run(char *argv[]);
|
||||
Loading…
x
Reference in New Issue
Block a user