-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathargparser.tpp
More file actions
349 lines (291 loc) · 7.64 KB
/
argparser.tpp
File metadata and controls
349 lines (291 loc) · 7.64 KB
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
#ifndef ARGPARSER_ARGPARSER_TPP_
#define ARGPARSER_ARGPARSER_TPP_
#include "argparser.hpp"
namespace argp
{
inline split_options::split_options(const OptionsList &options)
{
for (auto opt : options)
{
switch (opt->get_type())
{
case split_options::Type::POSITIONAL:
this->positional.push_back(
static_cast<PositionalOptionBase *>(opt));
break;
case split_options::Type::KEYWORD:
this->keyword.push_back(static_cast<KeywordOptionBase *>(opt));
break;
}
}
}
inline OptionBase::OptionBase() : is_set_(false) {}
inline void OptionBase::parse(const std::vector<std::string_view> &strings)
{
this->from_string(strings);
is_set_ = true;
}
inline bool OptionBase::is_set() const { return is_set_; }
inline PositionalOptionBase::PositionalOptionBase(std::string name,
std::string help,
bool is_required)
: name(name), help(help), is_required(is_required)
{
}
inline std::pair<std::string, std::string> PositionalOptionBase::get_help()
const
{
return {(this->is_required) ? this->name : "[" + this->name + "]",
this->help};
}
inline split_options::Type PositionalOptionBase::get_type() const
{
return split_options::Type::POSITIONAL;
}
inline KeywordOptionBase::KeywordOptionBase(
std::vector<std::string> identifiers, std::string help)
: identifiers(identifiers), help(help)
{
}
inline std::pair<std::string, std::string> KeywordOptionBase::get_help() const
{
int length = std::accumulate(
this->identifiers.begin(), this->identifiers.end(), 0,
[](int length, std::string str) { return length + str.length() + 2; });
std::string tmp;
tmp.reserve(length);
bool is_first = true;
for (auto str : this->identifiers)
{
if (is_first)
{
tmp += str;
is_first = false;
}
else
{
tmp += ", ";
tmp += str;
}
}
return {std::move(tmp), this->help};
}
inline split_options::Type KeywordOptionBase::get_type() const
{
return split_options::Type::KEYWORD;
}
template <class T>
inline void PositionalOption<T>::from_string(
const std::vector<std::string_view> &strings)
{
std::string str = std::string(strings[0]);
std::istringstream iss(str);
iss >> this->val;
if (!iss)
{
throw std::invalid_argument("Could not parse the data.");
}
}
template <>
inline void PositionalOption<std::string>::from_string(
const std::vector<std::string_view> &strings)
{
this->val = std::string(strings[0]);
}
template <class T>
inline PositionalOption<T>::PositionalOption(std::string name, std::string help,
bool is_required,
T val /* = T() */)
: PositionalOptionBase(name, help, is_required), val(val)
{
}
template <class T>
inline int PositionalOption<T>::get_param_count()
{
return 0;
}
template <class T>
inline bool PositionalOption<T>::matches(std::string_view)
{
return !this->is_set();
}
template <class T>
inline T PositionalOption<T>::get_val() const
{
return this->val;
}
template <class T>
inline constexpr const int KeywordOption<T>::NUM_OPTS = 1;
template <>
inline constexpr const int KeywordOption<bool>::NUM_OPTS = 0;
template <class T>
inline void KeywordOption<T>::from_string(
const std::vector<std::string_view> &strings)
{
std::string str = std::string(strings[1]);
std::istringstream iss(str);
iss >> this->val;
if (!iss)
{
throw std::invalid_argument("Could not parse the data.");
}
}
template <>
inline void KeywordOption<std::string>::from_string(
const std::vector<std::string_view> &strings)
{
this->val = std::string(strings[1]);
}
template <>
inline void KeywordOption<bool>::from_string(
const std::vector<std::string_view> &)
{
this->val = true;
}
template <class T>
inline KeywordOption<T>::KeywordOption(std::vector<std::string> identifiers,
std::string help, T val /* = T() */)
: KeywordOptionBase(identifiers, help), val(val)
{
}
template <class T>
inline int KeywordOption<T>::get_param_count()
{
return NUM_OPTS;
}
template <class T>
inline bool KeywordOption<T>::matches(std::string_view identifier)
{
for (auto id : this->identifiers)
{
if (id == identifier)
{
return true;
}
}
return false;
}
template <typename T>
inline T KeywordOption<T>::get_val() const
{
return val;
}
namespace impl
{
inline OptionBase *match_option(const char *arg, const OptionsList &opts)
{
auto split_opts = split_options(opts);
for (auto opt : split_opts.keyword)
{
if (opt->matches(arg))
{
return opt;
}
}
for (auto opt : split_opts.positional)
{
if (opt->matches(arg))
{
return opt;
}
}
return nullptr;
}
inline void handle_match(int &i, OptionBase *opt, int argc, const char *argv[])
{
int param_count = opt->get_param_count();
if (param_count < -1)
{
throw std::invalid_argument("Invalid number of requested parameters.");
}
if (param_count == -1)
{
param_count = argc - i - 1;
}
if (i + param_count >= argc)
{
throw std::out_of_range("Not enough arguments.");
}
std::vector<std::string_view> opts;
opts.reserve(param_count + 1);
for (int end = i + param_count + 1; i < end; i++)
{
opts.push_back(std::string_view(argv[i]));
}
--i;
opt->parse(opts);
}
inline indented::indented(std::string_view str, size_t width,
char fill /* = ' ' */)
: str(str), width(width), fill(fill)
{
}
inline std::ostream &operator<<(std::ostream &os, const indented &val)
{
for (auto c : val.str)
{
os << c;
if (c == '\n' || c == '\r')
{
for (size_t i = 0; i < val.width; i++)
{
os << val.fill;
}
}
}
return os;
}
} // namespace impl
inline std::vector<std::string> parse(int argc, const char *argv[],
const OptionsList &opts,
int skip_first_n /* = 1 */)
{
std::vector<std::string> unrecognised;
for (int i = skip_first_n; i < argc; i++)
{
OptionBase *opt = impl::match_option(argv[i], opts);
if (opt != nullptr)
{
impl::handle_match(i, opt, argc, argv);
}
else
{
unrecognised.push_back(argv[i]);
}
continue;
}
return unrecognised;
}
inline void print_help(std::ostream &os, std::string_view cmd,
const OptionsList &opts, size_t min_w /* = 25 */)
{
auto split_opts = split_options(opts);
auto flags = os.flags();
os << std::left;
os << "Usage: " << cmd;
if (split_opts.keyword.size() > 0)
{
os << " <options>";
}
for (auto pos_opt : split_opts.positional)
{
os << " " << pos_opt->get_help().first;
}
os << "\nOptions:\n";
for (OptionBase *opt : opts)
{
std::pair<std::string, std::string> &&help = opt->get_help();
if (help.first.length() <= min_w - 2)
{
os << std::setw(min_w - 2) << help.first << " ";
}
else
{
os << help.first << "\n" << std::setw(min_w) << "";
}
os << impl::indented(help.second, min_w) << "\n";
}
os.flags(flags);
}
} // namespace argp
#endif // ARGPARSER_ARGPARSER_TPP_