Repository navigation
Expand file tree
/
Copy pathtest_help_version.cpp
More file actions
374 lines (327 loc) · 14.5 KB
/
Copy pathtest_help_version.cpp
File metadata and controls
374 lines (327 loc) · 14.5 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
#include <gtest/gtest.h>
#include <optional>
#include <rfl.hpp>
#include <rfl/cli.hpp>
#include <string>
#include <variant>
#include <vector>
// `--help` and `--version` in rfl::cli::read_app, turned on by
// rfl::cli::App, and their rows in rfl::cli::usage.
namespace test_help_version {
template <class T, class... Ps>
rfl::Result<std::variant<T, rfl::cli::HelpOrVersion>> read_app(
std::vector<const char*> _args) {
_args.insert(_args.begin(), "app");
return rfl::cli::read_app<T, Ps...>(static_cast<int>(_args.size()),
const_cast<char**>(_args.data()));
}
template <class T, class... Ps>
rfl::Result<T> read(std::vector<const char*> _args) {
_args.insert(_args.begin(), "app");
return rfl::cli::read<T, Ps...>(static_cast<int>(_args.size()),
const_cast<char**>(_args.data()));
}
/// The help or version text, or "(struct)" or the error.
template <class T>
std::string text_of(
const rfl::Result<std::variant<T, rfl::cli::HelpOrVersion>>& _result) {
if (!_result) {
return std::string("error: ") + _result.error().what();
}
const auto* info = std::get_if<rfl::cli::HelpOrVersion>(&*_result);
return info ? info->text : std::string("(struct)");
}
template <class T>
std::optional<rfl::cli::HelpOrVersion::Kind> kind_of(
const rfl::Result<std::variant<T, rfl::cli::HelpOrVersion>>& _result) {
if (!_result) {
return std::nullopt;
}
const auto* info = std::get_if<rfl::cli::HelpOrVersion>(&*_result);
return info ? std::optional(info->kind) : std::nullopt;
}
using Kind = rfl::cli::HelpOrVersion::Kind;
struct Copy {
rfl::Positional<rfl::Description<"File to copy", std::string>> source;
rfl::Short<"o", rfl::Description<"Where to copy it", std::string>> output;
rfl::Short<"v", std::optional<bool>> verbose;
rfl::Short<"j", rfl::DefaultVal<int>> jobs = 1;
};
using CopyApp = rfl::cli::App<"copy", "1.2.0", "Copies a file.">;
constexpr const char* copy_help =
"copy 1.2.0\n"
"Copies a file.\n"
"\n"
"Usage: app [-h|--help] [--version] <source> -o|--output=<value> "
"[-v|--verbose[=<bool>]] [-j|--jobs=<value>]\n"
"\n"
"Arguments:\n"
" -h, --help Show this help\n"
" --version Show the version\n"
" <source> File to copy. Required.\n"
" -o, --output=<value> Where to copy it. Required.\n"
" -v, --verbose[=<bool>]\n"
" -j, --jobs=<value> Default: 1.\n";
TEST(cli, test_help_version_usage) {
EXPECT_EQ((rfl::cli::usage<Copy, CopyApp>("app", true).value()), copy_help);
// The usage line has no header.
EXPECT_EQ((rfl::cli::usage<Copy, CopyApp>("app").value()),
"Usage: app [-h|--help] [--version] <source> -o|--output=<value> "
"[-v|--verbose[=<bool>]] [-j|--jobs=<value>]\n");
// Without rfl::cli::App, there are no such rows.
EXPECT_EQ(rfl::cli::usage<Copy>("app").value(),
"Usage: app <source> -o|--output=<value> "
"[-v|--verbose[=<bool>]] [-j|--jobs=<value>]\n");
}
TEST(cli, test_help_version_help) {
// The required fields are missing, but the help is shown.
for (const auto& args : std::vector<std::vector<const char*>>{
{"--help"}, {"-h"}, {"-vh"}, {"in.txt", "-h"},
{"--version", "--help"}, {"--help=true"}, {"-h", "--jobs=x"}}) {
const auto result = read_app<Copy, CopyApp>(args);
EXPECT_EQ(kind_of(result), Kind::help) << args.front();
EXPECT_EQ(text_of(result), copy_help) << args.front();
}
}
TEST(cli, test_help_version_version) {
for (const auto& args : std::vector<std::vector<const char*>>{
{"--version"}, {"in.txt", "--version", "-v"}}) {
const auto result = read_app<Copy, CopyApp>(args);
EXPECT_EQ(kind_of(result), Kind::version) << args.front();
EXPECT_EQ(text_of(result), "copy 1.2.0\n") << args.front();
}
}
TEST(cli, test_help_version_abbreviated) {
// With rfl::cli::AllowAbbreviations, `--help` and `--version` can be
// abbreviated like the fields.
using Abbreviated = rfl::cli::AllowAbbreviations;
const auto help = read_app<Copy, CopyApp, Abbreviated>({"--hel"});
EXPECT_EQ(kind_of(help), Kind::help);
EXPECT_EQ(text_of(help), copy_help);
const auto version = read_app<Copy, CopyApp, Abbreviated>({"--vers"});
EXPECT_EQ(kind_of(version), Kind::version);
// `--verbose` shares a prefix with `--version`.
EXPECT_EQ(text_of(read_app<Copy, CopyApp, Abbreviated>({"--ver"})),
"error: Option '--ver' is ambiguous; possibilities: '--verbose', "
"'--version'.");
// Without it, `--hel` is an unknown option, which is ignored.
EXPECT_EQ(text_of(read_app<Copy, CopyApp>({"--hel", "in.txt", "-o", "x"})),
"(struct)");
}
TEST(cli, test_help_version_reads_the_struct) {
const auto result =
read_app<Copy, CopyApp>({"in.txt", "-o", "out.txt", "-j", "4"});
ASSERT_TRUE(result) << result.error().what();
const auto* copy = std::get_if<Copy>(&*result);
ASSERT_TRUE(copy);
EXPECT_EQ(copy->source.value().value(), "in.txt");
EXPECT_EQ(copy->output.value().value(), "out.txt");
EXPECT_EQ(copy->jobs.value().get(), 4);
// `--help=false` does not ask for the help.
const auto not_help =
read_app<Copy, CopyApp>({"in.txt", "-o", "out.txt", "--help=false"});
ASSERT_TRUE(not_help) << not_help.error().what();
EXPECT_TRUE(std::holds_alternative<Copy>(*not_help));
}
TEST(cli, test_help_version_errors) {
// Errors are errors, not help.
EXPECT_EQ(text_of(read_app<Copy, CopyApp>({"in.txt"})),
"error: Field named 'output' not found.");
EXPECT_EQ(text_of(read_app<Copy, CopyApp>({"--help=yes"})),
"error: Could not cast 'yes' to boolean for key 'help'.");
// An earlier token that fails to parse is an error.
EXPECT_EQ(text_of(read_app<Copy, CopyApp>({"-o", "--help"})),
"error: Missing value for option '-o'.");
// After `--`, everything is positional.
EXPECT_EQ(text_of(read_app<Copy, CopyApp>({"-o", "x", "--", "--help"})),
"(struct)");
}
TEST(cli, test_help_version_off_by_default) {
// Without rfl::cli::App, `--help` is an option like any other: ignored
// unless rfl::NoExtraFields is passed.
const auto ignored = read<Copy>({"in.txt", "-o", "out.txt", "--help"});
ASSERT_TRUE(ignored) << ignored.error().what();
const auto rejected =
read<Copy, rfl::NoExtraFields>({"in.txt", "-o", "out.txt", "--help"});
ASSERT_FALSE(rejected);
EXPECT_EQ(std::string(rejected.error().what()), "Unknown option '--help'.");
EXPECT_FALSE(read<Copy>({"-h"}));
// read_app without rfl::cli::App is read.
EXPECT_EQ(text_of(read_app<Copy>({"in.txt", "-o", "out.txt", "--help"})),
"(struct)");
}
// A field that takes the name of a built-in option does not compile: a
// field with rfl::Short<"h", ...> (wherever it sits among the field's
// wrappers), a field named `help`, or, with a version, a field named
// `version`. rfl::cli::App opts out of them instead.
// rfl::cli::BuiltinHelp::long_only leaves `-h` to a field: the help is
// `--help` only.
struct WithHostShort {
rfl::Short<"h", rfl::Description<"Host to connect to", std::string>> host;
};
using LongHelpApp =
rfl::cli::App<"client", "", "", rfl::cli::BuiltinHelp::long_only>;
TEST(cli, test_help_version_long_only) {
EXPECT_EQ((rfl::cli::usage<WithHostShort, LongHelpApp>("client", true)
.value()),
"client\n"
"\n"
"Usage: client [--help] -h|--host=<value>\n"
"\n"
"Arguments:\n"
" --help Show this help\n"
" -h, --host=<value> Host to connect to. Required.\n");
const auto host = read_app<WithHostShort, LongHelpApp>({"-h", "example.com"});
ASSERT_TRUE(host) << host.error().what();
EXPECT_EQ(std::get<WithHostShort>(*host).host.value().value(),
"example.com");
EXPECT_EQ(kind_of(read_app<WithHostShort, LongHelpApp>({"--help"})),
Kind::help);
// No version: `--version` is an unknown option.
EXPECT_EQ(text_of(read_app<WithHostShort, LongHelpApp, rfl::NoExtraFields>(
{"--version"})),
"error: Unknown option '--version'.");
}
// The same with rfl::Short inside other wrappers.
struct WithWrappedHostShort {
rfl::Description<"Host to connect to", rfl::Short<"h", std::string>> host;
};
TEST(cli, test_help_version_long_only_wrapped_short_h) {
EXPECT_EQ((rfl::cli::usage<WithWrappedHostShort, LongHelpApp>("client")
.value()),
"Usage: client [--help] -h|--host=<value>\n");
const auto host =
read_app<WithWrappedHostShort, LongHelpApp>({"-h", "example.com"});
ASSERT_TRUE(host) << host.error().what();
EXPECT_EQ(std::get<WithWrappedHostShort>(*host).host.value().value(),
"example.com");
}
// rfl::cli::BuiltinHelp::none leaves out the help, like argparse's
// add_help=False, so a field can be called `help`. Here it is
// `--help=<topic>`.
struct WithHelpField {
rfl::Description<"Show help on a topic", std::optional<std::string>> help;
rfl::Short<"n", std::optional<int>> count;
};
TEST(cli, test_help_version_without_help) {
using MyApp = rfl::cli::App<"tool", "0.1", "", rfl::cli::BuiltinHelp::none>;
EXPECT_EQ((rfl::cli::usage<WithHelpField, MyApp>("tool", true).value()),
"tool 0.1\n"
"\n"
"Usage: tool [--version] [--help=<value>] [-n|--count=<value>]\n"
"\n"
"Arguments:\n"
" --version Show the version\n"
" --help=<value> Show help on a topic\n"
" -n, --count=<value>\n");
const auto topic = read_app<WithHelpField, MyApp>({"--help=lists"});
ASSERT_TRUE(topic) << topic.error().what();
EXPECT_EQ(std::get<WithHelpField>(*topic).help.value().value(), "lists");
// `-h` is not the help either.
EXPECT_FALSE((read_app<WithHelpField, MyApp>({"-h"})));
EXPECT_EQ(text_of(read_app<WithHelpField, MyApp>({"--version"})),
"tool 0.1\n");
}
// Without a version, there is no `--version`, so a field can be called
// `version`.
struct WithVersionField {
std::optional<int> version;
};
TEST(cli, test_help_version_without_version) {
using MyApp = rfl::cli::App<"tool">;
const auto result = read_app<WithVersionField, MyApp>({"--version=3"});
ASSERT_TRUE(result) << result.error().what();
EXPECT_EQ(std::get<WithVersionField>(*result).version, 3);
EXPECT_EQ((rfl::cli::usage<WithVersionField, MyApp>("tool").value()),
"Usage: tool [-h|--help] [--version=<value>]\n");
}
TEST(cli, test_help_version_without_name) {
// Without a name, the program name is used.
using MyApp = rfl::cli::App<"", "2.0", "", rfl::cli::BuiltinHelp::long_only>;
EXPECT_EQ(text_of(read_app<WithHostShort, MyApp>({"--version"})),
"app 2.0\n");
EXPECT_EQ((rfl::cli::usage<WithHostShort, MyApp>("app", true).value()),
"app 2.0\n"
"\n"
"Usage: app [--help] [--version] -h|--host=<value>\n"
"\n"
"Arguments:\n"
" --help Show this help\n"
" --version Show the version\n"
" -h, --host=<value> Host to connect to. Required.\n");
// Without anything, there is no header and no `--version`.
EXPECT_EQ((rfl::cli::usage<WithVersionField, rfl::cli::App<>>("app", true).value()),
"Usage: app [-h|--help] [--version=<value>]\n"
"\n"
"Arguments:\n"
" -h, --help Show this help\n"
" --version=<value>\n");
}
TEST(cli, test_help_version_no_extra_fields) {
// The built-in options are known options, so rfl::NoExtraFields accepts
// them.
EXPECT_EQ(
(kind_of(read_app<Copy, CopyApp, rfl::NoExtraFields>({"--help"}))),
Kind::help);
const auto result = read_app<Copy, CopyApp, rfl::NoExtraFields>(
{"in.txt", "-o", "out.txt", "--help=0"});
ASSERT_TRUE(result) << result.error().what();
EXPECT_TRUE(std::holds_alternative<Copy>(*result));
}
TEST(cli, test_help_version_spellings) {
// `-h` and `--help` together are the help.
EXPECT_EQ(kind_of(read_app<Copy, CopyApp>({"-h", "--help"})), Kind::help);
// A negative number is still a positional argument.
const auto negative = read_app<Copy, CopyApp>({"-5", "-o", "out.txt"});
ASSERT_TRUE(negative) << negative.error().what();
EXPECT_EQ(std::get<Copy>(*negative).source.value().value(), "-5");
// const argv, as for rfl::cli::read.
const char* const args[] = {"app", "-vh"};
EXPECT_EQ(kind_of(rfl::cli::read_app<Copy, CopyApp>(2, args)), Kind::help);
}
TEST(cli, test_help_version_wide) {
const wchar_t* args[] = {L"app", L"--version"};
const auto result =
rfl::cli::read_app<Copy, CopyApp>(2, const_cast<wchar_t**>(args));
EXPECT_EQ(text_of(result), "copy 1.2.0\n");
}
using Strings = std::vector<std::string>;
// recipe [options] <name> <args...>, as in test_options_first.cpp.
struct Recipe {
rfl::Short<"v", std::optional<bool>> verbose;
rfl::Positional<std::string> name;
rfl::Positional<rfl::DefaultVal<Strings>> args;
};
TEST(cli, test_help_version_options_first) {
using Settings = rfl::cli::App<"recipe">;
// `--help` before the first positional argument is the help. After it,
// it is passed through, as every later token is.
EXPECT_EQ((kind_of(read_app<Recipe, Settings, rfl::cli::OptionsFirst,
rfl::cli::ListDelimiter<"">>({"-v", "--help"}))),
Kind::help);
const auto passed = read_app<Recipe, Settings, rfl::cli::OptionsFirst,
rfl::cli::ListDelimiter<"">>({"build", "--help"});
ASSERT_TRUE(passed) << passed.error().what();
EXPECT_EQ(std::get<Recipe>(*passed).args.value().value(),
(Strings{"--help"}));
}
struct Report {
rfl::ImplicitValue<std::optional<std::string>, "all"> details;
std::optional<std::vector<std::optional<std::string>>> tags;
};
TEST(cli, test_help_version_implicit_value_and_keep_empty_items) {
using Settings = rfl::cli::App<"report">;
// The settings below rfl::cli::App apply to the struct as in read.
const auto result =
read_app<Report, Settings, rfl::cli::KeepEmptyItems,
rfl::cli::ListDelimiter<";">>({"--details", "--tags=a;;b"});
ASSERT_TRUE(result) << result.error().what();
const auto& report = std::get<Report>(*result);
EXPECT_EQ(report.details(), "all");
EXPECT_EQ(report.tags, (std::vector<std::optional<std::string>>{
"a", std::nullopt, "b"}));
// A bare `--details` never takes the next token, so `--help` is the help.
EXPECT_EQ(kind_of(read_app<Report, Settings>({"--details", "--help"})),
Kind::help);
}
} // namespace test_help_version