-
Notifications
You must be signed in to change notification settings - Fork 4
/
CommandlineParser.h
58 lines (47 loc) · 1.39 KB
/
CommandlineParser.h
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
#ifndef INPUTPARSER_H
#define INPUTPARSER_H
#include <string>
#include <iostream>
#include <map>
class CommandlineParser {
private:
std::map<std::string, std::string> parameters;
public:
CommandlineParser (char **begin, char **end) {
std::string lastArg = "";
std::string currentArg = "";
for (char **it = ++begin; it != end; ++it) {
currentArg = *it;
if (currentArg.substr(0, 2) == "--") {
if (lastArg != "") {
parameters.insert({lastArg, ""});
lastArg = currentArg;
continue;
}
lastArg = currentArg;
continue;
}
if (lastArg == "") {
std::cout << "Invalid parameter: " << currentArg << std::endl;
continue;
}
parameters.insert({lastArg, currentArg});
lastArg = "";
}
if (lastArg != "") {
parameters.insert({lastArg, ""});
}
}
std::string getCmdOption(const std::string &option) {
auto it = parameters.find(option);
if (it == parameters.end()) {
return "";
}
return it->second;
}
bool cmdOptionExists(const std::string &option) {
auto it = parameters.find(option);
return it != parameters.end();
}
};
#endif // INPUTPARSER_H