1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
#include "cmd/cmd.h"
#include "utils.h"
#include <iostream>
#include <exception>
#include <algorithm>
#include <map>
const std::map<std::string, std::vector<int>> nargs({
{"cp", {2}},
{"mv", {2}},
{"attacks", {1}},
{"attack", {3}},
{"damage", {2, 3}},
{"heal", {2}},
{"spellcasting", {2, 3, 4}},
});
void checkArgs(std::string cmd, std::vector<std::string> args) {
if(nargs.contains(cmd)) {
auto& allowed = nargs.at(cmd);
if(std::find(allowed.begin(), allowed.end(), args.size()) == allowed.end()) {
throw std::runtime_error("Subcommand '" + cmd + "' expected " + utils::join(allowed, ", ") + " arguments but got " + std::to_string(args.size()));
}
}
}
// Removes flags from args (in-place) and returns map of options
std::map<std::string, std::string> extractFlags(std::vector<std::string>& args, const std::vector<std::string>& knownOptions = {}) {
std::map<std::string, std::string> ret;
auto it = args.begin();
while(it != args.end()) {
if((*it)[0] == '-') {
while((*it)[0] == '-') {
(*it).erase((*it).begin());
}
if(it < args.end()-1) {
ret.emplace(*it, *(it+1));
if(std::find(knownOptions.begin(), knownOptions.end(), *it) != knownOptions.end()) {
args.erase(it);
}
} else {
ret.emplace(*it, std::string());
}
args.erase(it);
} else {
it++;
}
}
return ret;
}
int main(int argc, char *argv[]) {
// Full thing is surrounded in try/catch to plesantly print out any errors
try {
std::string exename = argv[0];
std::vector<std::string> args(&argv[1], &argv[argc]);
auto argsWithFlags = args;
auto flags = extractFlags(args);
cmd::mkdir({cmd::getTruePath("")});
if(args.empty()) {
std::cout << cmd::list(args);
return 0;
}
std::string cmd = args[0];
std::vector<std::string> argsOrig(args);
args.erase(args.begin());
checkArgs(cmd, args);
if(cmd == "ls") std::cout << cmd::list(args);
else if(cmd == "cp") std::cout << cmd::cp(args);
else if(cmd == "mkdir") std::cout << cmd::mkdir(args);
else if(cmd == "mv") std::cout << cmd::mv(args);
else if(cmd == "rm") std::cout << cmd::rm(args);
else if(cmd == "attacks") std::cout << cmd::attacks(args);
else if(cmd == "roll") std::cout << cmd::roll(args);
else if(cmd == "attack") std::cout << cmd::attack(args, flags);
else if(cmd == "damage") std::cout << cmd::damage(args, flags);
else if(cmd == "heal") std::cout << cmd::heal(args);
else if(cmd == "save") {
args = argsWithFlags;
extractFlags(args, {"damage", "type"});
args.erase(args.begin());
std::cout << cmd::save(args, flags);
}
else if(cmd == "reset") std::cout << cmd::reset(args);
else if(cmd == "set") std::cout << cmd::set(args);
else if(cmd == "add") std::cout << cmd::add(args);
else if(cmd == "del") std::cout << cmd::del(args);
else if(cmd == "edit") std::cout << cmd::edit(args);
else if(cmd == "spellcasting") std::cout << cmd::spellcasting(args);
else if(cmd == "git") std::cout << cmd::git(argsWithFlags);
else if(cmd == "help") std::cout << cmd::usage(exename);
else std::cout << cmd::list(argsOrig);
} catch(std::exception& e) {
std::cerr << e.what() << std::endl;
return 1;
}
return 0;
}
|