-
Notifications
You must be signed in to change notification settings - Fork 1
/
SimpleCommandLineReader.cpp
48 lines (39 loc) · 1.22 KB
/
SimpleCommandLineReader.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
/*
Author: https://helloacm.com
Free to Use, donation is appreciated: https://helloacm.com/out/paypal
*/
#include <map>
#include <vector>
#include <algorithm>
#include "SimpleCommandLineReader.h"
using namespace std;
SimpleCommandLineReader::SimpleCommandLineReader(int argc, const char** argv): SimpleCommandLineReader(argc, argv, false) {
};
SimpleCommandLineReader::SimpleCommandLineReader(int argc, const char** argv, bool sensitive) : _caseSensitive(sensitive) {
// argv[0] can be skipped
for (int i = 1; i < argc; i ++) {
process(argv[i]);
}
};
void SimpleCommandLineReader::process(const string& arg) {
auto sepPos = arg.find(SepChar);
if(sepPos != string::npos) {
auto key = arg.substr(0, sepPos);
_dict[uniformString(key)] = arg.substr(sepPos + 1);
}
}
const string& SimpleCommandLineReader::Get(const string& key) const {
try {
return _dict.at(uniformString(key));
} catch (out_of_range&) {
throw runtime_error("No such value");
}
}
string SimpleCommandLineReader::Get(const string& key, const string& default_value) const noexcept {
try {
return Get(key);
}
catch(out_of_range&) {
return default_value;
}
}