forked from brichard19/BitCrack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CmdParse.cpp
83 lines (63 loc) · 1.3 KB
/
CmdParse.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include "CmdParse.h"
CmdParse::CmdParse()
{
}
void CmdParse::add(const std::string shortForm, bool hasArg)
{
this->add(shortForm, "", hasArg);
}
void CmdParse::add(const std::string shortForm, const std::string longForm, bool hasArg)
{
ArgType arg;
arg.shortForm = shortForm;
arg.longForm = longForm;
arg.hasArg = hasArg;
_argType.push_back(arg);
}
bool CmdParse::get(const std::string opt, ArgType &t)
{
for(unsigned int i = 0; i < _argType.size(); i++) {
if(_argType[i].shortForm == opt || _argType[i].longForm == opt) {
t = _argType[i];
return true;
}
}
return false;
}
void CmdParse::parse(int argc, char **argv)
{
for(int i = 1; i < argc; i++) {
std::string arg(argv[i]);
ArgType t;
if(get(arg, t)) {
// It is an option
OptArg a;
if(t.hasArg) {
// It requires an argument
if(i == argc - 1) {
throw std::string("-k requires an argument");
}
std::string optArg(argv[i + 1]);
i++;
a.option = arg;
a.arg = optArg;
} else {
// It does not require an argument
a.option = arg;
a.arg = "";
}
_optArgs.push_back(a);
} else {
// It is an operand
_operands.push_back(arg);
}
}
}
std::vector<OptArg> CmdParse::getArgs()
{
return _optArgs;
}
std::vector<std::string> CmdParse::getOperands()
{
return _operands;
}