Repository navigation
Expand file tree
/
Copy pathtest_usage.cpp
More file actions
432 lines (382 loc) · 16.4 KB
/
Copy pathtest_usage.cpp
File metadata and controls
432 lines (382 loc) · 16.4 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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
#include <gtest/gtest.h>
#include <map>
#include <optional>
#include <rfl.hpp>
#include <rfl/cli.hpp>
#include <string>
#include <vector>
namespace test_usage {
enum class LogLevel { debug, info, warn };
struct Database {
std::string host;
int port;
};
struct Config {
rfl::Positional<std::string> input_file;
std::string host_name;
bool verbose;
std::vector<std::string> tags;
rfl::Short<"p", int> port;
Database database;
std::optional<LogLevel> log_level;
};
TEST(cli, test_usage_synopsis) {
// The exact string proposed in
// https://github.com/munich-data-engineering/reflect-cpp/issues/692.
EXPECT_EQ(rfl::cli::usage<Config>("my_binary").value(),
"Usage: my_binary <input-file> --host-name=<value> "
"--verbose[=<bool>] --tags=<v1,v2,...> -p|--port=<value> "
"--database.host=<value> --database.port=<value> "
"[--log-level=<debug|info|warn>]\n");
}
// The issue's second example: rfl::Positional and rfl::Short inside
// rfl::Description, which the parser finds, and so does the help.
struct Described {
rfl::Description<"Path to the input file", rfl::Positional<std::string>>
input_file;
rfl::Description<"Host to connect to", std::string> host_name;
bool verbose;
rfl::Description<"Port to connect on", rfl::Short<"p", int>> port;
rfl::Description<"Minimum level to log", std::optional<LogLevel>> log_level;
};
TEST(cli, test_usage_detailed) {
EXPECT_EQ(
rfl::cli::usage<Described>("my_binary", true).value(),
"Usage: my_binary <input-file> --host-name=<value> --verbose[=<bool>] "
"-p|--port=<value> [--log-level=<debug|info|warn>]\n"
"\n"
"Arguments:\n"
" <input-file> Path to the input file. Required.\n"
" --host-name=<value> Host to connect to. Required.\n"
" --verbose[=<bool>] Required.\n"
" -p, --port=<value> Port to connect on. Required.\n"
" --log-level=<debug|info|warn> Minimum level to log\n");
// What the help advertises is what the parser accepts.
const char* args[] = {"my_binary", "in.txt", "--host-name=localhost",
"--verbose", "-p", "8080",
"--log-level=warn"};
const auto result =
rfl::cli::read<Described>(7, const_cast<char**>(args));
ASSERT_TRUE(result) << result.error().what();
EXPECT_EQ(result.value().input_file().value(), "in.txt");
EXPECT_EQ(result.value().port().value(), 8080);
EXPECT_EQ(result.value().log_level.get(), LogLevel::warn);
// A plain bool is required, as the help says.
const char* no_verbose[] = {"my_binary", "in.txt", "--host-name=localhost",
"-p", "8080"};
EXPECT_FALSE(rfl::cli::read<Described>(5, const_cast<char**>(no_verbose)));
}
struct Validated {
rfl::Description<"Port to listen on",
rfl::Validator<int, rfl::Minimum<1>, rfl::Maximum<65535>>>
port;
rfl::Validator<double, rfl::ExclusiveMinimum<0>, rfl::Maximum<1>> ratio;
rfl::Validator<int, rfl::ExclusiveMaximum<100>> percent;
rfl::Validator<int, rfl::AnyOf<rfl::Maximum<0>, rfl::Minimum<10>>> outside;
rfl::Validator<int, rfl::OneOf<rfl::EqualTo<1>, rfl::EqualTo<2>>> one_or_two;
rfl::Validator<int, rfl::NotEqualTo<0>> nonzero;
rfl::Validator<std::vector<std::string>, rfl::Size<rfl::Minimum<1>>> tags;
rfl::Validator<std::vector<int>,
rfl::Size<rfl::AllOf<rfl::Minimum<1>, rfl::Maximum<3>>>>
ids;
rfl::Validator<std::string, rfl::Size<rfl::Minimum<1>>,
rfl::PatternValidator<"^[a-z]+$", "Lowercase">>
user;
rfl::Literal<"fast", "slow"> mode;
};
TEST(cli, test_usage_validators) {
EXPECT_EQ(rfl::cli::usage<Validated>("app", true).value(),
"Usage: app --port=<value> --ratio=<value> --percent=<value> "
"--outside=<value> --one-or-two=<value> --nonzero=<value> "
"--tags=<v1,v2,...> --ids=<v1,v2,...> --user=<value> "
"--mode=<fast|slow>\n"
"\n"
"Arguments:\n"
" --port=<value> Port to listen on. Range: 1..65535. "
"Required.\n"
" --ratio=<value> Range: > 0 and <= 1. Required.\n"
" --percent=<value> Range: < 100. Required.\n"
" --outside=<value> Range: <= 0 or >= 10. Required.\n"
" --one-or-two=<value> Range: 1 or 2. Required.\n"
" --nonzero=<value> Range: != 0. Required.\n"
" --tags=<v1,v2,...> Size: >= 1. Can be repeated. Required.\n"
" --ids=<v1,v2,...> Size: 1..3. Can be repeated. Required.\n"
" --user=<value> Size: >= 1. Pattern: ^[a-z]+$. "
"Required.\n"
" --mode=<fast|slow> Required.\n");
}
struct WithDefaultVal {
rfl::DefaultVal<int> port = 8080;
rfl::DefaultVal<std::string> host = "localhost";
rfl::DefaultVal<std::vector<std::string>> tags =
std::vector<std::string>{"a", "b"};
rfl::DefaultVal<LogLevel> level = LogLevel::info;
rfl::DefaultVal<double> rate = 0.25;
rfl::DefaultVal<bool> dry_run = false;
int count = 3;
rfl::Short<"r", rfl::DefaultVal<int>> retries = 5;
};
TEST(cli, test_usage_default_val) {
// Without rfl::DefaultIfMissing, `count` is required, so its member
// initializer is never used and not shown. A DefaultVal inside rfl::Short
// makes `retries` optional, as for the parser.
EXPECT_EQ(rfl::cli::usage<WithDefaultVal>("app", true).value(),
"Usage: app [--port=<value>] [--host=<value>] "
"[--tags=<v1,v2,...>] [--level=<debug|info|warn>] "
"[--rate=<value>] [--dry-run[=<bool>]] --count=<value> "
"[-r|--retries=<value>]\n"
"\n"
"Arguments:\n"
" --port=<value> Default: 8080.\n"
" --host=<value> Default: localhost.\n"
" --tags=<v1,v2,...> Default: a,b. Can be repeated.\n"
" --level=<debug|info|warn> Default: info.\n"
" --rate=<value> Default: 0.25.\n"
" --dry-run[=<bool>] Default: false.\n"
" --count=<value> Required.\n"
" -r, --retries=<value> Default: 5.\n");
const char* args[] = {"app", "--count=1", "-r", "2"};
const auto result =
rfl::cli::read<WithDefaultVal>(4, const_cast<char**>(args));
ASSERT_TRUE(result) << result.error().what();
EXPECT_EQ(result.value().port.get(), 8080);
EXPECT_EQ(result.value().host.get(), "localhost");
EXPECT_EQ(result.value().retries.value().get(), 2);
const char* no_retries[] = {"app", "--count=1"};
const auto defaulted =
rfl::cli::read<WithDefaultVal>(2, const_cast<char**>(no_retries));
ASSERT_TRUE(defaulted) << defaulted.error().what();
EXPECT_EQ(defaulted.value().retries.value().get(), 5);
}
struct Limits {
rfl::Description<"Connections per host", rfl::DefaultVal<int>> per_host = 4;
int total;
};
/// rfl::DefaultVal inside other wrappers, in different orders.
struct WrappedDefaultVal {
rfl::Short<"l", rfl::Description<"Minimum level to log",
rfl::DefaultVal<LogLevel>>>
log_level = LogLevel::info;
rfl::Description<"Seconds to wait", rfl::DefaultVal<int>> timeout = 30;
rfl::Positional<rfl::DefaultVal<std::string>> output = "out.txt";
rfl::Short<"n", rfl::Description<"Number of runs", int>> runs;
Limits limits;
};
TEST(cli, test_usage_wrapped_default_val) {
EXPECT_EQ(rfl::cli::usage<WrappedDefaultVal>("app", true).value(),
"Usage: app [-l|--log-level=<debug|info|warn>] "
"[--timeout=<value>] [<output>] "
"-n|--runs=<value> [--limits.per-host=<value>] "
"--limits.total=<value>\n"
"\n"
"Arguments:\n"
" -l, --log-level=<debug|info|warn> Minimum level to log. "
"Default: info.\n"
" --timeout=<value> Seconds to wait. "
"Default: 30.\n"
" <output> Default: out.txt.\n"
" -n, --runs=<value> Number of runs. Required.\n"
" --limits.per-host=<value> Connections per host. "
"Default: 4.\n"
" --limits.total=<value> Required.\n");
// Everything the help leaves optional can be left out.
const char* minimal[] = {"app", "-n", "3", "--limits.total=8"};
const auto defaulted =
rfl::cli::read<WrappedDefaultVal>(4, const_cast<char**>(minimal));
ASSERT_TRUE(defaulted) << defaulted.error().what();
EXPECT_EQ(defaulted.value().output.value().get(), "out.txt");
// What the help calls required is required.
const char* no_runs[] = {"app", "--limits.total=8"};
EXPECT_FALSE(
rfl::cli::read<WrappedDefaultVal>(2, const_cast<char**>(no_runs)));
const char* no_total[] = {"app", "-n", "3"};
EXPECT_FALSE(
rfl::cli::read<WrappedDefaultVal>(3, const_cast<char**>(no_total)));
}
/// The default-constructed `port` does not satisfy its validator.
struct WithInvalidDefault {
rfl::Validator<int, rfl::Minimum<1>> port;
rfl::DefaultVal<int> threads = 4;
};
/// The same with the DefaultVal inside rfl::Short.
struct WithInvalidWrappedDefault {
rfl::Validator<int, rfl::Minimum<1>> port;
rfl::Short<"t", rfl::DefaultVal<int>> threads = 4;
};
struct WithInvalidWrappedDefaultInside {
WithInvalidWrappedDefault server;
};
/// Has no default constructor, so neither has a struct that contains it.
class Port {
public:
using ReflectionType = int;
Port(int _value) : value_(_value) {}
int reflection() const { return value_; }
private:
int value_;
};
struct WithPort {
Port port;
int retries = 3;
};
/// A bare DefaultVal here would not compile: the struct has no default
/// constructor. A wrapped one is required.
struct WithPortAndDefaultVal {
Port port;
rfl::Description<"Retries", rfl::DefaultVal<int>> retries = 3;
};
TEST(cli, test_usage_defaults_without_default_construction) {
// The parser fills in a missing DefaultVal field only after it
// default-constructed the struct. If that throws (`port`'s zero value
// fails its validator) or the struct has no default constructor, the
// field is required, and the help shows it without a default.
static_assert(!std::is_default_constructible_v<WithPortAndDefaultVal>);
// With a bare DefaultVal, every read fails, and so does usage: the schema
// takes the defaults from a default-constructed struct.
const auto invalid = rfl::cli::usage<WithInvalidDefault>("app", true);
ASSERT_FALSE(invalid);
EXPECT_EQ(std::string(invalid.error().what()),
"Value expected to be greater than or equal to 1, but got 0.");
EXPECT_EQ(rfl::cli::usage<WithInvalidWrappedDefault>("app").value(),
"Usage: app --port=<value> -t|--threads=<value>\n");
EXPECT_EQ(rfl::cli::usage<WithInvalidWrappedDefaultInside>("app").value(),
"Usage: app --server.port=<value> --server.threads=<value>\n");
EXPECT_EQ(rfl::cli::usage<WithPortAndDefaultVal>("app").value(),
"Usage: app --port=<value> --retries=<value>\n");
// A bare DefaultVal makes every read fail; a wrapped one is required.
const char* both[] = {"app", "--port=1", "--threads=2"};
EXPECT_FALSE(rfl::cli::read<WithInvalidDefault>(3, const_cast<char**>(both)));
const auto wrapped = rfl::cli::read<WithInvalidWrappedDefault>(
3, const_cast<char**>(both));
ASSERT_TRUE(wrapped) << wrapped.error().what();
EXPECT_EQ(wrapped.value().threads.value().get(), 2);
const char* port_only[] = {"app", "--port=1"};
EXPECT_FALSE(rfl::cli::read<WithInvalidWrappedDefault>(
2, const_cast<char**>(port_only)));
EXPECT_FALSE(rfl::cli::read<WithPortAndDefaultVal>(
2, const_cast<char**>(port_only)));
const char* nested[] = {"app", "--server.port=1"};
EXPECT_FALSE(rfl::cli::read<WithInvalidWrappedDefaultInside>(
2, const_cast<char**>(nested)));
// With rfl::DefaultIfMissing nothing is required, but the defaults are
// unknown without T{}, so usage() leaves them out.
static_assert(!std::is_default_constructible_v<WithPort>);
EXPECT_EQ((rfl::cli::usage<WithPort, rfl::DefaultIfMissing>("app", true).value()),
"Usage: app [--port=<value>] [--retries=<value>]\n"
"\n"
"Arguments:\n"
" --port=<value>\n"
" --retries=<value>\n");
}
struct Inner {
std::string host = "db";
int port = 5432;
};
struct WithInitializers {
int count = 3;
std::string name;
bool verbose = false;
std::optional<int> limit;
Inner database;
};
TEST(cli, test_usage_default_if_missing) {
EXPECT_EQ(
(rfl::cli::usage<WithInitializers, rfl::DefaultIfMissing>("app", true).value()),
"Usage: app [--count=<value>] [--name=<value>] [--verbose[=<bool>]] "
"[--limit=<value>] [--database.host=<value>] [--database.port=<value>]\n"
"\n"
"Arguments:\n"
" --count=<value> Default: 3.\n"
" --name=<value> Default: \"\".\n"
" --verbose[=<bool>] Default: false.\n"
" --limit=<value>\n"
" --database.host=<value> Default: db.\n"
" --database.port=<value> Default: 5432.\n");
const char* args[] = {"app"};
const auto result = rfl::cli::read<WithInitializers, rfl::DefaultIfMissing>(
1, const_cast<char**>(args));
ASSERT_TRUE(result) << result.error().what();
EXPECT_EQ(result.value().count, 3);
EXPECT_EQ(result.value().database.port, 5432);
}
TEST(cli, test_usage_without_default_if_missing) {
EXPECT_EQ(rfl::cli::usage<WithInitializers>("app").value(),
"Usage: app --count=<value> --name=<value> --verbose[=<bool>] "
"[--limit=<value>] --database.host=<value> "
"--database.port=<value>\n");
const char* args[] = {"app"};
EXPECT_FALSE(rfl::cli::read<WithInitializers>(1, const_cast<char**>(args)));
}
struct Tls {
bool verify;
};
struct WithNestedFlag {
Tls tls;
};
TEST(cli, test_usage_nested_flag) {
// A nested bool is a flag, as for the parser.
EXPECT_EQ(rfl::cli::usage<WithNestedFlag>("app").value(),
"Usage: app --tls.verify[=<bool>]\n");
}
struct Address {
std::string city;
int zip;
};
struct Names {
rfl::Rename<"output_dir", std::string> output_dir;
rfl::Flatten<Address> address;
std::optional<Database> backup;
std::map<std::string, std::string> labels;
};
TEST(cli, test_usage_names) {
EXPECT_EQ(rfl::cli::usage<Names>("app", true).value(),
"Usage: app --output_dir=<value> --city=<value> --zip=<value> "
"[--backup.host=<value>] [--backup.port=<value>] "
"--labels.<key>=<value>\n"
"\n"
"Arguments:\n"
" --output_dir=<value> Required.\n"
" --city=<value> Required.\n"
" --zip=<value> Required.\n"
" --backup.host=<value>\n"
" --backup.port=<value>\n"
" --labels.<key>=<value> Required.\n");
const char* args[] = {"app", "--output_dir=/tmp", "--city=Springfield",
"--zip=12345", "--labels.env=prod"};
const auto result = rfl::cli::read<Names>(5, const_cast<char**>(args));
ASSERT_TRUE(result) << result.error().what();
EXPECT_EQ(result.value().labels.at("env"), "prod");
}
struct Optionals {
rfl::Positional<std::optional<LogLevel>> level;
rfl::Short<"v", std::optional<bool>> verbose;
rfl::Deprecated<"Use --port instead", "Old port", std::optional<int>>
old_port;
rfl::Description<"First line\nSecond line", std::string> notes;
};
TEST(cli, test_usage_optionals) {
EXPECT_EQ(rfl::cli::usage<Optionals>("app", true).value(),
"Usage: app [<level>] [-v|--verbose[=<bool>]] "
"[--old-port=<value>] --notes=<value>\n"
"\n"
"Arguments:\n"
" <level> Choices: debug|info|warn.\n"
" -v, --verbose[=<bool>]\n"
" --old-port=<value> Old port. Deprecated: Use --port "
"instead.\n"
" --notes=<value> First line\n"
" Second line. Required.\n");
}
TEST(cli, test_usage_no_optionals) {
// Processors are applied exactly as in read: with rfl::NoOptionals,
// optional fields are required.
EXPECT_EQ((rfl::cli::usage<Optionals, rfl::NoOptionals>("app").value()),
"Usage: app <level> -v|--verbose[=<bool>] --old-port=<value> "
"--notes=<value>\n");
}
struct Empty {};
TEST(cli, test_usage_empty) {
EXPECT_EQ(rfl::cli::usage<Empty>("app").value(), "Usage: app\n");
EXPECT_EQ(rfl::cli::usage<Empty>("app", true).value(), "Usage: app\n");
}
} // namespace test_usage