-
Notifications
You must be signed in to change notification settings - Fork 14
/
cmdline.cpp
39 lines (31 loc) · 885 Bytes
/
cmdline.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
#include <cstring>
#include <string>
#include <unordered_set>
class CommandLine final {
int argc;
char** argv;
std::unordered_set<std::string> arguments;
std::unordered_set<std::string> flags;
public:
CommandLine(int argc, char* argv[])
: argc(argc)
, argv(argv) {
for (int i=1; i < argc; i++) {
const std::string arg(argv[i]);
if (arg.size() >= 2 && arg[0] == '-' && arg[1] == '-') {
flags.insert(std::move(arg));
} else {
arguments.insert(std::move(arg));
}
}
}
bool has(const std::string& name) const {
return arguments.count(name);
}
bool has_flag(const std::string& name) const {
return flags.count(name) || flags.count("--" + name);
}
bool empty() const {
return arguments.empty();
}
};