File: testArgumentParser.cxx

package info (click to toggle)
cmake 4.2.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 152,336 kB
  • sloc: ansic: 403,896; cpp: 303,920; sh: 4,105; python: 3,583; yacc: 3,106; lex: 1,279; f90: 538; asm: 471; lisp: 375; cs: 270; java: 266; fortran: 239; objc: 215; perl: 213; xml: 198; makefile: 111; javascript: 83; pascal: 63; tcl: 55; php: 25; ruby: 22
file content (574 lines) | stat: -rw-r--r-- 21,786 bytes parent folder | download | duplicates (2)
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
   file LICENSE.rst or https://cmake.org/licensing for details.  */

#include <map>
#include <string>
#include <utility>
#include <vector>

#include <cm/optional>
#include <cm/string_view>
#include <cmext/string_view>

#include "cmArgumentParser.h"
#include "cmArgumentParserTypes.h"

#include "testCommon.h"

namespace {
struct Result : ArgumentParser::ParseResult
{
  struct SubResult : ParseResult
  {
    ArgumentParser::NonEmpty<std::string> SubCommand;
    bool SubOption = false;
    ArgumentParser::NonEmpty<std::string> SubString;
    ArgumentParser::NonEmpty<std::vector<std::string>> SubList;
    std::vector<cm::string_view> ParsedKeywords;
  };

  bool Option1 = false;
  bool Option2 = false;

  std::string String1;
  cm::optional<std::string> String2;
  cm::optional<std::string> String3;
  ArgumentParser::Maybe<std::string> String4;
  ArgumentParser::NonEmpty<std::string> String5;
  ArgumentParser::NonEmpty<std::string> String6;

  ArgumentParser::NonEmpty<std::vector<std::string>> List1;
  ArgumentParser::NonEmpty<std::vector<std::string>> List2;
  cm::optional<ArgumentParser::NonEmpty<std::vector<std::string>>> List3;
  cm::optional<ArgumentParser::NonEmpty<std::vector<std::string>>> List4;
  cm::optional<ArgumentParser::NonEmpty<std::vector<std::string>>> List5;
  cm::optional<ArgumentParser::MaybeEmpty<std::vector<std::string>>> List6;

  std::vector<std::vector<std::string>> Multi1;
  std::vector<std::vector<std::string>> Multi2;
  cm::optional<std::vector<std::vector<std::string>>> Multi3;
  cm::optional<std::vector<std::vector<std::string>>> Multi4;

  cm::optional<std::string> Pos0;
  cm::optional<std::string> Pos1;
  cm::optional<std::string> Pos2;
  ArgumentParser::MaybeEmpty<std::vector<std::string>> TrailingPos;

  bool Func0_ = false;
  ArgumentParser::Continue Func0(cm::string_view)
  {
    Func0_ = true;
    return ArgumentParser::Continue::No;
  }

  std::string Func1_;
  ArgumentParser::Continue Func1(cm::string_view arg)
  {
    Func1_ = std::string(arg);
    return ArgumentParser::Continue::No;
  }

  std::map<std::string, std::vector<std::string>> Func2_;
  ArgumentParser::Continue Func2(cm::string_view key, cm::string_view arg)
  {
    Func2_[std::string(key)].emplace_back(arg);
    return key == "FUNC_2b" ? ArgumentParser::Continue::Yes
                            : ArgumentParser::Continue::No;
  }

  std::vector<std::string> Func3_;
  ArgumentParser::Continue Func3(cm::string_view arg)
  {
    Func3_.emplace_back(arg);
    return ArgumentParser::Continue::Yes;
  }

  std::map<std::string, std::vector<std::string>> Func4_;
  ArgumentParser::Continue Func4(cm::string_view key, cm::string_view arg)
  {
    Func4_[std::string(key)].emplace_back(arg);
    return key == "FUNC_4b" ? ArgumentParser::Continue::Yes
                            : ArgumentParser::Continue::No;
  }

  cm::optional<SubResult> Sub;
  ArgumentParser::NonEmpty<std::string> Parent;

  ArgumentParser::Maybe<std::string> UnboundMaybe{ 'u', 'n', 'b', 'o',
                                                   'u', 'n', 'd' };
  ArgumentParser::MaybeEmpty<std::vector<std::string>> UnboundMaybeEmpty{
    1, "unbound"
  };
  ArgumentParser::NonEmpty<std::vector<std::string>> UnboundNonEmpty{
    1, "unbound"
  };
  ArgumentParser::NonEmpty<std::string> UnboundNonEmptyStr{ 'u', 'n', 'b', 'o',
                                                            'u', 'n', 'd' };

  std::vector<cm::string_view> ParsedKeywords;
};

struct Derived : Result
{
};

std::initializer_list<cm::string_view> const args = {
  /* clang-format off */
  "pos0",                    // position index 0
  "OPTION_1",                // option
  "pos2",                    // position index 2, ignored because after keyword
  // "OPTION_2",             // option that is not present
  "STRING_1",                // string arg missing value
  "STRING_2", "foo", "bar",  // string arg + unparsed value, presence captured
  // "STRING_3",             // string arg that is not present
  "STRING_4",                // string arg allowed to be missing value
  "STRING_5", "foo",         // string arg that is not empty
  "STRING_6", "",            // string arg that is empty
  "LIST_1",                  // list arg missing values
  "LIST_2", "foo", "bar",    // list arg with 2 elems
  "LIST_3", "bar",           // list arg ...
  "LIST_3", "foo",           // ... with continuation
  "LIST_4",                  // list arg missing values, presence captured
  // "LIST_5",               // list arg that is not present
  "LIST_6",                  // list arg allowed to be empty
  "MULTI_2",                 // multi list with 0 lists
  "MULTI_3", "foo", "bar",   // multi list with first list with two elems
  "MULTI_3", "bar", "foo",   // multi list with second list with two elems
  // "MULTI_4",              // multi list arg that is not present
  "FUNC_0",                  // callback arg missing value
  "FUNC_1", "foo", "ign1",   // callback with one arg + unparsed value
  "FUNC_2a", "foo", "ign2",  // callback with keyword-dependent arg count
  "FUNC_2b", "bar", "zot",   // callback with keyword-dependent arg count
  "FUNC_3", "foo", "bar",    // callback with list arg ...
  "FUNC_4a", "foo", "ign4",  // callback with keyword-dependent arg count
  "FUNC_4b", "bar", "zot",   // callback with keyword-dependent arg count
  "SUB_CMD",  "foo",         // switch to subparser and set Sub::SUB_CMD to foo
  "SUB_OPTION",              // subparser option
  "SUB_STRING", "sub_value", // subparser string
  "SUB_LIST", "a", "b", "c", // subparser list
  // Return to main parser (simulate another main option if needed)
  "PARENT", "value",
};

struct ResultTrailingPos : public ArgumentParser::ParseResult
{
  bool Option1 = false;
  ArgumentParser::NonEmpty<std::vector<std::string>> List1;

  cm::optional<std::string> Pos0;
  cm::optional<std::string> Pos1;
  ArgumentParser::NonEmpty<std::vector<std::string>> TrailingPos;
};

struct DerivedTrailingPos : ResultTrailingPos
{
};

std::initializer_list<cm::string_view> const args_trailingpos = {
  /* clang-format off */
  "pos0",                    // position index 0
  "pos1",                    // position index 1
  "pos_trailing0",           // trailing positional 0
  "pos_trailing1",           // trailing positional 1
  "OPTION_1",                // option
  "LIST_1", "foo", "bar",    // list arg with 2 elems
  /* clang-format on */
};

bool verifyResult(Result const& result,
                  std::vector<std::string> const& unparsedArguments)
{
  static std::vector<std::string> const foobar = { "foo", "bar" };
  static std::vector<std::string> const barfoo = { "bar", "foo" };
  static std::vector<std::string> const unbound = { "unbound" };
  static std::vector<cm::string_view> const parsedKeywords = {
    /* clang-format off */
    "OPTION_1",
    "STRING_1",
    "STRING_2",
    "STRING_4",
    "STRING_5",
    "STRING_6",
    "LIST_1",
    "LIST_2",
    "LIST_3",
    "LIST_3",
    "LIST_4",
    "LIST_6",
    "MULTI_2",
    "MULTI_3",
    "MULTI_3",
    "FUNC_0",
    "FUNC_1",
    "FUNC_2a",
    "FUNC_2b",
    "FUNC_3",
    "FUNC_4a",
    "FUNC_4b",
    "SUB_CMD",
    "PARENT",
    /* clang-format on */
  };

  static std::vector<cm::string_view> subParsedKeywords = {
    "SUB_CMD", "SUB_OPTION", "SUB_STRING", "SUB_LIST"
  };

  static std::map<std::string, std::vector<std::string>> const func2map = {
    { "FUNC_2a", { "foo" } }, { "FUNC_2b", { "bar", "zot" } }
  };
  static std::map<std::string, std::vector<std::string>> const func4map = {
    { "FUNC_4a", { "foo" } }, { "FUNC_4b", { "bar", "zot" } }
  };
  static std::map<cm::string_view, std::string> const keywordErrors = {
    { "STRING_1"_s, "  missing required value\n" },
    { "STRING_6"_s, "  empty string not allowed\n" },
    { "LIST_1"_s, "  missing required value\n" },
    { "LIST_4"_s, "  missing required value\n" },
    { "FUNC_0"_s, "  missing required value\n" }
  };
  static std::vector<std::string> const unparsed = { "pos2", "bar", "ign1",
                                                     "ign2", "ign4" };

  ASSERT_TRUE(!result);

  ASSERT_TRUE(result.Option1);
  ASSERT_TRUE(!result.Option2);

  ASSERT_TRUE(result.String1.empty());
  ASSERT_TRUE(result.String2);
  ASSERT_TRUE(*result.String2 == "foo");
  ASSERT_TRUE(!result.String3);
  ASSERT_TRUE(result.String4.empty());
  ASSERT_TRUE(result.String5 == "foo");
  ASSERT_TRUE(result.String6.empty());

  ASSERT_TRUE(result.List1.empty());
  ASSERT_TRUE(result.List2 == foobar);
  ASSERT_TRUE(result.List3);
  ASSERT_TRUE(*result.List3 == barfoo);
  ASSERT_TRUE(result.List4);
  ASSERT_TRUE(result.List4->empty());
  ASSERT_TRUE(!result.List5);
  ASSERT_TRUE(result.List6);
  ASSERT_TRUE(result.List6->empty());

  ASSERT_TRUE(result.Multi1.empty());
  ASSERT_TRUE(result.Multi2.size() == 1);
  ASSERT_TRUE(result.Multi2[0].empty());
  ASSERT_TRUE(result.Multi3);
  ASSERT_TRUE((*result.Multi3).size() == 2);
  ASSERT_TRUE((*result.Multi3)[0] == foobar);
  ASSERT_TRUE((*result.Multi3)[1] == barfoo);
  ASSERT_TRUE(!result.Multi4);

  ASSERT_TRUE(result.Pos0 == "pos0");
  ASSERT_TRUE(!result.Pos1);
  ASSERT_TRUE(!result.Pos2);
  ASSERT_TRUE(result.TrailingPos.empty());

  ASSERT_TRUE(result.Func0_ == false);
  ASSERT_TRUE(result.Func1_ == "foo");
  ASSERT_TRUE(result.Func2_ == func2map);
  ASSERT_TRUE(result.Func3_ == foobar);
  ASSERT_TRUE(result.Func4_ == func4map);

  ASSERT_TRUE(unparsedArguments == unparsed);

  ASSERT_TRUE(result.UnboundMaybe == "unbound");
  ASSERT_TRUE(result.UnboundMaybeEmpty == unbound);
  ASSERT_TRUE(result.UnboundNonEmpty == unbound);
  ASSERT_TRUE(result.UnboundNonEmptyStr == "unbound");

  ASSERT_TRUE(result.Sub->SubCommand == "foo");
  ASSERT_TRUE(result.Sub->SubOption);
  ASSERT_TRUE(result.Sub->SubString == "sub_value");
  ASSERT_TRUE(result.Sub->SubList ==
              std::vector<std::string>({ "a", "b", "c" }));
  ASSERT_TRUE(result.Parent == "value");

  ASSERT_TRUE(result.Sub->ParsedKeywords == subParsedKeywords);
  ASSERT_TRUE(result.ParsedKeywords == parsedKeywords);

  ASSERT_TRUE(result.GetKeywordErrors().size() == keywordErrors.size());
  for (auto const& ke : result.GetKeywordErrors()) {
    auto const ki = keywordErrors.find(ke.first);
    ASSERT_TRUE(ki != keywordErrors.end());
    ASSERT_TRUE(ke.second == ki->second);
  }

  return true;
}

bool verifyResult(ResultTrailingPos const& result,
                  std::vector<std::string> const& unparsedArguments)
{
  static std::vector<std::string> const foobar = { "foo", "bar" };
  static std::vector<std::string> const trailing = { "pos_trailing0",
                                                     "pos_trailing1" };

  ASSERT_TRUE(result);
  ASSERT_TRUE(unparsedArguments.empty());

  ASSERT_TRUE(result.Option1);
  ASSERT_TRUE(result.List1 == foobar);

  ASSERT_TRUE(result.Pos0 == "pos0");
  ASSERT_TRUE(result.Pos1 == "pos1");
  ASSERT_TRUE(result.TrailingPos == trailing);

  return true;
}

bool testArgumentParserDynamic()
{
  Result result;
  result.Sub = Result::SubResult();

  std::vector<std::string> unparsedArguments;

  std::function<ArgumentParser::Continue(cm::string_view, cm::string_view)>
    func4 = [&result](cm::string_view key,
                      cm::string_view arg) -> ArgumentParser::Continue {
    return result.Func4(key, arg);
  };

  cmArgumentParser<void> parserDynamic;
  parserDynamic.Bind("OPTION_1"_s, result.Option1);
  ASSERT_TRUE(parserDynamic.HasKeyword("OPTION_1"_s));
  ASSERT_TRUE(!parserDynamic.HasKeyword("NOT_AN_OPTION"_s));

  auto subParser = cmArgumentParser<void>{}
                     .Bind("SUB_CMD"_s, result.Sub->SubCommand)
                     .Bind("SUB_OPTION"_s, result.Sub->SubOption)
                     .Bind("SUB_STRING"_s, result.Sub->SubString)
                     .Bind("SUB_LIST"_s, result.Sub->SubList)
                     .BindParsedKeywords(result.Sub->ParsedKeywords);

  static_cast<ArgumentParser::ParseResult&>(result) =
    cmArgumentParser<void>{}
      .Bind(0, result.Pos0)
      .Bind(1, result.Pos1)
      .Bind(2, result.Pos2)
      .BindTrailingArgs(result.TrailingPos)
      .Bind("OPTION_1"_s, result.Option1)
      .Bind("OPTION_2"_s, result.Option2)
      .Bind("STRING_1"_s, result.String1)
      .Bind("STRING_2"_s, result.String2)
      .Bind("STRING_3"_s, result.String3)
      .Bind("STRING_4"_s, result.String4)
      .Bind("STRING_5"_s, result.String5)
      .Bind("STRING_6"_s, result.String6)
      .Bind("LIST_1"_s, result.List1)
      .Bind("LIST_2"_s, result.List2)
      .Bind("LIST_3"_s, result.List3)
      .Bind("LIST_4"_s, result.List4)
      .Bind("LIST_5"_s, result.List5)
      .Bind("LIST_6"_s, result.List6)
      .Bind("MULTI_1"_s, result.Multi1)
      .Bind("MULTI_2"_s, result.Multi2)
      .Bind("MULTI_3"_s, result.Multi3)
      .Bind("MULTI_4"_s, result.Multi4)
      .Bind("FUNC_0"_s,
            [&result](cm::string_view arg) -> ArgumentParser::Continue {
              return result.Func0(arg);
            })
      .Bind("FUNC_1"_s,
            [&result](cm::string_view arg) -> ArgumentParser::Continue {
              return result.Func1(arg);
            })
      .Bind("FUNC_2a"_s,
            [&result](cm::string_view key, cm::string_view arg)
              -> ArgumentParser::Continue { return result.Func2(key, arg); })
      .Bind("FUNC_2b"_s,
            [&result](cm::string_view key, cm::string_view arg)
              -> ArgumentParser::Continue { return result.Func2(key, arg); })
      .Bind("FUNC_3"_s,
            [&result](cm::string_view arg) -> ArgumentParser::Continue {
              return result.Func3(arg);
            })
      .Bind("FUNC_4a"_s, func4)
      .Bind("FUNC_4b"_s, func4)
      .BindSubParser("SUB_CMD"_s, subParser, result.Sub)
      .Bind("PARENT"_s, result.Parent)
      .BindParsedKeywords(result.ParsedKeywords)
      .Parse(args, &unparsedArguments);

  if (!verifyResult(result, unparsedArguments)) {
    return false;
  }

  unparsedArguments.clear();

  ResultTrailingPos result_trailing;
  static_cast<ArgumentParser::ParseResult&>(result_trailing) =
    cmArgumentParser<void>{}
      .Bind(0, result_trailing.Pos0)
      .Bind(1, result_trailing.Pos1)
      .BindTrailingArgs(result_trailing.TrailingPos)
      .Bind("OPTION_1"_s, result_trailing.Option1)
      .Bind("LIST_1"_s, result_trailing.List1)
      .Parse(args_trailingpos, &unparsedArguments);

  return verifyResult(result_trailing, unparsedArguments);
}

static auto const parserStaticFunc4 =
  [](Result& result, cm::string_view key,
     cm::string_view arg) -> ArgumentParser::Continue {
  return result.Func4(key, arg);
};

#define BIND_ALL(name, resultType)                                            \
  /* Define the sub-parser first */                                           \
  static auto sub##name =                                                     \
    cmArgumentParser<resultType::SubResult>{}                                 \
      .Bind("SUB_CMD"_s, &resultType::SubResult::SubCommand)                  \
      .Bind("SUB_OPTION"_s, &resultType::SubResult::SubOption)                \
      .Bind("SUB_STRING"_s, &resultType::SubResult::SubString)                \
      .Bind("SUB_LIST"_s, &resultType::SubResult::SubList)                    \
      .BindParsedKeywords(&resultType::SubResult::ParsedKeywords);            \
                                                                              \
  /* Define the main parser, which uses the sub-parser */                     \
  static const auto name =                                                    \
    cmArgumentParser<resultType>{}                                            \
      .Bind(0, &resultType::Pos0)                                             \
      .Bind(1, &resultType::Pos1)                                             \
      .Bind(2, &resultType::Pos2)                                             \
      .BindTrailingArgs(&resultType::TrailingPos)                             \
      .Bind("OPTION_1"_s, &resultType::Option1)                               \
      .Bind("OPTION_2"_s, &resultType::Option2)                               \
      .Bind("STRING_1"_s, &resultType::String1)                               \
      .Bind("STRING_2"_s, &resultType::String2)                               \
      .Bind("STRING_3"_s, &resultType::String3)                               \
      .Bind("STRING_4"_s, &resultType::String4)                               \
      .Bind("STRING_5"_s, &resultType::String5)                               \
      .Bind("STRING_6"_s, &resultType::String6)                               \
      .Bind("LIST_1"_s, &resultType::List1)                                   \
      .Bind("LIST_2"_s, &resultType::List2)                                   \
      .Bind("LIST_3"_s, &resultType::List3)                                   \
      .Bind("LIST_4"_s, &resultType::List4)                                   \
      .Bind("LIST_5"_s, &resultType::List5)                                   \
      .Bind("LIST_6"_s, &resultType::List6)                                   \
      .Bind("MULTI_1"_s, &resultType::Multi1)                                 \
      .Bind("MULTI_2"_s, &resultType::Multi2)                                 \
      .Bind("MULTI_3"_s, &resultType::Multi3)                                 \
      .Bind("MULTI_4"_s, &resultType::Multi4)                                 \
      .Bind("FUNC_0"_s, &resultType::Func0)                                   \
      .Bind("FUNC_1"_s, &resultType::Func1)                                   \
      .Bind("FUNC_2a"_s, &resultType::Func2)                                  \
      .Bind("FUNC_2b"_s, &resultType::Func2)                                  \
      .Bind("FUNC_3"_s,                                                       \
            [](resultType& result, cm::string_view arg)                       \
              -> ArgumentParser::Continue { return result.Func3(arg); })      \
      .Bind("FUNC_4a"_s, parserStaticFunc4)                                   \
      .Bind("FUNC_4b"_s, parserStaticFunc4)                                   \
      .BindSubParser("SUB_CMD"_s, sub##name, &resultType::Sub)                \
      .Bind("PARENT"_s, &resultType::Parent)                                  \
      .BindParsedKeywords(&resultType::ParsedKeywords);

BIND_ALL(parserStatic, Result);
BIND_ALL(parserDerivedStatic, Derived);

#define BIND_TRAILING(name, resultType)                                       \
  static auto const name = cmArgumentParser<resultType>{}                     \
                             .Bind(0, &resultType::Pos0)                      \
                             .Bind(1, &resultType::Pos1)                      \
                             .BindTrailingArgs(&resultType::TrailingPos)      \
                             .Bind("OPTION_1"_s, &resultType::Option1)        \
                             .Bind("LIST_1"_s, &resultType::List1)

BIND_TRAILING(parserTrailingStatic, ResultTrailingPos);
BIND_TRAILING(parserTrailingDerivedStatic, DerivedTrailingPos);

bool testArgumentParserStatic()
{
  ASSERT_TRUE(parserStatic.HasKeyword("OPTION_1"_s));
  ASSERT_TRUE(!parserStatic.HasKeyword("NOT_AN_OPTION"_s));

  std::vector<std::string> unparsedArguments;
  Result const result = parserStatic.Parse(args, &unparsedArguments);
  if (!verifyResult(result, unparsedArguments)) {
    return false;
  }

  unparsedArguments.clear();
  ResultTrailingPos const result_trailing =
    parserTrailingStatic.Parse(args_trailingpos, &unparsedArguments);
  return verifyResult(result_trailing, unparsedArguments);
}

bool testArgumentParserDerivedStatic()
{
  ASSERT_TRUE(parserDerivedStatic.HasKeyword("OPTION_1"_s));
  ASSERT_TRUE(!parserDerivedStatic.HasKeyword("NOT_AN_OPTION"_s));

  std::vector<std::string> unparsedArguments;
  Derived const result = parserDerivedStatic.Parse(args, &unparsedArguments);
  if (!verifyResult(result, unparsedArguments)) {
    return false;
  }

  unparsedArguments.clear();
  ResultTrailingPos const result_trailing =
    parserTrailingDerivedStatic.Parse(args_trailingpos, &unparsedArguments);
  return verifyResult(result_trailing, unparsedArguments);
}

bool testArgumentParserStaticBool()
{
  std::vector<std::string> unparsedArguments;
  Result result;
  ASSERT_TRUE(parserStatic.Parse(result, args, &unparsedArguments) == false);
  return verifyResult(result, unparsedArguments);
}

bool testArgumentParserTypes()
{
  ArgumentParser::Maybe<std::string> maybeString;
  maybeString = std::string();
  maybeString = "";

  ArgumentParser::MaybeEmpty<std::vector<std::string>> maybeEmptyVecStr;
  maybeEmptyVecStr = std::vector<std::string>{};

  ArgumentParser::NonEmpty<std::string> nonEmptyString;
  nonEmptyString = std::string("x");
  nonEmptyString = "x";

  ArgumentParser::NonEmpty<std::vector<std::string>> nonEmptyVecStr;
  nonEmptyVecStr = std::vector<std::string>{ "" };
  return true;
}

}

int testArgumentParser(int /*unused*/, char* /*unused*/[])
{
  if (!testArgumentParserDynamic()) {
    std::cout << "While executing testArgumentParserDynamic().\n";
    return -1;
  }

  if (!testArgumentParserStatic()) {
    std::cout << "While executing testArgumentParserStatic().\n";
    return -1;
  }

  if (!testArgumentParserDerivedStatic()) {
    std::cout << "While executing testArgumentParserDerivedStatic().\n";
    return -1;
  }

  if (!testArgumentParserStaticBool()) {
    std::cout << "While executing testArgumentParserStaticBool().\n";
    return -1;
  }

  if (!testArgumentParserTypes()) {
    std::cout << "While executing testArgumentParserTypes().\n";
    return -1;
  }

  return 0;
}