testArgumentParser.cxx 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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/optional>
  8. #include <cm/string_view>
  9. #include <cmext/string_view>
  10. #include "cmArgumentParser.h"
  11. #include "cmArgumentParserTypes.h"
  12. namespace {
  13. struct Result : public ArgumentParser::ParseResult
  14. {
  15. bool Option1 = false;
  16. bool Option2 = false;
  17. std::string String1;
  18. cm::optional<std::string> String2;
  19. cm::optional<std::string> String3;
  20. ArgumentParser::Maybe<std::string> String4;
  21. ArgumentParser::NonEmpty<std::vector<std::string>> List1;
  22. ArgumentParser::NonEmpty<std::vector<std::string>> List2;
  23. cm::optional<ArgumentParser::NonEmpty<std::vector<std::string>>> List3;
  24. cm::optional<ArgumentParser::NonEmpty<std::vector<std::string>>> List4;
  25. cm::optional<ArgumentParser::NonEmpty<std::vector<std::string>>> List5;
  26. cm::optional<ArgumentParser::MaybeEmpty<std::vector<std::string>>> List6;
  27. std::vector<std::vector<std::string>> Multi1;
  28. std::vector<std::vector<std::string>> Multi2;
  29. cm::optional<std::vector<std::vector<std::string>>> Multi3;
  30. cm::optional<std::vector<std::vector<std::string>>> Multi4;
  31. };
  32. std::initializer_list<cm::string_view> const args = {
  33. /* clang-format off */
  34. "OPTION_1", // option
  35. // "OPTION_2", // option that is not present
  36. "STRING_1", // string arg missing value
  37. "STRING_2", "foo", "bar", // string arg + unparsed value, presence captured
  38. // "STRING_3", // string arg that is not present
  39. "STRING_4", // string arg allowed to be missing value
  40. "LIST_1", // list arg missing values
  41. "LIST_2", "foo", "bar", // list arg with 2 elems
  42. "LIST_3", "bar", // list arg ...
  43. "LIST_3", "foo", // ... with continuation
  44. "LIST_4", // list arg missing values, presence captured
  45. // "LIST_5", // list arg that is not present
  46. "LIST_6", // list arg allowed to be empty
  47. "MULTI_2", // multi list with 0 lists
  48. "MULTI_3", "foo", "bar", // multi list with first list with two elems
  49. "MULTI_3", "bar", "foo", // multi list with second list with two elems
  50. // "MULTI_4", // multi list arg that is not present
  51. /* clang-format on */
  52. };
  53. bool verifyResult(Result const& result,
  54. std::vector<std::string> const& unparsedArguments,
  55. std::vector<cm::string_view> const& keywordsMissingValue)
  56. {
  57. static std::vector<std::string> const foobar = { "foo", "bar" };
  58. static std::vector<std::string> const barfoo = { "bar", "foo" };
  59. static std::vector<cm::string_view> const missing = { "STRING_1"_s,
  60. "LIST_1"_s,
  61. "LIST_4"_s };
  62. #define ASSERT_TRUE(x) \
  63. do { \
  64. if (!(x)) { \
  65. std::cout << "ASSERT_TRUE(" #x ") failed on line " << __LINE__ << "\n"; \
  66. return false; \
  67. } \
  68. } while (false)
  69. ASSERT_TRUE(result);
  70. ASSERT_TRUE(result.Option1);
  71. ASSERT_TRUE(!result.Option2);
  72. ASSERT_TRUE(result.String1.empty());
  73. ASSERT_TRUE(result.String2);
  74. ASSERT_TRUE(*result.String2 == "foo");
  75. ASSERT_TRUE(!result.String3);
  76. ASSERT_TRUE(result.String4.empty());
  77. ASSERT_TRUE(result.List1.empty());
  78. ASSERT_TRUE(result.List2 == foobar);
  79. ASSERT_TRUE(result.List3);
  80. ASSERT_TRUE(*result.List3 == barfoo);
  81. ASSERT_TRUE(result.List4);
  82. ASSERT_TRUE(result.List4->empty());
  83. ASSERT_TRUE(!result.List5);
  84. ASSERT_TRUE(result.List6);
  85. ASSERT_TRUE(result.List6->empty());
  86. ASSERT_TRUE(result.Multi1.empty());
  87. ASSERT_TRUE(result.Multi2.size() == 1);
  88. ASSERT_TRUE(result.Multi2[0].empty());
  89. ASSERT_TRUE(result.Multi3);
  90. ASSERT_TRUE((*result.Multi3).size() == 2);
  91. ASSERT_TRUE((*result.Multi3)[0] == foobar);
  92. ASSERT_TRUE((*result.Multi3)[1] == barfoo);
  93. ASSERT_TRUE(!result.Multi4);
  94. ASSERT_TRUE(unparsedArguments.size() == 1);
  95. ASSERT_TRUE(unparsedArguments[0] == "bar");
  96. ASSERT_TRUE(keywordsMissingValue == missing);
  97. return true;
  98. }
  99. bool testArgumentParserDynamic()
  100. {
  101. Result result;
  102. std::vector<std::string> unparsedArguments;
  103. std::vector<cm::string_view> keywordsMissingValue;
  104. static_cast<ArgumentParser::ParseResult&>(result) =
  105. cmArgumentParser<void>{}
  106. .Bind("OPTION_1"_s, result.Option1)
  107. .Bind("OPTION_2"_s, result.Option2)
  108. .Bind("STRING_1"_s, result.String1)
  109. .Bind("STRING_2"_s, result.String2)
  110. .Bind("STRING_3"_s, result.String3)
  111. .Bind("STRING_4"_s, result.String4)
  112. .Bind("LIST_1"_s, result.List1)
  113. .Bind("LIST_2"_s, result.List2)
  114. .Bind("LIST_3"_s, result.List3)
  115. .Bind("LIST_4"_s, result.List4)
  116. .Bind("LIST_5"_s, result.List5)
  117. .Bind("LIST_6"_s, result.List6)
  118. .Bind("MULTI_1"_s, result.Multi1)
  119. .Bind("MULTI_2"_s, result.Multi2)
  120. .Bind("MULTI_3"_s, result.Multi3)
  121. .Bind("MULTI_4"_s, result.Multi4)
  122. .Parse(args, &unparsedArguments, &keywordsMissingValue);
  123. return verifyResult(result, unparsedArguments, keywordsMissingValue);
  124. }
  125. static auto const parserStatic = //
  126. cmArgumentParser<Result>{}
  127. .Bind("OPTION_1"_s, &Result::Option1)
  128. .Bind("OPTION_2"_s, &Result::Option2)
  129. .Bind("STRING_1"_s, &Result::String1)
  130. .Bind("STRING_2"_s, &Result::String2)
  131. .Bind("STRING_3"_s, &Result::String3)
  132. .Bind("STRING_4"_s, &Result::String4)
  133. .Bind("LIST_1"_s, &Result::List1)
  134. .Bind("LIST_2"_s, &Result::List2)
  135. .Bind("LIST_3"_s, &Result::List3)
  136. .Bind("LIST_4"_s, &Result::List4)
  137. .Bind("LIST_5"_s, &Result::List5)
  138. .Bind("LIST_6"_s, &Result::List6)
  139. .Bind("MULTI_1"_s, &Result::Multi1)
  140. .Bind("MULTI_2"_s, &Result::Multi2)
  141. .Bind("MULTI_3"_s, &Result::Multi3)
  142. .Bind("MULTI_4"_s, &Result::Multi4)
  143. /* keep semicolon on own line */;
  144. bool testArgumentParserStatic()
  145. {
  146. std::vector<std::string> unparsedArguments;
  147. std::vector<cm::string_view> keywordsMissingValue;
  148. Result const result =
  149. parserStatic.Parse(args, &unparsedArguments, &keywordsMissingValue);
  150. return verifyResult(result, unparsedArguments, keywordsMissingValue);
  151. }
  152. bool testArgumentParserStaticBool()
  153. {
  154. std::vector<std::string> unparsedArguments;
  155. std::vector<cm::string_view> keywordsMissingValue;
  156. Result result;
  157. ASSERT_TRUE(parserStatic.Parse(result, args, &unparsedArguments,
  158. &keywordsMissingValue) == true);
  159. return verifyResult(result, unparsedArguments, keywordsMissingValue);
  160. }
  161. } // namespace
  162. int testArgumentParser(int /*unused*/, char* /*unused*/ [])
  163. {
  164. if (!testArgumentParserDynamic()) {
  165. std::cout << "While executing testArgumentParserDynamic().\n";
  166. return -1;
  167. }
  168. if (!testArgumentParserStatic()) {
  169. std::cout << "While executing testArgumentParserStatic().\n";
  170. return -1;
  171. }
  172. if (!testArgumentParserStaticBool()) {
  173. std::cout << "While executing testArgumentParserStaticBool().\n";
  174. return -1;
  175. }
  176. return 0;
  177. }