-
Notifications
You must be signed in to change notification settings - Fork 0
/
getopt.hpp
363 lines (330 loc) · 11.2 KB
/
getopt.hpp
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
/*
Copyright (c) 2023-2024 Oliver Lau <oliver.lau@gmail.com>
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the “Software”), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef __GETOPT_HPP__
#define __GETOPT_HPP__
#include <cstdlib>
#include <exception>
#include <functional>
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>
namespace argparser
{
class argument_required_exception : public std::exception
{
std::string exception_message;
public:
explicit argument_required_exception(std::string const &option_name)
{
exception_message = "Option `" + option_name + "` requires an argument, but none is given.";
}
const char *what() const throw()
{
return exception_message.c_str();
}
};
class unknown_option_exception : public std::exception
{
std::string exception_message;
public:
explicit unknown_option_exception(std::string const &option_name)
{
exception_message = "Option `" + option_name + "` is unknown.";
}
const char *what() const throw()
{
return exception_message.c_str();
}
};
class help_requested_exception : public std::exception
{
std::string exception_message{};
public:
explicit help_requested_exception(){};
const char *what() const throw()
{
return exception_message.c_str();
}
};
namespace util
{
template <class T>
inline void hash_combine(std::size_t &seed, const T &v)
{
std::hash<T> hasher;
seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
}
struct options_hash
{
std::size_t operator()(std::vector<std::string> const &options) const noexcept
{
std::size_t seed = 0;
for (std::string const &s : options)
{
hash_combine(seed, s);
}
return seed;
}
};
}
class argparser
{
public:
enum arg_type_t
{
invalid_argument,
no_argument,
required_argument,
optional_argument,
};
using callback_t = std::function<void(std::string const &)>;
struct arg_options
{
std::string help{};
std::string arg_name{};
callback_t handler{nullptr};
arg_type_t arg_type{invalid_argument};
operator bool() const
{
return handler != nullptr;
}
};
struct positional
{
std::string help{};
std::string arg_name{};
callback_t handler{nullptr};
};
argparser() = delete;
argparser(int argc, char *argv[])
{
const std::size_t n = static_cast<std::size_t>(argc);
args_.reserve(n);
for (std::size_t i = 1; i < n; ++i)
{
args_.push_back(argv[i]);
}
}
argparser ®(std::vector<std::string> const &options, std::string const &arg_name, arg_type_t arg_type, std::string const &help, callback_t handler)
{
options_.emplace(std::make_pair(
options,
arg_options{help, arg_name, handler, arg_type}));
return *this;
}
argparser ®(std::vector<std::string> const &options, arg_type_t arg_type, std::string const &help, callback_t handler)
{
return reg(options, std::string{}, arg_type, help, handler);
}
[[deprecated]]
argparser ®(std::vector<std::string> const &options, arg_type_t arg_type, callback_t handler)
{
return reg(options, std::string{}, arg_type, std::string{}, handler);
}
argparser &pos(std::string const &arg_name, std::string help, callback_t handler)
{
positionals_.emplace_back(positional{help,
arg_name,
handler});
return *this;
}
[[deprecated]]
argparser &pos(std::string const &arg_name, callback_t handler)
{
positionals_.emplace_back(positional{std::string{},
arg_name,
handler});
return *this;
}
void display_help(void)
{
std::cout << info_text_ << "\n"
<< "Usage:\n\n"
<< " " << name_ << ' ';
if (!options_.empty())
{
std::cout << "[OPTIONS]";
if (!positionals_.empty())
{
std::cout << ' ';
}
}
if (!positionals_.empty())
{
bool has_help = false;
for (auto pos = std::begin(positionals_); pos != std::end(positionals_); ++pos)
{
std::cout << pos->arg_name;
if (std::next(pos) != std::end(positionals_))
{
std::cout << ' ';
}
has_help |= !pos->help.empty();
}
if (has_help)
{
std::cout << "\n\nArguments:\n\n";
for (positional const &pos : positionals_)
{
std::cout << " " << pos.arg_name << "\n\n"
<< " " << pos.help << "\n";
}
}
}
std::cout << "\n\nOptions:\n\n";
for (auto const &[switches, options] : options_)
{
std::cout << " ";
for (auto opt = std::begin(switches); opt != std::end(switches); ++opt)
{
switch (options.arg_type)
{
case no_argument:
std::cout << *opt;
break;
case required_argument:
std::cout << *opt << ' ' << options.arg_name;
break;
case optional_argument:
std::cout << *opt << " [" << options.arg_name << "]";
break;
default:
break;
}
if (std::next(opt) != std::end(switches))
{
std::cout << ", ";
}
}
if (!options.help.empty())
{
std::cout << "\n\n " << options.help;
}
std::cout << "\n\n";
}
std::cout << std::endl;
throw help_requested_exception();
}
argparser &help(std::vector<std::string> const &options, std::string const &help)
{
options_.emplace(std::make_pair(
options,
arg_options{help, std::string{}, std::bind(&argparser::display_help, this), no_argument}));
return *this;
}
argparser &info(std::string const &text, std::string const &name)
{
info_text_ = text;
name_ = name;
return *this;
}
arg_options find(std::string const &opt)
{
for (auto const &[options, option_options] : options_)
{
for (auto const &option : options)
{
if (option == opt)
{
return option_options;
}
}
}
return arg_options{};
}
/**
* @brief Parse command-line arguments.
*
* @return true Successfully parsed.
* @return false Errors occurred.
*/
bool operator()()
{
current_positional_ = positionals_.begin();
auto arg = args_.begin();
while (arg != args_.end())
{
arg_options opt = find(*arg);
if (opt)
{
switch (opt.arg_type)
{
case no_argument:
opt.handler(std::string());
break;
case required_argument:
{
if (std::next(arg) != std::end(args_))
{
opt.handler(*(++arg));
}
else
{
throw argument_required_exception(*arg);
}
break;
}
case optional_argument:
{
if (std::next(arg) != std::end(args_) && find(*std::next(arg)))
{
opt.handler(*(++arg));
}
else
{
opt.handler(std::string{});
}
break;
}
case invalid_argument:
[[fallthrough]];
default:
break;
}
}
else
{
if (current_positional_ != std::end(positionals_))
{
(current_positional_->handler)(*arg);
++current_positional_;
}
else
{
throw unknown_option_exception(*arg);
}
}
++arg;
}
return true;
}
private:
std::vector<std::string> args_;
std::vector<positional> positionals_;
std::vector<positional>::const_iterator current_positional_;
std::unordered_map<std::vector<std::string>, arg_options, util::options_hash> options_;
std::string info_text_;
std::string name_;
};
}
#endif // __GETOPT_HPP__