Newer
Older
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
#pragma once
#include <unordered_map>
#include <unordered_set>
#include <iostream>
#include <string>
#include "ErrorCodes.h"
typedef std::unordered_map<std::string, std::string> KeyValueMap;
typedef std::unordered_set<std::string> AcceptedKeys;
#define ACCEPTED_KEYS { "--input", "--bounds" }
class ParseCommandLine
{
private:
std::string _launchPath;
KeyValueMap _cmdLineMap;
std::vector<ErrorCodes> _errors;
std::vector<std::string> _errorMsgs;
private:
static AcceptedKeys s_validKeys;
inline bool validKey(const std::string& key)
{
return s_validKeys.count(key);
}
public:
ParseCommandLine(int argc, char** argv)
{
_launchPath = argv[0];
for (int i = 1; i < argc; i += 2)
{
std::string key = argv[i];
if (validKey(key))
_cmdLineMap.emplace(key.substr(2), argv[i + 1]);
else
{
_errors.emplace_back(ErrorCodes::InvalidArgument);
_errorMsgs.emplace_back(key);
}
}
}
inline const std::string& getValue(const std::string& key) const
{
return _cmdLineMap.at(key);
}
inline const bool hasKey(const std::string& key) const
{
if (_cmdLineMap.find(key) == _cmdLineMap.end())
return false;
return true;
}
const bool hasErrors() const { return _errors.size() > 0; }
void dumpErrors();
};