-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler_context.cpp
More file actions
385 lines (328 loc) · 11.8 KB
/
handler_context.cpp
File metadata and controls
385 lines (328 loc) · 11.8 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
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
#include "pch.hpp"
#include "handler_context.hpp"
#include "processor.hpp"
#include "option_error.hpp"
#include <be/core/exceptions.hpp>
namespace be::cli {
///////////////////////////////////////////////////////////////////////////////
const Processor& HandlerContext::processor() const {
return processor_;
}
///////////////////////////////////////////////////////////////////////////////
bool HandlerContext::enable_option_handling() const {
return enable_options_;
}
///////////////////////////////////////////////////////////////////////////////
void HandlerContext::enable_option_handling(bool enable_options) {
enable_options_ = enable_options;
}
///////////////////////////////////////////////////////////////////////////////
/// \brief The handling phase during which the handler was called.
HandlerContext::handling_phase HandlerContext::phase() const {
return phase_;
}
///////////////////////////////////////////////////////////////////////////////
/// \brief The raw position on the command line of the current argument being
/// processed.
///
/// \details The first argument has raw position 0.
std::size_t HandlerContext::raw_position() const {
return raw_position_;
}
///////////////////////////////////////////////////////////////////////////////
/// \brief The number of positional arguments that have been processed before
/// the current argument was parsed.
///
/// \details Call increment_position(true) from a handler to increment the
/// position index for future arguments. Note that the position is
/// never incremented more than once for the same argument, and the
/// increment operation does not occur until all handlers have been
/// run for the current argument.
std::size_t HandlerContext::position() const {
return position_;
}
///////////////////////////////////////////////////////////////////////////////
/// \brief The contents of the command line at the current raw_position().
const S& HandlerContext::argument() const {
return args_[raw_position_];
}
///////////////////////////////////////////////////////////////////////////////
const S& HandlerContext::option() const {
return option_info_.options.at(current_option_);
}
///////////////////////////////////////////////////////////////////////////////
bool HandlerContext::is_short_option() const {
return !option_info_.is_long_option;
}
///////////////////////////////////////////////////////////////////////////////
bool HandlerContext::is_long_option() const {
return option_info_.is_long_option;
}
///////////////////////////////////////////////////////////////////////////////
const S& HandlerContext::option_prefix() const {
return option_info_.prefix;
}
///////////////////////////////////////////////////////////////////////////////
const S& HandlerContext::option_suffix() const {
return option_info_.suffix;
}
///////////////////////////////////////////////////////////////////////////////
std::size_t HandlerContext::value_count() const {
if (!value_count_valid_) {
value_count_ = option_value_count();
for (std::size_t i = raw_position_ + 1, n = args_.size(); i < n; ++i) {
if (!(arg_consumption_[i] & consumed)) {
++value_count_;
}
}
value_count_valid_ = true;
}
return value_count_;
}
///////////////////////////////////////////////////////////////////////////////
std::size_t HandlerContext::option_value_count() const {
if (!option_value_count_valid_) {
option_value_count_ = 0;
for (auto state : option_value_consumption_) {
if (!(state & consumed)) {
++option_value_count_;
}
}
option_value_count_valid_ = true;
}
return option_value_count_;
}
///////////////////////////////////////////////////////////////////////////////
const S& HandlerContext::peek_value(std::size_t value_index) {
return consume_value_(value_index, unconsumed);
}
///////////////////////////////////////////////////////////////////////////////
const S& HandlerContext::consume_value(std::size_t value_index) {
return consume_value_(value_index, consume_after_handler);
}
///////////////////////////////////////////////////////////////////////////////
const S& HandlerContext::consume_value_after_option(std::size_t value_index) {
return consume_value_(value_index, consume_after_option);
}
///////////////////////////////////////////////////////////////////////////////
const S& HandlerContext::consume_value_after_phase(std::size_t value_index) {
return consume_value_(value_index, consume_after_phase);
}
///////////////////////////////////////////////////////////////////////////////
const S& HandlerContext::consume_value_after_argument(std::size_t value_index) {
return consume_value_(value_index, consume_after_argument);
}
///////////////////////////////////////////////////////////////////////////////
bool HandlerContext::is_option(std::size_t value_index) {
if (value_index < option_value_count()) {
return false;
} else {
auto results = processor_.option_parser_(peek_value(value_index));
return !results.options.empty();
}
}
///////////////////////////////////////////////////////////////////////////////
void HandlerContext::handled(bool is_handled) {
if (phase_ == option_phase) {
option_handled_ = is_handled;
} else {
arg_handled_ = is_handled;
}
}
///////////////////////////////////////////////////////////////////////////////
bool HandlerContext::handled() const {
if (phase_ == option_phase) {
return option_handled_;
} else {
return arg_handled_;
}
}
///////////////////////////////////////////////////////////////////////////////
void HandlerContext::stop(bool should_stop) {
stop_ = should_stop;
if (should_stop) {
handled(true);
}
}
///////////////////////////////////////////////////////////////////////////////
bool HandlerContext::stop() const {
return stop_;
}
///////////////////////////////////////////////////////////////////////////////
void HandlerContext::stop_after_phase(bool should_stop) {
stop_after_phase_ = should_stop;
if (should_stop) {
handled(true);
}
}
///////////////////////////////////////////////////////////////////////////////
bool HandlerContext::stop_after_phase() const {
return stop_after_phase_;
}
///////////////////////////////////////////////////////////////////////////////
void HandlerContext::increment_position(bool increment_position) {
increment_position_ = increment_position;
if (increment_position) {
handled(true);
}
}
///////////////////////////////////////////////////////////////////////////////
bool HandlerContext::increment_position() const {
return increment_position_;
}
///////////////////////////////////////////////////////////////////////////////
const S& HandlerContext::consume_value_(std::size_t value_index, consumed_state when) {
if (value_index < option_value_count()) {
for (std::size_t i = 0, n = option_info_.values.size(); i < n; ++i) {
if (!(option_value_consumption_[i] & consumed)) {
if (value_index == 0) {
option_value_consumption_[i] |= when;
return option_info_.values[i];
} else {
--value_index;
}
}
}
} else {
value_index -= option_value_count();
for (std::size_t i = raw_position_ + 1, n = args_.size(); i < n; ++i) {
if (!(arg_consumption_[i] & consumed)) {
if (value_index == 0) {
arg_consumption_[i] |= when;
return args_[i];
} else {
--value_index;
}
}
}
}
throw FatalTrace(std::make_error_code(std::errc::invalid_argument), "Invalid value index!");
}
///////////////////////////////////////////////////////////////////////////////
HandlerContext::HandlerContext(Processor& processor, const arg_sequence& args)
: processor_(processor),
args_(args),
enable_options_(true),
raw_position_(0),
position_(0),
value_count_(0),
value_count_valid_(false),
option_value_count_(0),
option_value_count_valid_(false),
phase_(raw_positional_phase),
arg_handled_(false),
option_handled_(false),
stop_(false),
stop_after_phase_(false),
increment_position_(false),
current_option_(0)
{
arg_consumption_.resize(args_.size(), unconsumed);
}
///////////////////////////////////////////////////////////////////////////////
bool HandlerContext::argument_(std::size_t raw_position) {
raw_position_ = raw_position;
arg_handled_ = false;
stop_ = false;
stop_after_phase_ = false;
increment_position_ = false;
return !(arg_consumption_[raw_position] & consumed);
}
///////////////////////////////////////////////////////////////////////////////
void HandlerContext::raw_phase_() {
phase_ = raw_positional_phase;
}
///////////////////////////////////////////////////////////////////////////////
void HandlerContext::option_phase_(OptionParseResult option_info) {
phase_ = option_phase;
option_info_ = std::move(option_info);
current_option_ = 0;
option_value_consumption_.clear();
option_value_consumption_.resize(option_info_.values.size(), unconsumed);
value_count_valid_ = false;
option_value_count_valid_ = false;
}
///////////////////////////////////////////////////////////////////////////////
void HandlerContext::option_(std::size_t current_option) {
current_option_ = current_option;
option_handled_ = false;
stop_ = false;
}
///////////////////////////////////////////////////////////////////////////////
void HandlerContext::positional_phase_() {
phase_ = positional_phase;
}
///////////////////////////////////////////////////////////////////////////////
void HandlerContext::clear_option_info_() {
option_info_.options.clear();
option_info_.values.clear();
option_value_consumption_.clear();
current_option_ = 0;
option_handled_ = false;
option_value_count_ = 0;
option_value_count_valid_ = true;
}
///////////////////////////////////////////////////////////////////////////////
void HandlerContext::after_handler_() {
for (auto& state : arg_consumption_) {
if (state & consume_after_handler) {
state = consumed;
value_count_valid_ = false;
}
}
for (auto& state : option_value_consumption_) {
if (state & consume_after_handler) {
state = consumed;
value_count_valid_ = false;
option_value_count_valid_ = false;
}
}
}
///////////////////////////////////////////////////////////////////////////////
void HandlerContext::after_option_() {
for (auto& state : arg_consumption_) {
if (state & consume_after_option) {
state = consumed;
value_count_valid_ = false;
}
}
for (auto& state : option_value_consumption_) {
if (state & consume_after_option) {
state = consumed;
value_count_valid_ = false;
option_value_count_valid_ = false;
}
}
if (!option_handled_) {
throw OptionError(*this, "Unrecognized option!");
}
arg_handled_ = true;
}
///////////////////////////////////////////////////////////////////////////////
/// \return true if subsequent phases should be skipped for this arg
bool HandlerContext::after_phase_() {
for (auto& state : arg_consumption_) {
if (state & consume_after_phase) {
state = consumed;
}
}
value_count_valid_ = false;
clear_option_info_();
return stop_after_phase_;
}
///////////////////////////////////////////////////////////////////////////////
void HandlerContext::after_argument_() {
for (auto& state : arg_consumption_) {
if (state & consume_after_argument) {
state = consumed;
}
}
value_count_valid_ = false;
if (increment_position_) {
++position_;
}
if (!arg_handled_) {
throw ArgumentError(*this, "Unhandled argument!");
}
}
} // be::cli