-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOptionParser.h
185 lines (161 loc) · 3.09 KB
/
OptionParser.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#pragma once
class OptionVar
{
std::string name;
std::string value;
bool is_flag;
public:
OptionVar()
{
}
OptionVar(const char* name, const char* def, bool is_flag)
{
this->name = name;
this->value = def;
this->is_flag = is_flag;
}
bool IsFlag()
{
return this->is_flag;
}
bool GetBool()
{
return this->value == "1" || this->value == "true" || this->value == "True" || this->value == "TRUE";
}
int GetInt()
{
if (this->value.length() == 0)
return 0;
try
{
return std::stoi(this->value);
}
catch (...)
{
return 0;
}
}
std::string GetString()
{
return value;
}
void SetValue(const std::string& value)
{
this->value = value;
}
};
class OptionParser
{
std::map<std::string, OptionVar> vars;
public:
std::vector<std::string> commands;
OptionVar& GetOption(const char* name)
{
return vars[name];
}
void AddOption(const char* name, const char* def, bool is_flag)
{
if (vars.find(name) == vars.end())
vars[name] = OptionVar(name, def, is_flag);
}
void Parse(const std::string& aargs)
{
char* command = new char[aargs.size()+1];
strcpy(command, aargs.c_str());
char* args[400] = {};
int numargs = 0;
int i = 0; bool inquotes = false;
//add a dummy arg with the current location
numargs = 1;
args[0] = "~ignore me~";
while (command[i])
{
if (command[i] == '"')
{
inquotes = !inquotes;
if (inquotes == false)
command[i] = 0;
}
else if (command[i] == ' ' && inquotes == false)
{
command[i] = 0;
numargs++;
}
else
{
if (args[numargs] == 0)
args[numargs] = &command[i];
}
i++;
}
if (args[numargs] != 0)
numargs++;
this->Parse(numargs, args);
delete[] command;
}
void Parse(int argc, char ** args)
{
for (int i = 1; i < argc; i++)
{
if (args[i][0] == '-' && args[i][1] == '-')
{
std::string option = &args[i][2];
std::string value;
if (i + 1 < argc)
value = args[i+1];
auto find = vars.find(option);
if (find != vars.end())
{
if (value.length() && value[0] != '-')
i++;
else
value = "true";
vars[option].SetValue(value);
}
else
{
printf("WARNING: Argument/flag '%s' does not exist\n", option.c_str());
vars[option] = OptionVar(option.c_str(), value.c_str(), true);
}
}
else if (args[i][0] == '-')
{
//its an arg
char o = args[i][1];
std::string option;
option += o;
//read in value
std::string value;
int pos = 2;
while (args[i][pos])
value += args[i][pos++];
auto find = vars.find(option);
if (find != vars.end())
{
if (value == "" && find->second.IsFlag())
{
vars[option].SetValue("true");
}
else
{
vars[option].SetValue(value);
}
}
else
{
printf("WARNING: Argument/flag '%s' does not exist\n", option.c_str());
vars[option] = OptionVar(option.c_str(), value.c_str(), true);
}
}
else
{
//read in value
std::string value;
int pos = 0;
while (args[i][pos])
value += args[i][pos++];
this->commands.push_back(value);
}
}
}
};