Repository navigation
Expand file tree
/
Copy pathtest_repeated_options.cpp
More file actions
261 lines (226 loc) · 10.2 KB
/
Copy pathtest_repeated_options.cpp
File metadata and controls
261 lines (226 loc) · 10.2 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
#include <gtest/gtest.h>
#include <array>
#include <deque>
#include <map>
#include <optional>
#include <rfl.hpp>
#include <rfl/cli.hpp>
#include <set>
#include <string>
#include <utility>
#include <vector>
namespace test_repeated_options {
template <class T, class... Ps>
rfl::Result<T> read(std::vector<const char*> _args) {
_args.insert(_args.begin(), "program");
return rfl::cli::read<T, Ps...>(static_cast<int>(_args.size()),
const_cast<char**>(_args.data()));
}
template <class T>
std::string error_of(const rfl::Result<T>& _result) {
return _result ? std::string("(no error)")
: std::string(_result.error().what());
}
using Strings = std::vector<std::string>;
struct Database {
std::optional<std::string> host;
std::optional<Strings> replicas;
};
struct Opts {
rfl::Short<"r", std::optional<Strings>> report;
std::optional<std::vector<int>> ports;
std::optional<std::set<std::string>> tags;
std::optional<std::deque<std::string>> queue;
std::optional<std::array<int, 3>> rgb;
std::optional<std::pair<int, std::string>> range;
rfl::Short<"o", std::optional<std::string>> output;
std::optional<int> level;
rfl::Short<"q", std::optional<bool>> quiet;
std::optional<Database> database;
std::optional<std::map<std::string, std::string>> labels;
std::optional<std::map<std::string, Strings>> groups;
rfl::ImplicitValue<std::optional<Strings>, "all"> help_reports;
rfl::Positional<std::optional<std::string>> input;
};
TEST(cli, test_repeated_long_option_appends) {
for (const auto& args : std::vector<std::vector<const char*>>{
{"--report", "a", "--report", "b", "--report", "c"},
{"--report=a", "--report=b", "--report=c"},
{"--report", "a,b", "--report", "c"},
{"--report=a", "--report", "b,c"}}) {
const auto result = read<Opts>(args);
ASSERT_TRUE(result) << result.error().what();
EXPECT_EQ(result.value().report(), (Strings{"a", "b", "c"}));
}
}
TEST(cli, test_repeated_short_and_long_merge_in_argv_order) {
const auto result =
read<Opts>({"-r", "a", "--report", "b", "-r=c", "-rd", "--report=e"});
ASSERT_TRUE(result) << result.error().what();
EXPECT_EQ(result.value().report(), (Strings{"a", "b", "c", "d", "e"}));
}
TEST(cli, test_repeated_short_list_in_a_cluster) {
// -q is a flag, so -qra is -q -r a.
const auto result = read<Opts>({"-qra", "-r", "b", "in.txt"});
ASSERT_TRUE(result) << result.error().what();
EXPECT_EQ(result.value().quiet(), true);
EXPECT_EQ(result.value().report(), (Strings{"a", "b"}));
EXPECT_EQ(result.value().input(), "in.txt");
// The flag is still given only once.
EXPECT_EQ(error_of(read<Opts>({"-qra", "-qrb"})),
"Duplicate short argument: -qrb");
}
TEST(cli, test_repeated_abbreviations_merge) {
const auto result =
read<Opts>({"--rep", "a", "--report", "b", "--repo=c", "-r", "d"});
ASSERT_TRUE(result) << result.error().what();
EXPECT_EQ(result.value().report(), (Strings{"a", "b", "c", "d"}));
}
TEST(cli, test_repeated_other_list_types) {
const auto result = read<Opts>(
{"--ports", "80", "--ports=81,82", "--tags", "b", "--tags", "a,b",
"--queue", "x", "--queue", "y", "--rgb", "1", "--rgb", "2,3",
"--range", "5", "--range", "five"});
ASSERT_TRUE(result) << result.error().what();
const auto& o = result.value();
EXPECT_EQ(o.ports, (std::vector<int>{80, 81, 82}));
EXPECT_EQ(o.tags, (std::set<std::string>{"a", "b"}));
EXPECT_EQ(o.queue, (std::deque<std::string>{"x", "y"}));
EXPECT_EQ(o.rgb, (std::array<int, 3>{1, 2, 3}));
EXPECT_EQ(o.range, (std::pair<int, std::string>{5, "five"}));
}
TEST(cli, test_repeated_fixed_size_checked_on_all_items) {
// Repeating is the same as joining with ',': the size is checked once.
EXPECT_EQ(error_of(read<Opts>({"--rgb", "1", "--rgb", "2"})),
error_of(read<Opts>({"--rgb=1,2"})));
EXPECT_NE(error_of(read<Opts>({"--rgb=1,2"})), "(no error)");
}
TEST(cli, test_repeated_empty_occurrence_adds_nothing) {
const auto some = read<Opts>({"--report=a", "--report=", "--report=b"});
ASSERT_TRUE(some) << some.error().what();
EXPECT_EQ(some.value().report(), (Strings{"a", "b"}));
const auto none = read<Opts>({"--report=", "--report="});
ASSERT_TRUE(none) << none.error().what();
EXPECT_EQ(none.value().report(), Strings{});
}
TEST(cli, test_repeated_nested_list) {
const auto result = read<Opts>({"--database.replicas", "a",
"--database.replicas=b,c",
"--database.host=db"});
ASSERT_TRUE(result) << result.error().what();
ASSERT_TRUE(result.value().database);
EXPECT_EQ(result.value().database->replicas, (Strings{"a", "b", "c"}));
EXPECT_EQ(result.value().database->host, "db");
}
TEST(cli, test_repeated_map_entry_list) {
const auto result = read<Opts>(
{"--groups.x", "a", "--groups.y=c", "--groups.x=b"});
ASSERT_TRUE(result) << result.error().what();
EXPECT_EQ(result.value().groups,
(std::map<std::string, Strings>{{"x", {"a", "b"}}, {"y", {"c"}}}));
}
TEST(cli, test_repeated_scalar_errors) {
EXPECT_EQ(error_of(read<Opts>({"--output", "a", "--output", "b"})),
"Duplicate argument: --output");
EXPECT_EQ(error_of(read<Opts>({"--level=1", "--lev=2"})),
"Duplicate argument: --lev=2");
EXPECT_EQ(error_of(read<Opts>({"-o", "a", "-o", "b"})),
"Duplicate short argument: -o");
EXPECT_EQ(error_of(read<Opts>({"-o", "a", "--output=b"})),
"Conflict: '-o' and '--output' both provided.");
EXPECT_EQ(error_of(read<Opts>({"--quiet", "--quiet"})),
"Duplicate argument: --quiet");
EXPECT_EQ(error_of(read<Opts>({"x", "--input=y"})),
"Conflict: positional argument and '--input' both provided.");
}
TEST(cli, test_repeated_nested_scalar_and_map_key_error) {
EXPECT_EQ(
error_of(read<Opts>({"--database.host=a", "--database.host", "b"})),
"Duplicate argument: --database.host");
EXPECT_EQ(error_of(read<Opts>({"--labels.env=a", "--labels.env=b"})),
"Duplicate argument: --labels.env=b");
const auto two_keys = read<Opts>({"--labels.env=a", "--labels.os=b"});
ASSERT_TRUE(two_keys) << two_keys.error().what();
EXPECT_EQ(two_keys.value().labels,
(std::map<std::string, std::string>{{"env", "a"}, {"os", "b"}}));
}
TEST(cli, test_repeated_implicit_value_list) {
// A bare occurrence adds the implicit value, split like any other value.
const auto bare = read<Opts>({"--help-reports"});
ASSERT_TRUE(bare) << bare.error().what();
EXPECT_EQ(bare.value().help_reports(), (Strings{"all"}));
const auto mixed = read<Opts>(
{"--help-reports=cpu", "--help-reports", "--help-rep=gpu,mem"});
ASSERT_TRUE(mixed) << mixed.error().what();
EXPECT_EQ(mixed.value().help_reports(), (Strings{"cpu", "all", "gpu", "mem"}));
// It still never takes the next token.
const auto next = read<Opts>({"--help-reports", "in.txt"});
ASSERT_TRUE(next) << next.error().what();
EXPECT_EQ(next.value().help_reports(), (Strings{"all"}));
EXPECT_EQ(next.value().input(), "in.txt");
const auto absent = read<Opts>({});
ASSERT_TRUE(absent) << absent.error().what();
EXPECT_FALSE(absent.value().help_reports().has_value());
}
TEST(cli, test_repeated_with_no_extra_fields) {
const auto result = read<Opts, rfl::NoExtraFields>(
{"--report", "a", "-r", "b"});
ASSERT_TRUE(result) << result.error().what();
EXPECT_EQ(result.value().report(), (Strings{"a", "b"}));
EXPECT_EQ(error_of(read<Opts, rfl::NoExtraFields>({"--nope=1", "--nope=2"})),
"Unknown option '--nope'.");
// Without NoExtraFields, an unknown option is ignored, but may be given
// only once.
EXPECT_EQ(error_of(read<Opts>({"--nope=1", "--nope=2"})),
"Duplicate argument: --nope=2");
}
struct Wrapped {
rfl::Description<"Files to include",
rfl::DefaultVal<std::vector<std::string>>>
include;
rfl::Rename<"L", std::optional<std::vector<std::string>>> lib_dirs;
rfl::Validator<std::vector<int>, rfl::Size<rfl::Maximum<2>>> pair;
};
TEST(cli, test_repeated_through_wrappers) {
const auto result = read<Wrapped>(
{"--include", "a.h", "--include=b.h", "--L", "/x", "--L", "/y",
"--pair", "1", "--pair", "2"});
ASSERT_TRUE(result) << result.error().what();
EXPECT_EQ(result.value().include().value(), (Strings{"a.h", "b.h"}));
EXPECT_EQ(result.value().lib_dirs(), (Strings{"/x", "/y"}));
EXPECT_EQ(result.value().pair.value(), (std::vector<int>{1, 2}));
// The validator sees every item.
EXPECT_NE(error_of(read<Wrapped>({"--pair", "1", "--pair", "2,3"})),
"(no error)");
}
TEST(cli, test_repeated_occurrences_in_parsed_args) {
// The occurrences of a list option are held under its long name,
// the first in `named` and the others in `later_occurrences`.
const auto options = rfl::cli::internal::make_options<
Opts, rfl::Processors<rfl::SnakeCaseToKebabCase>>();
std::vector<const char*> args = {"program", "-r", "a,b", "--report", "c"};
const auto parsed = rfl::cli::internal::parse_argv(
static_cast<int>(args.size()), const_cast<char**>(args.data()), options);
ASSERT_TRUE(parsed) << parsed.error().what();
EXPECT_EQ(parsed.value().named.at("report"), "a,b");
EXPECT_EQ(parsed.value().later_occurrences.at("report"), Strings{"c"});
EXPECT_TRUE(parsed.value().short_args.empty());
// The flat map of resolve_args<T> cannot hold them.
const auto resolved = rfl::cli::resolve_args<
Opts, rfl::Processors<rfl::SnakeCaseToKebabCase>>(parsed.value());
EXPECT_EQ(error_of(resolved), "Duplicate argument: --report");
}
TEST(cli, test_repeated_occurrences_keep_embedded_nul) {
// The occurrences are kept apart, not joined with a separator, so an
// argument that contains '\0', which the range overload can pass, is
// not split there.
const auto result = rfl::cli::read<Opts>(std::vector<std::string>{
std::string("--report=a\0b", 12), "--report", "c"});
ASSERT_TRUE(result) << result.error().what();
EXPECT_EQ(result.value().report(), (Strings{std::string("a\0b", 3), "c"}));
const auto once = rfl::cli::read<Opts>(
std::vector<std::string>{std::string("--report=a\0b,c", 14)});
ASSERT_TRUE(once) << once.error().what();
EXPECT_EQ(once.value().report(), (Strings{std::string("a\0b", 3), "c"}));
}
} // namespace test_repeated_options