-
-
Notifications
You must be signed in to change notification settings - Fork 56
/
opt.hpp
373 lines (314 loc) · 9.4 KB
/
opt.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
364
365
366
367
368
369
370
371
372
373
// Copyright 2018-2022 René Ferdinand Rivera Morell
// Copyright 2017 Two Blue Cubes Ltd. All rights reserved.
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#ifndef LYRA_OPT_HPP
#define LYRA_OPT_HPP
#include "lyra/detail/print.hpp"
#include "lyra/detail/trait_utils.hpp"
#include "lyra/parser.hpp"
#include "lyra/val.hpp"
#include <memory>
#include <string>
namespace lyra {
/* tag::reference[]
[#lyra_opt]
= `lyra::opt`
A parser for one option with multiple possible names. The option value(s) are
communicated through a reference to a variable, a container, or a callback.
Is-a <<lyra_bound_parser>>.
end::reference[] */
class opt : public bound_parser<opt>
{
public:
enum class ctor_lambda_e
{
val
};
enum class ctor_ref_e
{
val
};
// Flag option ctors..
explicit opt(bool & ref);
template <typename L>
explicit opt(L const & ref,
typename std::enable_if<detail::is_invocable<L>::value,
ctor_lambda_e>::type
= ctor_lambda_e::val);
// Value option ctors..
template <typename T>
opt(T & ref,
std::string const & hint,
typename std::enable_if<!detail::is_invocable<T>::value,
ctor_ref_e>::type
= ctor_ref_e::val);
template <typename L>
opt(L const & ref,
std::string const & hint,
typename std::enable_if<detail::is_invocable<L>::value,
ctor_lambda_e>::type
= ctor_lambda_e::val);
// Bound value ctors..
template <typename T>
explicit opt(detail::BoundVal<T> && val)
: bound_parser(val.move_to_shared())
{}
template <typename T>
explicit opt(detail::BoundVal<T> && val, std::string const & hint)
: bound_parser(val.move_to_shared(), hint)
{}
// Option specifications..
opt & name(const std::string & opt_name);
opt & operator[](std::string const & opt_name);
// Internal..
virtual std::string get_usage_text(
const option_style & style) const override
{
std::string usage;
for (std::size_t o = 0; o < opt_names.size(); ++o)
{
if (o > 0) usage += "|";
usage += format_opt(opt_names[o], style);
}
if (!m_hint.empty()) usage += " <" + m_hint + ">";
return usage;
}
virtual help_text get_help_text(const option_style & style) const override
{
std::string text;
for (auto const & opt_name : opt_names)
{
if (!text.empty()) text += ", ";
text += format_opt(opt_name, style);
}
if (!m_hint.empty()) ((text += " <") += m_hint) += ">";
return { { text, m_description } };
}
virtual bool is_named(const std::string & n) const override
{
if (bound_parser::is_named(n)) return true;
for (auto & name : opt_names)
if (n == name) return true;
return false;
}
using parser::parse;
parse_result parse(detail::token_iterator const & tokens,
const option_style & style) const override
{
LYRA_PRINT_SCOPE("opt::parse");
auto validationResult = validate();
if (!validationResult) return parse_result(validationResult);
auto remainingTokens = tokens;
if (remainingTokens && remainingTokens.has_option_prefix())
{
auto const & token = remainingTokens.option();
if (is_match(token.name, style))
{
if (m_ref->isFlag())
{
if (remainingTokens.has_value_delimiter())
{
return parse_result::error(
{ parser_result_type::short_circuit_all,
remainingTokens },
"Flag option '" + token.name + "' contains value '"
+ remainingTokens.value().name + "'.");
}
remainingTokens.pop(token);
auto flagRef
= static_cast<detail::BoundFlagRefBase *>(m_ref.get());
auto flag_result = flagRef->setFlag(true);
if (!flag_result) return parse_result(flag_result);
LYRA_PRINT_DEBUG(
"(=)", get_usage_text(style), "==", token.name);
if (flag_result.value()
== parser_result_type::short_circuit_all)
return parse_result::ok(detail::parse_state(
flag_result.value(), remainingTokens));
}
else
{
auto const & argToken = remainingTokens.value();
if (argToken.type == detail::token_type::unknown)
return parse_result::error(
{ parser_result_type::no_match, remainingTokens },
"Expected argument following " + token.name);
remainingTokens.pop(token, argToken);
auto valueRef
= static_cast<detail::BoundValueRefBase *>(m_ref.get());
if (value_choices)
{
auto choice_result
= value_choices->contains_value(argToken.name);
if (!choice_result) return parse_result(choice_result);
}
auto v_result = valueRef->setValue(argToken.name);
if (!v_result)
{
// Matched the option, but not the value. This is a
// hard fail that needs to skip subsequent parsing.
return parse_result::error(
{ parser_result_type::short_circuit_all,
remainingTokens },
v_result.message());
}
LYRA_PRINT_DEBUG("(=)", get_usage_text(style),
"==", token.name, argToken.name);
if (v_result.value()
== parser_result_type::short_circuit_all)
return parse_result::ok(detail::parse_state(
v_result.value(), remainingTokens));
}
return parse_result::ok(detail::parse_state(
parser_result_type::matched, remainingTokens));
}
LYRA_PRINT_DEBUG("(!)", get_usage_text(style), "!= ", token.name);
}
else
{
LYRA_PRINT_DEBUG("(!)", get_usage_text(style),
"!=", remainingTokens.argument().name);
}
return parse_result::ok(
detail::parse_state(parser_result_type::no_match, remainingTokens));
}
result validate() const override
{
if (opt_names.empty())
return result::error("No options supplied to opt");
if (m_ref->isFlag() && value_choices)
return result::error("Flag options cannot contain choices.");
for (auto const & name : opt_names)
{
if (name.empty())
return result::error("Option name cannot be empty");
if (name[0] != '-')
return result::error("Option name must begin with '-'");
}
return bound_parser::validate();
}
virtual std::unique_ptr<parser> clone() const override
{
return make_clone<opt>(this);
}
protected:
std::vector<std::string> opt_names;
bool is_match(
std::string const & opt_name, const option_style & style) const
{
auto opt_normalized = normalise_opt(opt_name, style);
for (auto const & name : opt_names)
{
if (normalise_opt(name, style) == opt_normalized) return true;
}
return false;
}
std::string normalise_opt(
std::string const & opt_name, const option_style & style) const
{
if (detail::token_iterator::is_prefixed(
style.short_option_prefix, style.short_option_size, opt_name))
return std::string("-") + opt_name.substr(style.short_option_size);
if (detail::token_iterator::is_prefixed(
style.long_option_prefix, style.long_option_size, opt_name))
return std::string("--") + opt_name.substr(style.long_option_size);
return opt_name;
}
std::string format_opt(
std::string const & opt_name, const option_style & style) const
{
if (opt_name[0] == '-' && opt_name[1] == '-')
return style.long_option_string() + opt_name.substr(2);
else if (opt_name[0] == '-')
return style.short_option_string() + opt_name.substr(1);
else
return opt_name;
}
};
/* tag::reference[]
[#lyra_opt_ctor]
== Construction
end::reference[] */
/* tag::reference[]
[#lyra_opt_ctor_flags]
=== Flags
[source]
----
lyra::opt::opt(bool& ref);
template <typename L>
lyra::opt::opt(L const& ref);
----
Constructs a flag option with a target `bool` to indicate if the flag is
present. The first form takes a reference to a variable to receive the
`bool`. The second takes a callback that is called with `true` when the
option is present.
end::reference[] */
inline opt::opt(bool & ref)
: bound_parser(std::make_shared<detail::BoundFlagRef>(ref))
{}
template <typename L>
opt::opt(L const & ref,
typename std::enable_if<detail::is_invocable<L>::value,
opt::ctor_lambda_e>::type)
: bound_parser(std::make_shared<detail::BoundFlagLambda<L>>(ref))
{}
/* tag::reference[]
[#lyra_opt_ctor_values]
=== Values
[source]
----
template <typename T>
lyra::opt::opt(T& ref, std::string const& hint);
template <typename L>
lyra::opt::opt(L const& ref, std::string const& hint)
----
Constructs a value option with a target `ref`. The first form takes a reference
to a variable to receive the value. The second takes a callback that is called
with the value when the option is present.
end::reference[] */
template <typename T>
opt::opt(T & ref,
std::string const & hint,
typename std::enable_if<!detail::is_invocable<T>::value,
opt::ctor_ref_e>::type)
: bound_parser(ref, hint)
{}
template <typename L>
opt::opt(L const & ref,
std::string const & hint,
typename std::enable_if<detail::is_invocable<L>::value,
opt::ctor_lambda_e>::type)
: bound_parser(ref, hint)
{}
/* tag::reference[]
[#lyra_opt_specification]
== Specification
end::reference[] */
/* tag::reference[]
[#lyra_opt_name]
=== `lyra::opt::name`
[source]
----
lyra::opt& lyra::opt::name(const std::string &opt_name)
lyra::opt& lyra::opt::operator[](const std::string &opt_name)
----
Add a spelling for the option of the form `--<name>` or `-n`.
One can add multiple short spellings at once with `-abc`.
end::reference[] */
inline opt & opt::name(const std::string & opt_name)
{
if (opt_name.size() > 2 && opt_name[0] == '-' && opt_name[1] != '-')
for (auto o : opt_name.substr(1))
opt_names.push_back(std::string(1, opt_name[0]) + o);
else
opt_names.push_back(opt_name);
return *this;
}
inline opt & opt::operator[](const std::string & opt_name)
{
return this->name(opt_name);
}
} // namespace lyra
#endif