-
Notifications
You must be signed in to change notification settings - Fork 0
/
OptionParser.cpp
361 lines (299 loc) · 7.44 KB
/
OptionParser.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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#include "OptionParser.h"
//using std:cout;
//using std::endl;
using std::string;
#include <iostream>
using namespace std;
NamePair::NamePair(char _Short)
{
Short = _Short;
Long = "";
}
NamePair::NamePair(const std::string &_Long)
{
Guarantee(_Long.size() > 1, "Long name must have size > 1.");
Short = 0;
Long = _Long;
}
NamePair::NamePair(const std::string &_Long, char _Short)
{
Guarantee(_Long.size() > 1, "Long name must have size > 1.");
Short = _Short;
Long = _Long;
}
int OptionParser::flag_id(const std::string &flag)
{
Guarantee(flag.size(), "Empty name!");
if (flag.size() > 1)
return _long_flag_ids[flag];
else
return _short_flag_ids[flag[0]];
}
int OptionParser::option_id(const std::string &option)
{
Guarantee(option.size(), "Empty name!");
if (option.size() > 1)
return _long_option_ids[option];
else
return _short_option_ids[option[0]];
}
bool OptionParser::set_flag_value(const std::string &flag, bool value)
{
int id = flag_id(flag);
if (! id)
return false;
_flag_values[id] = value;
return true;
}
bool OptionParser::set_option_value(const std::string &option, const std::string &value)
{
int id = option_id(option);
if (! id)
return false;
_option_values[id] = value;
return true;
}
template <class T>
bool has(vector<T> &vec, T value)
{
for (T v : vec)
if (value == v)
return true;
return false;
}
// Valid for small vectors (int has small range). Returns -1 if it failed.
template <class T>
int index(vector<T> &vec, T value)
{
for (int i = 0; i < vec.size(); ++i)
{
if (value == vec.at(i))
return i;
}
return -1;
}
bool OptionParser::has_flag(char Short)
{
for (NamePair &name : _flag_names)
{
if (name.Short == Short)
return true;
}
return false;
}
bool OptionParser::has_flag(const std::string &Long)
{
Guarantee(Long.size(), "Empty name!");
// If needed convert to short name.
if (Long.size() == 1)
return has_flag(Long[0]);
// Valid long name.
for (NamePair &name : _flag_names)
{
if (name.Long == Long)
return true;
}
return false;
}
bool OptionParser::has_option(char Short)
{
for (NamePair &name : _option_names)
{
if (name.Short == Short)
return true;
}
return false;
}
bool OptionParser::has_option(const std::string &Long)
{
Guarantee(Long.size(),"Empty name!");
// If needed convert to short name.
if (Long.size() == 1)
return has_option(Long[0]);
// Valid long name.
for (NamePair &name : _option_names)
{
if (name.Long == Long)
return true;
}
return false;
}
int OptionParser::parse(int argc, char **argv)
{
int i = 1; // Skip the program name.
while (i < argc)
{
std::string arg(argv[i]);
if (arg.substr(0,2) == "--" || arg.substr(0,1) == "-") // Flag / Option
{
if (arg.substr(0,2) == "--") // Long flag/option
arg = arg.substr(2);
else // Short flag/option
arg = arg.substr(1);
int pos;
if ( has_flag(arg) ) // Flag
set_flag_value(arg, true);
else if ( has_option(arg) ) // Option
{
Guarantee(i+1 < argc, "Last option %s wasn't given an argument.", arg.c_str());
set_option_value(arg, argv[i+1]);
// We already parsed the option argument. Skip argv[i+1].
++i;
}
else
Guarantee(0, "Used unknown option %s\n", arg.c_str());
}
else // Standard argument (neither flag or option)
return i;
++i;
}
return argc;
}
void OptionParser::addUsage(const std::string &usage)
{
_usage = usage;
}
void OptionParser::printUsage()
{
cout << _usage;
}
bool OptionParser::valid(char Short)
{
for (NamePair &name : _flag_names)
if (name.Short == Short)
return false;
for (NamePair &name : _option_names)
if (name.Short == Short)
return false;
return true;
}
bool OptionParser::valid(const std::string &Long)
{
for (NamePair &name : _flag_names)
if (name.Long == Long)
return false;
for (NamePair &name : _option_names)
if (name.Long == Long)
return false;
return true;
}
void OptionParser::setFlag(char flag)
{
Guarantee(valid(flag), "%c was already used.", flag);
_flag_names.push_back( NamePair(flag) );
_short_flag_ids[flag] = _ids++;
}
void OptionParser::setFlag(const std::string &flag)
{
if (flag.size() == 1)
{
setFlag(flag[0]);
return;
}
Guarantee(valid(flag), "%s was already used.", flag.c_str());
_flag_names.push_back( NamePair(flag) );
_long_flag_ids[flag] = _ids++;
}
void OptionParser::setFlag(const std::string &long_flag, char short_flag)
{
Guarantee(valid(long_flag) && valid(short_flag), "%s or %c was already used.", long_flag.c_str(), short_flag);
_flag_names.push_back( NamePair(long_flag,short_flag) );
_long_flag_ids[long_flag] = _short_flag_ids[short_flag] = _ids++;
}
void OptionParser::setOption(char option)
{
Guarantee(valid(option), "%c was already used.", option);
_option_names.push_back( NamePair(option) );
_short_option_ids[option] = _ids++;
}
void OptionParser::setOption(const std::string &option)
{
if (option.size() == 1)
{
setOption(option);
return;
}
Guarantee(valid(option), "%s was already used.", option.c_str());
_option_names.push_back( NamePair(option) );
_long_option_ids[option] = _ids++;
}
void OptionParser::setOption(const std::string &long_option, char short_option)
{
Guarantee(valid(long_option) && valid(short_option), "%s or %c was already used.", long_option.c_str(), short_option);
_option_names.push_back( NamePair(long_option,short_option) );
_long_option_ids[long_option] = _short_option_ids[short_option] = _ids++;
}
bool OptionParser::getFlag(char short_flag)
{
int id = _short_flag_ids[short_flag];
return _flag_values[ id ];
}
bool OptionParser::getFlag(const std::string &long_flag)
{
if (long_flag.size() == 1)
return getFlag(long_flag[0]);
return _flag_values[ _long_flag_ids[long_flag] ];
}
std::string OptionParser::getOption(char short_option)
{
return _option_values[ _short_option_ids[short_option] ];
}
std::string OptionParser::getOption(const std::string &long_option)
{
if (long_option.size() == 1)
return getOption(long_option[0]);
return _option_values[ _long_option_ids[long_option] ];
}
bool OptionParser::Flag(char short_flag)
{
return _flag_values[ _short_flag_ids[short_flag] ];
}
bool OptionParser::Flag(const std::string &long_flag)
{
if (long_flag.size() == 1)
return Flag(long_flag[0]);
return _flag_values[ _long_flag_ids[long_flag] ];
}
bool OptionParser::Option(char short_option)
{
return _option_values[ _short_option_ids[short_option] ] != "";
}
bool OptionParser::Option(const std::string &long_option)
{
if (long_option.size() == 1)
return Option(long_option[0]);
return _option_values[ _long_option_ids[long_option] ] != "";
}
void OptionParser::print()
{
puts("");
if (_flag_names.size())
{
printf("Flags: ");
for (auto &f: _flag_names)
{
int id;
if (f.Short)
id = _short_flag_ids[f.Short];
else
id = _long_flag_ids[f.Long];
printf("[%s](%c)=%d ", f.Long.c_str(), f.Short, _flag_values[id]);
puts("");
}
if (_option_names.size())
{
printf("Options: ");
for (auto &f: _option_names)
{
int id;
if (f.Short)
id = _short_option_ids[f.Short];
else
id = _long_option_ids[f.Long];
printf("[%s](%c)=<%s> ", f.Long.c_str(), f.Short, _option_values[id].c_str());
}
puts("");
}
puts("");
}
}