testArgumentParser.cxx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include <initializer_list>
  4. #include <iostream>
  5. #include <string>
  6. #include <vector>
  7. #include <cm/string_view>
  8. #include <cmext/string_view>
  9. #include "cmArgumentParser.h"
  10. namespace {
  11. struct Result
  12. {
  13. bool Option1 = false;
  14. bool Option2 = false;
  15. std::string String1;
  16. std::string String2;
  17. std::vector<std::string> List1;
  18. std::vector<std::string> List2;
  19. std::vector<std::string> List3;
  20. std::vector<std::vector<std::string>> Multi1;
  21. std::vector<std::vector<std::string>> Multi2;
  22. std::vector<std::vector<std::string>> Multi3;
  23. };
  24. std::initializer_list<cm::string_view> const args = {
  25. /* clang-format off */
  26. "OPTION_1", // option
  27. "STRING_1", // string arg missing value
  28. "STRING_2", "foo", "bar", // string arg + unparsed value
  29. "LIST_1", // list arg missing values
  30. "LIST_2", "foo", "bar", // list arg with 2 elems
  31. "LIST_3", "bar", // list arg ...
  32. "LIST_3", "foo", // ... with continuation
  33. "MULTI_2", // multi list with 0 lists
  34. "MULTI_3", "foo", "bar", // multi list with first list with two elems
  35. "MULTI_3", "bar", "foo", // multi list with second list with two elems
  36. /* clang-format on */
  37. };
  38. bool verifyResult(Result const& result,
  39. std::vector<std::string> const& unparsedArguments,
  40. std::vector<std::string> const& keywordsMissingValue)
  41. {
  42. static std::vector<std::string> const foobar = { "foo", "bar" };
  43. static std::vector<std::string> const barfoo = { "bar", "foo" };
  44. static std::vector<std::string> const missing = { "STRING_1", "LIST_1" };
  45. #define ASSERT_TRUE(x) \
  46. do { \
  47. if (!(x)) { \
  48. std::cout << "ASSERT_TRUE(" #x ") failed on line " << __LINE__ << "\n"; \
  49. return false; \
  50. } \
  51. } while (false)
  52. ASSERT_TRUE(result.Option1);
  53. ASSERT_TRUE(!result.Option2);
  54. ASSERT_TRUE(result.String1.empty());
  55. ASSERT_TRUE(result.String2 == "foo");
  56. ASSERT_TRUE(result.List1.empty());
  57. ASSERT_TRUE(result.List2 == foobar);
  58. ASSERT_TRUE(result.List3 == barfoo);
  59. ASSERT_TRUE(result.Multi1.empty());
  60. ASSERT_TRUE(result.Multi2.size() == 1);
  61. ASSERT_TRUE(result.Multi2[0].empty());
  62. ASSERT_TRUE(result.Multi3.size() == 2);
  63. ASSERT_TRUE(result.Multi3[0] == foobar);
  64. ASSERT_TRUE(result.Multi3[1] == barfoo);
  65. ASSERT_TRUE(unparsedArguments.size() == 1);
  66. ASSERT_TRUE(unparsedArguments[0] == "bar");
  67. ASSERT_TRUE(keywordsMissingValue == missing);
  68. return true;
  69. }
  70. bool testArgumentParserDynamic()
  71. {
  72. Result result;
  73. std::vector<std::string> unparsedArguments;
  74. std::vector<std::string> keywordsMissingValue;
  75. cmArgumentParser<void>{}
  76. .Bind("OPTION_1"_s, result.Option1)
  77. .Bind("OPTION_2"_s, result.Option2)
  78. .Bind("STRING_1"_s, result.String1)
  79. .Bind("STRING_2"_s, result.String2)
  80. .Bind("LIST_1"_s, result.List1)
  81. .Bind("LIST_2"_s, result.List2)
  82. .Bind("LIST_3"_s, result.List3)
  83. .Bind("MULTI_1"_s, result.Multi1)
  84. .Bind("MULTI_2"_s, result.Multi2)
  85. .Bind("MULTI_3"_s, result.Multi3)
  86. .Parse(args, &unparsedArguments, &keywordsMissingValue);
  87. return verifyResult(result, unparsedArguments, keywordsMissingValue);
  88. }
  89. bool testArgumentParserStatic()
  90. {
  91. static auto const parser = //
  92. cmArgumentParser<Result>{}
  93. .Bind("OPTION_1"_s, &Result::Option1)
  94. .Bind("OPTION_2"_s, &Result::Option2)
  95. .Bind("STRING_1"_s, &Result::String1)
  96. .Bind("STRING_2"_s, &Result::String2)
  97. .Bind("LIST_1"_s, &Result::List1)
  98. .Bind("LIST_2"_s, &Result::List2)
  99. .Bind("LIST_3"_s, &Result::List3)
  100. .Bind("MULTI_1"_s, &Result::Multi1)
  101. .Bind("MULTI_2"_s, &Result::Multi2)
  102. .Bind("MULTI_3"_s, &Result::Multi3);
  103. std::vector<std::string> unparsedArguments;
  104. std::vector<std::string> keywordsMissingValue;
  105. Result const result =
  106. parser.Parse(args, &unparsedArguments, &keywordsMissingValue);
  107. return verifyResult(result, unparsedArguments, keywordsMissingValue);
  108. }
  109. } // namespace
  110. int testArgumentParser(int /*unused*/, char* /*unused*/ [])
  111. {
  112. if (!testArgumentParserDynamic()) {
  113. std::cout << "While executing testArgumentParserDynamic().\n";
  114. return -1;
  115. }
  116. if (!testArgumentParserStatic()) {
  117. std::cout << "While executing testArgumentParserStatic().\n";
  118. return -1;
  119. }
  120. return 0;
  121. }