forked from ifsmirnov/jngen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.h
409 lines (338 loc) · 9.83 KB
/
options.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
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
#pragma once
#include "common.h"
#include "range_option.h"
#include <map>
#include <sstream>
#include <string>
#include <vector>
namespace jngen {
namespace detail {
template<typename T>
constexpr bool isValidOptionType() {
return std::is_same<T, std::string>::value ||
(std::is_arithmetic<T>::value && !std::is_same<T, char>::value);
}
template<typename T>
using StringIfCharPtrElseT = typename std::conditional<
std::is_same<typename std::decay<T>::type, const char*>::value ||
std::is_same<typename std::decay<T>::type, char*>::value,
std::string,
T>::type;
} // namespace detail
struct Index {
size_t index;
std::string name;
Index(size_t index) : index(index) {
if (!config.largeOptionIndices) {
ensure(
index < 32,
"Looks like you called getOpt('c'). Consider using "
"getOpt(\"c\") or set 'config.largeOptionIndices = true' "
"if you indeed have more than 32 options.");
}
}
Index(const std::string& name) : name(name) {
ensure(!name.empty(), "Variable name cannot be empty");
}
bool isNamed() const {
return !name.empty();
}
};
struct VariableMap {
std::vector<std::string> positional;
std::map<std::string, std::string> named;
int count(size_t pos) const {
return pos < positional.size();
}
int count(const std::string& name) const {
return named.count(name);
}
std::string operator[](size_t pos) const {
if (!count(pos)) {
return "";
}
return positional.at(pos);
}
std::string operator[](const std::string& name) const {
if (!count(name)) {
return "";
}
return named.at(name);
}
int count(const Index& index) const {
if (index.isNamed()) {
return count(index.name);
} else {
return count(index.index);
}
}
std::string operator[](const Index& index) const {
if (index.isNamed()) {
return (*this)[index.name];
} else {
return (*this)[index.index];
}
}
void assertExistence(const Index& index) const {
if (count(index)) {
return;
}
if (index.isNamed()) {
ensure(false, format(
"There is no variable with name '%s'", index.name.c_str()));
} else {
ensure(false, format(
"There is no variable with index %d", index.index));
}
}
bool initialized = false;
};
template<typename T>
class PendingVariable {
public:
explicit PendingVariable(std::string value) :
value_(std::move(value))
{ }
PendingVariable(std::string value, T defaultValue) :
value_(std::move(value)),
default_(std::move(defaultValue))
{ }
explicit PendingVariable(std::nullptr_t, T defaultValue) :
valid_(false),
default_(std::move(defaultValue))
{ }
// We need this check in order to make the following work:
// string s = getOpt(0);
// s = getOpt(0);
// Weird things happen if we allow all kind of casts. See
// https://stackoverflow.com/questions/46740341
template<
typename U,
typename std::enable_if<
detail::isValidOptionType<U>()>::type* = nullptr>
operator U() const
{
if (!valid_) {
return static_cast<U>(default_);
}
std::istringstream ss(value_);
U t;
if (ss >> t) {
return t;
} else {
ensure(
false,
format(
"Cannot parse option. Raw value: '%s'",
value_.c_str()));
}
}
// TODO: getOpt operators, like getOpt("n") == 100
private:
bool valid_ = true;
std::string value_;
T default_;
};
template<>
class PendingVariable<void> {
public:
explicit PendingVariable(std::string value) :
value_(std::move(value))
{ }
// We need this check in order to make the following work:
// string s = getOpt(0);
// s = getOpt(0);
// Weird things happen if we allow all kind of casts. See
// https://stackoverflow.com/questions/46740341
template<
typename U,
typename std::enable_if<
detail::isValidOptionType<U>()>::type* = nullptr>
operator U() const
{
std::istringstream ss(value_);
U t;
if (ss >> t) {
return t;
} else {
ensure(
false,
format(
"Cannot parse option. Raw value: '%s'",
value_.c_str()));
}
}
private:
std::string value_;
};
// TODO: think about seed as a last argument
inline VariableMap parseArguments(const std::vector<std::string>& args) {
VariableMap result;
auto setNamedVar = [&result](
const std::string& name,
const std::string& value)
{
ensure(
!result.count(value),
"Named arguments must have distinct names");
result.named[name] = value;
};
std::string pendingVarName;
for (const std::string& s: args) {
if (s == "-") {
continue;
}
if (s == "--") {
break;
}
if (s[0] != '-') {
if (!pendingVarName.empty()) {
setNamedVar(pendingVarName, s);
pendingVarName = "";
} else {
result.positional.push_back(s);
}
continue;
}
if (!pendingVarName.empty()) {
result.named[pendingVarName] = "1";
pendingVarName = "";
}
std::string name;
std::string value;
bool foundEq = false;
for (char c: s.substr(1)) {
if (!foundEq && c == '=') {
foundEq = true;
} else {
if (foundEq) {
value += c;
} else {
name += c;
}
}
}
if (foundEq) {
setNamedVar(name, value);
} else {
pendingVarName = name;
}
setNamedVar(name, value);
}
if (!pendingVarName.empty()) {
result.named[pendingVarName] = "1";
}
result.initialized = true;
return result;
}
JNGEN_EXTERN VariableMap vmap;
namespace detail {
inline PendingVariable<void> getOpt(const Index& index) {
ensure(
vmap.initialized,
"parseArgs(args, argv) must be called before getOpt(...)");
vmap.assertExistence(index);
return PendingVariable<void>(vmap[index]);
}
template<typename T, typename U = detail::StringIfCharPtrElseT<T>>
PendingVariable<U> getOpt(const Index& index, const T& defaultValue) {
ensure(
vmap.initialized,
"parseArgs(args, argv) must be called before getOpt(...)");
if (vmap.count(index)) {
return PendingVariable<U>(vmap[index], U{defaultValue});
} else {
return PendingVariable<U>(nullptr, U{defaultValue});
}
}
inline bool hasOpt(const Index& index) {
return vmap.count(index);
}
template<typename T>
bool readVariable(const std::string& value, T& var) {
std::istringstream ss(value);
T t;
if (ss >> t) {
var = t;
return true;
}
return false;
}
inline int getNamedImpl(std::vector<std::string>::const_iterator) { return 0; }
template<typename T, typename ... Args>
int getNamedImpl(
std::vector<std::string>::const_iterator it, T& var, Args&... args)
{
T value;
int res = 0;
if (readVariable(vmap[*it], value)) {
var = value;
++res;
}
res += getNamedImpl(++it, args...);
return res;
}
inline int getPositionalImpl(size_t) { return 0; }
template<typename T, typename ... Args>
int getPositionalImpl(size_t index, T& var, Args&... args) {
T value;
int res = 0;
if (readVariable(vmap[index], value)) {
var = value;
++res;
}
res += getPositionalImpl(index + 1, args...);
return res;
}
} // namespace detail
template<typename ... Args>
int doGetNamed(const std::string& names, Args&... args) {
ensure(
vmap.initialized,
"parseArgs(args, argv) must be called before getNamed(...)");
auto namesSplit = util::split(names, ',');
ENSURE(
namesSplit.size() == sizeof...(args),
"Number of names is not equal to number of variables");
return detail::getNamedImpl(namesSplit.begin(), args...);
}
template<typename ... Args>
int getPositional(Args&... args) {
ensure(
vmap.initialized,
"parseArgs(args, argv) must be called before getPositional(...)");
return detail::getPositionalImpl(0, args...);
}
inline void parseArgs(int argc, char *argv[]) {
vmap = parseArguments(std::vector<std::string>(argv + 1, argv + argc));
}
inline PendingVariable<void> getOpt(size_t index) {
return detail::getOpt(Index(index));
}
inline PendingVariable<void> getOpt(const std::string& name) {
return detail::getOpt(Index(name));
}
template<typename T, typename U = detail::StringIfCharPtrElseT<T>>
PendingVariable<U> getOpt(size_t index, const T& defaultValue) {
return detail::getOpt(Index(index), defaultValue);
}
template<typename T, typename U = detail::StringIfCharPtrElseT<T>>
PendingVariable<U> getOpt(const std::string& name, const T& defaultValue) {
return detail::getOpt(Index(name), defaultValue);
}
inline bool hasOpt(size_t index) {
return vmap.count(index);
}
inline bool hasOpt(const std::string& name) {
return vmap.count(name);
}
inline options::Range parseRange(const std::string& value) {
return options::Range::fromString(value);
}
} // namespace jngen
using jngen::parseArgs;
using jngen::getOpt;
using jngen::hasOpt;
using jngen::parseRange;
using jngen::getPositional;
#define getNamed(...) ::jngen::doGetNamed(#__VA_ARGS__, __VA_ARGS__)