11#include "gtest/gtest.h"
16TEST(CommandLineTest, Basic) {
19 {
"my_program",
"--flag1",
"--flag2=value2",
"arg1",
"arg2",
"arg3"});
21 EXPECT_TRUE(cl.has_argv0());
22 EXPECT_EQ(
"my_program", cl.argv0());
24 EXPECT_EQ(2u, cl.options().size());
25 EXPECT_EQ(
"flag1", cl.options()[0].name);
26 EXPECT_EQ(std::string(), cl.options()[0].value);
27 EXPECT_EQ(
"flag2", cl.options()[1].name);
28 EXPECT_EQ(
"value2", cl.options()[1].value);
30 EXPECT_EQ(3u, cl.positional_args().size());
31 EXPECT_EQ(
"arg1", cl.positional_args()[0]);
32 EXPECT_EQ(
"arg2", cl.positional_args()[1]);
33 EXPECT_EQ(
"arg3", cl.positional_args()[2]);
35 EXPECT_TRUE(cl.HasOption(
"flag1"));
36 EXPECT_TRUE(cl.HasOption(
"flag1",
nullptr));
37 size_t index =
static_cast<size_t>(-1);
38 EXPECT_TRUE(cl.HasOption(
"flag2", &index));
40 EXPECT_FALSE(cl.HasOption(
"flag3"));
41 EXPECT_FALSE(cl.HasOption(
"flag3",
nullptr));
43 std::string
value =
"nonempty";
44 EXPECT_TRUE(cl.GetOptionValue(
"flag1", &value));
45 EXPECT_EQ(std::string(), value);
46 EXPECT_TRUE(cl.GetOptionValue(
"flag2", &value));
47 EXPECT_EQ(
"value2", value);
48 EXPECT_FALSE(cl.GetOptionValue(
"flag3", &value));
50 EXPECT_EQ(std::string(), cl.GetOptionValueWithDefault(
"flag1",
"nope"));
51 EXPECT_EQ(
"value2", cl.GetOptionValueWithDefault(
"flag2",
"nope"));
52 EXPECT_EQ(
"nope", cl.GetOptionValueWithDefault(
"flag3",
"nope"));
55TEST(CommandLineTest, DefaultConstructor) {
57 EXPECT_FALSE(cl.has_argv0());
58 EXPECT_EQ(std::string(), cl.argv0());
59 EXPECT_EQ(std::vector<CommandLine::Option>(), cl.options());
60 EXPECT_EQ(std::vector<std::string>(), cl.positional_args());
63TEST(CommandLineTest, ComponentConstructor) {
64 const std::string argv0 =
"my_program";
65 const std::vector<CommandLine::Option> options = {
66 CommandLine::Option(
"flag",
"value")};
67 const std::vector<std::string> positional_args = {
"arg"};
69 CommandLine cl(argv0, options, positional_args);
70 EXPECT_TRUE(cl.has_argv0());
71 EXPECT_EQ(argv0, cl.argv0());
72 EXPECT_EQ(options, cl.options());
73 EXPECT_EQ(positional_args, cl.positional_args());
74 EXPECT_EQ(
"value", cl.GetOptionValueWithDefault(
"flag",
"nope"));
80 static std::vector<std::string>
argv = {
"my_program",
"--flag1",
81 "--flag2",
"subcommand",
82 "--subflag",
"subarg"};
83 auto first =
argv.cbegin();
84 auto last =
argv.cend();
85 std::vector<std::string>::const_iterator sub_first;
88 EXPECT_TRUE(cl.has_argv0());
89 EXPECT_EQ(
argv[0], cl.argv0());
90 std::vector<CommandLine::Option> expected_options = {
91 CommandLine::Option(
"flag1"), CommandLine::Option(
"flag2")};
92 EXPECT_EQ(expected_options, cl.options());
93 std::vector<std::string> expected_positional_args = {
argv[3],
argv[4],
95 EXPECT_EQ(expected_positional_args, cl.positional_args());
96 EXPECT_TRUE(cl.HasOption(
"flag1",
nullptr));
97 EXPECT_TRUE(cl.HasOption(
"flag2",
nullptr));
98 EXPECT_FALSE(cl.HasOption(
"subflag",
nullptr));
100 EXPECT_EQ(first + 3, sub_first);
102 EXPECT_TRUE(sub_cl.has_argv0());
103 EXPECT_EQ(
argv[3], sub_cl.argv0());
104 std::vector<CommandLine::Option> expected_sub_options = {
105 CommandLine::Option(
"subflag")};
106 EXPECT_EQ(expected_sub_options, sub_cl.options());
107 std::vector<std::string> expected_sub_positional_args = {
argv[5]};
108 EXPECT_EQ(expected_sub_positional_args, sub_cl.positional_args());
109 EXPECT_FALSE(sub_cl.HasOption(
"flag1",
nullptr));
110 EXPECT_FALSE(sub_cl.HasOption(
"flag2",
nullptr));
111 EXPECT_TRUE(sub_cl.HasOption(
"subflag",
nullptr));
116 static std::vector<std::string>
argv = {
"my_program",
"--flag"};
117 std::vector<std::string>::const_iterator sub_first;
119 argv.cbegin(),
argv.cend(), &sub_first);
120 EXPECT_EQ(
argv.cend(), sub_first);
125 static std::vector<std::string>
argv = {
"my_program",
"arg1",
"arg2"};
126 std::vector<std::string>::const_iterator sub_first;
128 argv.cbegin(),
argv.cend(), &sub_first);
129 EXPECT_EQ(
argv.cbegin() + 1, sub_first);
134 static std::vector<std::string>
argv = {
"my_program",
"--",
"--arg"};
135 std::vector<std::string>::const_iterator sub_first;
137 argv.cbegin(),
argv.cend(), &sub_first);
138 EXPECT_EQ(
argv.cbegin() + 2, sub_first);
142TEST(CommandLineTest, CommmandLineFromIterators) {
146 const std::vector<std::string>
argv = {
"my_program",
"--flag=value",
"arg"};
149 EXPECT_TRUE(cl.has_argv0());
150 EXPECT_EQ(
argv[0], cl.argv0());
151 std::vector<CommandLine::Option> expected_options = {
152 CommandLine::Option(
"flag",
"value")};
153 EXPECT_EQ(expected_options, cl.options());
154 std::vector<std::string> expected_positional_args = {
argv[2]};
155 EXPECT_EQ(expected_positional_args, cl.positional_args());
156 EXPECT_EQ(
"value", cl.GetOptionValueWithDefault(
"flag",
"nope"));
161 const std::vector<std::string>
argv;
164 EXPECT_FALSE(cl.has_argv0());
165 EXPECT_EQ(std::string(), cl.argv0());
166 EXPECT_EQ(std::vector<CommandLine::Option>(), cl.options());
167 EXPECT_EQ(std::vector<std::string>(), cl.positional_args());
172 const std::vector<std::string>
argv = {
""};
175 EXPECT_TRUE(cl.has_argv0());
176 EXPECT_EQ(std::string(), cl.argv0());
177 EXPECT_EQ(std::vector<CommandLine::Option>(), cl.options());
178 EXPECT_EQ(std::vector<std::string>(), cl.positional_args());
183 const std::vector<const char*>
argv = {
"my_program",
"--flag=value",
"arg"};
186 EXPECT_TRUE(cl.has_argv0());
187 EXPECT_EQ(
argv[0], cl.argv0());
188 std::vector<CommandLine::Option> expected_options = {
189 CommandLine::Option(
"flag",
"value")};
190 EXPECT_EQ(expected_options, cl.options());
191 std::vector<std::string> expected_positional_args = {
argv[2]};
192 EXPECT_EQ(expected_positional_args, cl.positional_args());
193 EXPECT_EQ(
"value", cl.GetOptionValueWithDefault(
"flag",
"nope"));
198 static const char*
const argv[] = {
"my_program",
"--flag=value",
"arg"};
201 EXPECT_TRUE(cl.has_argv0());
202 EXPECT_EQ(
argv[0], cl.argv0());
203 std::vector<CommandLine::Option> expected_options = {
204 CommandLine::Option(
"flag",
"value")};
205 EXPECT_EQ(expected_options, cl.options());
206 std::vector<std::string> expected_positional_args = {
argv[2]};
207 EXPECT_EQ(expected_positional_args, cl.positional_args());
208 EXPECT_EQ(
"value", cl.GetOptionValueWithDefault(
"flag",
"nope"));
213 static const char*
const argv[] = {
"my_program",
"--flag=value",
"arg"};
214 const int argc =
static_cast<int>(std::size(
argv));
217 EXPECT_TRUE(cl.has_argv0());
218 EXPECT_EQ(
argv[0], cl.argv0());
219 std::vector<CommandLine::Option> expected_options = {
220 CommandLine::Option(
"flag",
"value")};
221 EXPECT_EQ(expected_options, cl.options());
222 std::vector<std::string> expected_positional_args = {
argv[2]};
223 EXPECT_EQ(expected_positional_args, cl.positional_args());
224 EXPECT_EQ(
"value", cl.GetOptionValueWithDefault(
"flag",
"nope"));
229 std::initializer_list<const char*> il = {
"my_program",
"--flag=value",
232 EXPECT_TRUE(cl.has_argv0());
233 EXPECT_EQ(
"my_program", cl.argv0());
234 std::vector<CommandLine::Option> expected_options = {
235 CommandLine::Option(
"flag",
"value")};
236 EXPECT_EQ(expected_options, cl.options());
237 std::vector<std::string> expected_positional_args = {
"arg"};
238 EXPECT_EQ(expected_positional_args, cl.positional_args());
239 EXPECT_EQ(
"value", cl.GetOptionValueWithDefault(
"flag",
"nope"));
243 std::initializer_list<std::string> il = {
"my_program",
"--flag=value",
246 EXPECT_TRUE(cl.has_argv0());
247 EXPECT_EQ(
"my_program", cl.argv0());
248 std::vector<CommandLine::Option> expected_options = {
249 CommandLine::Option(
"flag",
"value")};
250 EXPECT_EQ(expected_options, cl.options());
251 std::vector<std::string> expected_positional_args = {
"arg"};
252 EXPECT_EQ(expected_positional_args, cl.positional_args());
253 EXPECT_EQ(
"value", cl.GetOptionValueWithDefault(
"flag",
"nope"));
257TEST(CommandLineTest, OddArguments) {
261 {
"my_program",
"--=",
"--=foo",
"--bar=",
"--==",
"--===",
"--==x",
263 EXPECT_TRUE(cl.has_argv0());
264 EXPECT_EQ(
"my_program", cl.argv0());
265 std::vector<CommandLine::Option> expected_options = {
266 CommandLine::Option(
"="), CommandLine::Option(
"=foo"),
267 CommandLine::Option(
"bar"), CommandLine::Option(
"="),
268 CommandLine::Option(
"=",
"="), CommandLine::Option(
"=",
"x")};
269 EXPECT_EQ(expected_options, cl.options());
270 std::vector<std::string> expected_positional_args = {
"arg"};
271 EXPECT_EQ(expected_positional_args, cl.positional_args());
277 EXPECT_TRUE(cl.has_argv0());
278 EXPECT_EQ(std::string(), cl.argv0());
279 EXPECT_EQ(std::vector<CommandLine::Option>(), cl.options());
280 std::vector<std::string> expected_positional_args = {
"-x"};
281 EXPECT_EQ(expected_positional_args, cl.positional_args());
287 EXPECT_TRUE(cl.has_argv0());
288 EXPECT_EQ(std::string(), cl.argv0());
289 EXPECT_EQ(std::vector<CommandLine::Option>(), cl.options());
290 std::vector<std::string> expected_positional_args = {
"-"};
291 EXPECT_EQ(expected_positional_args, cl.positional_args());
298 {
"",
"--flag=value",
"--",
"--not-a-flag",
"arg",
"--"});
299 EXPECT_TRUE(cl.has_argv0());
300 EXPECT_EQ(std::string(), cl.argv0());
301 std::vector<CommandLine::Option> expected_options = {
302 CommandLine::Option(
"flag",
"value")};
303 std::vector<std::string> expected_positional_args = {
"--not-a-flag",
"arg",
305 EXPECT_EQ(expected_positional_args, cl.positional_args());
309TEST(CommandLineTest, MultipleOccurrencesOfOption) {
311 {
"my_program",
"--flag1=value1",
"--flag2=value2",
"--flag1=value3"});
312 std::vector<CommandLine::Option> expected_options = {
313 CommandLine::Option(
"flag1",
"value1"),
314 CommandLine::Option(
"flag2",
"value2"),
315 CommandLine::Option(
"flag1",
"value3")};
316 EXPECT_EQ(
"value3", cl.GetOptionValueWithDefault(
"flag1",
"nope"));
317 EXPECT_EQ(
"value2", cl.GetOptionValueWithDefault(
"flag2",
"nope"));
318 std::vector<std::string_view> values = cl.GetOptionValues(
"flag1");
319 ASSERT_EQ(2u, values.size());
320 EXPECT_EQ(
"value1", values[0]);
321 EXPECT_EQ(
"value3", values[1]);
325void ExpectNotEqual(
const char*
message,
326 std::initializer_list<std::string> c1,
327 std::initializer_list<std::string> c2) {
334 EXPECT_TRUE(cl1 == cl1);
335 EXPECT_FALSE(cl1 != cl1);
336 EXPECT_TRUE(cl2 == cl2);
337 EXPECT_FALSE(cl2 != cl2);
340 EXPECT_FALSE(cl1 == cl2);
341 EXPECT_TRUE(cl1 != cl2);
342 EXPECT_FALSE(cl2 == cl1);
343 EXPECT_TRUE(cl2 != cl1);
346void ExpectEqual(
const char*
message,
347 std::initializer_list<std::string> c1,
348 std::initializer_list<std::string> c2) {
355 EXPECT_TRUE(cl1 == cl1);
356 EXPECT_FALSE(cl1 != cl1);
357 EXPECT_TRUE(cl2 == cl2);
358 EXPECT_FALSE(cl2 != cl2);
361 EXPECT_TRUE(cl1 == cl2);
362 EXPECT_FALSE(cl1 != cl2);
363 EXPECT_TRUE(cl2 == cl1);
364 EXPECT_FALSE(cl2 != cl1);
367TEST(CommandLineTest, ComparisonOperators) {
368 ExpectNotEqual(
"1", {}, {
""});
369 ExpectNotEqual(
"2", {
"abc"}, {
"def"});
370 ExpectNotEqual(
"3", {
"abc",
"--flag"}, {
"abc"});
371 ExpectNotEqual(
"4", {
"abc",
"--flag1"}, {
"abc",
"--flag2"});
372 ExpectNotEqual(
"5", {
"abc",
"--flag1",
"--flag2"}, {
"abc",
"--flag1"});
373 ExpectNotEqual(
"6", {
"abc",
"arg"}, {
"abc"});
374 ExpectNotEqual(
"7", {
"abc",
"arg1"}, {
"abc",
"arg2"});
375 ExpectNotEqual(
"8", {
"abc",
"arg1",
"arg2"}, {
"abc",
"arg1"});
376 ExpectNotEqual(
"9", {
"abc",
"--flag",
"arg1"}, {
"abc",
"--flag",
"arg2"});
380 ExpectEqual(
"10", {
"abc",
"--flag",
"arg"}, {
"abc",
"--flag",
"--",
"arg"});
383TEST(CommandLineTest, MoveAndCopy) {
385 {
"my_program",
"--flag1=value1",
"--flag2",
"arg"});
391 EXPECT_EQ(
"value1", cl2.GetOptionValueWithDefault(
"flag1",
"nope"));
394 CommandLine cl3(std::move(cl2));
396 EXPECT_EQ(
"value1", cl3.GetOptionValueWithDefault(
"flag1",
"nope"));
403 EXPECT_EQ(
"value1", cl4.GetOptionValueWithDefault(
"flag1",
"nope"));
408 cl5 = std::move(cl4);
410 EXPECT_EQ(
"value1", cl5.GetOptionValueWithDefault(
"flag1",
"nope"));
413void ToArgvHelper(
const char*
message, std::initializer_list<std::string> c) {
415 std::vector<std::string>
argv = c;
421 ToArgvHelper(
"1", {});
422 ToArgvHelper(
"2", {
""});
423 ToArgvHelper(
"3", {
"my_program"});
424 ToArgvHelper(
"4", {
"my_program",
"--flag"});
425 ToArgvHelper(
"5", {
"my_program",
"--flag1",
"--flag2=value"});
426 ToArgvHelper(
"6", {
"my_program",
"arg"});
427 ToArgvHelper(
"7", {
"my_program",
"arg1",
"arg2"});
428 ToArgvHelper(
"8", {
"my_program",
"--flag1",
"--flag2=value",
"arg1",
"arg2"});
429 ToArgvHelper(
"9", {
"my_program",
"--flag",
"--",
"--not-a-flag"});
430 ToArgvHelper(
"10", {
"my_program",
"--flag",
"arg",
"--"});
435 std::vector<std::string>
argv = {
"my_program"};
441 std::vector<std::string>
argv = {
"my_program",
"--flag",
"arg"};
G_BEGIN_DECLS GBytes * message
CommandLine CommandLineFromArgcArgv(int argc, const char *const *argv)
CommandLine CommandLineFromInitializerList(std::initializer_list< StringType > argv)
std::vector< std::string > CommandLineToArgv(const CommandLine &command_line)
CommandLine CommandLineFromIterators(InputIterator first, InputIterator last)
CommandLine CommandLineFromIteratorsFindFirstPositionalArg(InputIterator first, InputIterator last, InputIterator *first_positional_arg)
TEST(MallocMapping, EmptyContructor)