-
Notifications
You must be signed in to change notification settings - Fork 0
/
argumentParser.cpp
106 lines (97 loc) · 3.65 KB
/
argumentParser.cpp
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
99
100
101
102
103
104
105
106
#include "argumentParser.h"
void ArgumentParser::parse(int argc, char** argv, const std::vector<Argument>& arguments) {
shortNameToArgument.clear();
longNameToArgument.clear();
for (Argument argument : arguments) {
shortNameToArgument[argument.shortName] = argument;
longNameToArgument[argument.longName] = argument;
}
std::vector<std::string> commandLineArguments;
for (int i = 1; i < argc; ++i) {
commandLineArguments.push_back(std::string(argv[i]));
}
for (unsigned int i = 0; i < commandLineArguments.size(); ++i) {
parse(commandLineArguments, i);
}
}
void ArgumentParser::parse(const std::vector<std::string>& stringArgument, unsigned int& index) {
std::string currentArgument = stringArgument[index];
if (currentArgument.length() < 1 || currentArgument[0] != '-') {
throw GeneralException("Argument can only start with '-'");
}
if (currentArgument[1] == '-') {
std::string argumentName = currentArgument.substr(2);
if (longNameToArgument.find(argumentName) == longNameToArgument.end()) {
throw GeneralException(std::string("Argument '") + argumentName + "' is invalid");
}
Argument argument = longNameToArgument.at(argumentName);
parsedArguments[argument.shortName] = parseArgument(stringArgument, argument.type, index);
} else {
for (unsigned int i = 1; i < currentArgument.size(); ++i) {
if (shortNameToArgument.find(currentArgument[i]) == shortNameToArgument.end()) {
throw GeneralException(std::string("Argument '") + currentArgument[i] + "' is invalid");
}
Argument argument = shortNameToArgument.at(currentArgument[i]);
if (argument.type != BOOLEAN_ARGUMENT && i < currentArgument.size() - 1) {
throw GeneralException("Numeric argument cannot be in the middle of bool arguments");
}
parsedArguments[argument.shortName] = parseArgument(stringArgument, argument.type, index);
}
}
}
bool isPositiveNumeric(const std::string& str) {
for (unsigned int i = 0; i < str.size(); ++i) {
if (str[i] < '0' || str[i] > '9') {
return false;
}
}
return true;
}
std::string ArgumentParser::parseArgument(const std::vector<std::string>& stringArgument, const ArgumentType type, unsigned int& index) {
if (type == BOOLEAN_ARGUMENT) {
return "1";
}
else if (type == INTEGER_ARGUMENT) {
++index;
if (!isPositiveNumeric(stringArgument[index])) {
throw GeneralException("Expect numeric value for argument");
}
return stringArgument[index];
} else if (type == STRING_ARGUMENT) {
++index;
return stringArgument[index];
} else {
throw GeneralException("Unknown argument type");
}
}
std::map<std::string, std::string> ArgumentParser::getRawMap() {
std::map<std::string, std::string> result;
for (auto it : parsedArguments) {
const Argument& argument = shortNameToArgument[it.first];
result[argument.longName] = it.second;
}
return result;
}
std::string ArgumentParser::getStringArgument(char shortName) const {
return parsedArguments.at(shortName);
}
int ArgumentParser::getIntArgument(char shortName) const {
return atoi(parsedArguments.at(shortName).c_str());
}
bool ArgumentParser::isArgumentPresent(char shortName) const {
return parsedArguments.find(shortName) != parsedArguments.end();
}
std::string ArgumentParser::getStringArgumentOrReturnDefault(char shortName, const std::string& defaultValue) const {
if (parsedArguments.find(shortName) != parsedArguments.end()) {
return parsedArguments.at(shortName);
} else {
return defaultValue;
}
}
int ArgumentParser::getIntArgumentOrReturnDefault(char shortName, int defaultValue) const {
if (parsedArguments.find(shortName) != parsedArguments.end()) {
return atoi(parsedArguments.at(shortName).c_str());
} else {
return defaultValue;
}
}