-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathcmdParser.h
63 lines (55 loc) · 1.34 KB
/
cmdParser.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
59
60
61
62
63
#pragma once
#include <vector>
#include <string>
namespace Util {
class ParsedArgs {
struct OptionalArg {
std::string option;
std::string value;
};
std::vector<std::string> inputArgs;
std::vector<std::string> flags;
std::vector<OptionalArg> optionals;
public:
ParsedArgs(int argc, const char** argv) {
for(int i = 1; i < argc; i++) {
const char* item = argv[i];
if(item[0] == '-') {
if(item[1] == '-') {
if(i + 1 < argc) {
optionals.push_back(OptionalArg{std::string(item + 2), std::string(argv[i + 1])});
i++;
} else {
throw "No associated value for the final optional arg";
}
} else {
flags.emplace_back(item + 1);
}
} else {
inputArgs.emplace_back(item);
}
}
}
bool hasFlag(const char* flag) const {
for(const std::string& f : flags) {
if(f == flag) {
return true;
}
}
return false;
}
std::string getOptional(const char* key) const {
for(const OptionalArg& o : optionals) {
if(o.option == key) {
return o.value;
}
}
return std::string();
}
std::size_t argCount() const { return inputArgs.size(); }
const std::vector<std::string>& args() { return inputArgs; }
const std::string& operator[](std::size_t index) const { return inputArgs[index]; }
auto begin() const { return inputArgs.begin(); }
auto end() const { return inputArgs.end(); }
};
};