1
0

testArgumentParser.cxx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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 <functional>
  4. #include <initializer_list>
  5. #include <iostream>
  6. #include <map>
  7. #include <string>
  8. #include <utility>
  9. #include <vector>
  10. #include <cm/optional>
  11. #include <cm/string_view>
  12. #include <cmext/string_view>
  13. #include "cmArgumentParser.h"
  14. #include "cmArgumentParserTypes.h"
  15. namespace {
  16. struct Result : public ArgumentParser::ParseResult
  17. {
  18. bool Option1 = false;
  19. bool Option2 = false;
  20. std::string String1;
  21. cm::optional<std::string> String2;
  22. cm::optional<std::string> String3;
  23. ArgumentParser::Maybe<std::string> String4;
  24. ArgumentParser::NonEmpty<std::vector<std::string>> List1;
  25. ArgumentParser::NonEmpty<std::vector<std::string>> List2;
  26. cm::optional<ArgumentParser::NonEmpty<std::vector<std::string>>> List3;
  27. cm::optional<ArgumentParser::NonEmpty<std::vector<std::string>>> List4;
  28. cm::optional<ArgumentParser::NonEmpty<std::vector<std::string>>> List5;
  29. cm::optional<ArgumentParser::MaybeEmpty<std::vector<std::string>>> List6;
  30. std::vector<std::vector<std::string>> Multi1;
  31. std::vector<std::vector<std::string>> Multi2;
  32. cm::optional<std::vector<std::vector<std::string>>> Multi3;
  33. cm::optional<std::vector<std::vector<std::string>>> Multi4;
  34. bool Func0_ = false;
  35. ArgumentParser::Continue Func0(cm::string_view)
  36. {
  37. Func0_ = true;
  38. return ArgumentParser::Continue::No;
  39. }
  40. std::string Func1_;
  41. ArgumentParser::Continue Func1(cm::string_view arg)
  42. {
  43. Func1_ = std::string(arg);
  44. return ArgumentParser::Continue::No;
  45. }
  46. std::map<std::string, std::vector<std::string>> Func2_;
  47. ArgumentParser::Continue Func2(cm::string_view key, cm::string_view arg)
  48. {
  49. Func2_[std::string(key)].emplace_back(arg);
  50. return key == "FUNC_2b" ? ArgumentParser::Continue::Yes
  51. : ArgumentParser::Continue::No;
  52. }
  53. std::vector<std::string> Func3_;
  54. ArgumentParser::Continue Func3(cm::string_view arg)
  55. {
  56. Func3_.emplace_back(arg);
  57. return ArgumentParser::Continue::Yes;
  58. }
  59. std::map<std::string, std::vector<std::string>> Func4_;
  60. ArgumentParser::Continue Func4(cm::string_view key, cm::string_view arg)
  61. {
  62. Func4_[std::string(key)].emplace_back(arg);
  63. return key == "FUNC_4b" ? ArgumentParser::Continue::Yes
  64. : ArgumentParser::Continue::No;
  65. }
  66. ArgumentParser::Maybe<std::string> UnboundMaybe{ 'u', 'n', 'b', 'o',
  67. 'u', 'n', 'd' };
  68. ArgumentParser::MaybeEmpty<std::vector<std::string>> UnboundMaybeEmpty{
  69. 1, "unbound"
  70. };
  71. ArgumentParser::NonEmpty<std::vector<std::string>> UnboundNonEmpty{
  72. 1, "unbound"
  73. };
  74. std::vector<cm::string_view> ParsedKeywords;
  75. };
  76. std::initializer_list<cm::string_view> const args = {
  77. /* clang-format off */
  78. "OPTION_1", // option
  79. // "OPTION_2", // option that is not present
  80. "STRING_1", // string arg missing value
  81. "STRING_2", "foo", "bar", // string arg + unparsed value, presence captured
  82. // "STRING_3", // string arg that is not present
  83. "STRING_4", // string arg allowed to be missing value
  84. "LIST_1", // list arg missing values
  85. "LIST_2", "foo", "bar", // list arg with 2 elems
  86. "LIST_3", "bar", // list arg ...
  87. "LIST_3", "foo", // ... with continuation
  88. "LIST_4", // list arg missing values, presence captured
  89. // "LIST_5", // list arg that is not present
  90. "LIST_6", // list arg allowed to be empty
  91. "MULTI_2", // multi list with 0 lists
  92. "MULTI_3", "foo", "bar", // multi list with first list with two elems
  93. "MULTI_3", "bar", "foo", // multi list with second list with two elems
  94. // "MULTI_4", // multi list arg that is not present
  95. "FUNC_0", // callback arg missing value
  96. "FUNC_1", "foo", "ign1", // callback with one arg + unparsed value
  97. "FUNC_2a", "foo", "ign2", // callback with keyword-dependent arg count
  98. "FUNC_2b", "bar", "zot", // callback with keyword-dependent arg count
  99. "FUNC_3", "foo", "bar", // callback with list arg ...
  100. "FUNC_4a", "foo", "ign4", // callback with keyword-dependent arg count
  101. "FUNC_4b", "bar", "zot", // callback with keyword-dependent arg count
  102. /* clang-format on */
  103. };
  104. bool verifyResult(Result const& result,
  105. std::vector<std::string> const& unparsedArguments)
  106. {
  107. static std::vector<std::string> const foobar = { "foo", "bar" };
  108. static std::vector<std::string> const barfoo = { "bar", "foo" };
  109. static std::vector<std::string> const unbound = { "unbound" };
  110. static std::vector<cm::string_view> const parsedKeywords = {
  111. /* clang-format off */
  112. "OPTION_1",
  113. "STRING_1",
  114. "STRING_2",
  115. "STRING_4",
  116. "LIST_1",
  117. "LIST_2",
  118. "LIST_3",
  119. "LIST_3",
  120. "LIST_4",
  121. "LIST_6",
  122. "MULTI_2",
  123. "MULTI_3",
  124. "MULTI_3",
  125. "FUNC_0",
  126. "FUNC_1",
  127. "FUNC_2a",
  128. "FUNC_2b",
  129. "FUNC_3",
  130. "FUNC_4a",
  131. "FUNC_4b",
  132. /* clang-format on */
  133. };
  134. static std::map<std::string, std::vector<std::string>> const func2map = {
  135. { "FUNC_2a", { "foo" } }, { "FUNC_2b", { "bar", "zot" } }
  136. };
  137. static std::map<std::string, std::vector<std::string>> const func4map = {
  138. { "FUNC_4a", { "foo" } }, { "FUNC_4b", { "bar", "zot" } }
  139. };
  140. static std::map<cm::string_view, std::string> const keywordErrors = {
  141. { "STRING_1"_s, " missing required value\n" },
  142. { "LIST_1"_s, " missing required value\n" },
  143. { "LIST_4"_s, " missing required value\n" },
  144. { "FUNC_0"_s, " missing required value\n" }
  145. };
  146. static std::vector<std::string> const unparsed = { "bar", "ign1", "ign2",
  147. "ign4" };
  148. #define ASSERT_TRUE(x) \
  149. do { \
  150. if (!(x)) { \
  151. std::cout << "ASSERT_TRUE(" #x ") failed on line " << __LINE__ << "\n"; \
  152. return false; \
  153. } \
  154. } while (false)
  155. ASSERT_TRUE(!result);
  156. ASSERT_TRUE(result.Option1);
  157. ASSERT_TRUE(!result.Option2);
  158. ASSERT_TRUE(result.String1.empty());
  159. ASSERT_TRUE(result.String2);
  160. ASSERT_TRUE(*result.String2 == "foo");
  161. ASSERT_TRUE(!result.String3);
  162. ASSERT_TRUE(result.String4.empty());
  163. ASSERT_TRUE(result.List1.empty());
  164. ASSERT_TRUE(result.List2 == foobar);
  165. ASSERT_TRUE(result.List3);
  166. ASSERT_TRUE(*result.List3 == barfoo);
  167. ASSERT_TRUE(result.List4);
  168. ASSERT_TRUE(result.List4->empty());
  169. ASSERT_TRUE(!result.List5);
  170. ASSERT_TRUE(result.List6);
  171. ASSERT_TRUE(result.List6->empty());
  172. ASSERT_TRUE(result.Multi1.empty());
  173. ASSERT_TRUE(result.Multi2.size() == 1);
  174. ASSERT_TRUE(result.Multi2[0].empty());
  175. ASSERT_TRUE(result.Multi3);
  176. ASSERT_TRUE((*result.Multi3).size() == 2);
  177. ASSERT_TRUE((*result.Multi3)[0] == foobar);
  178. ASSERT_TRUE((*result.Multi3)[1] == barfoo);
  179. ASSERT_TRUE(!result.Multi4);
  180. ASSERT_TRUE(result.Func0_ == false);
  181. ASSERT_TRUE(result.Func1_ == "foo");
  182. ASSERT_TRUE(result.Func2_ == func2map);
  183. ASSERT_TRUE(result.Func3_ == foobar);
  184. ASSERT_TRUE(result.Func4_ == func4map);
  185. ASSERT_TRUE(unparsedArguments == unparsed);
  186. ASSERT_TRUE(result.UnboundMaybe == "unbound");
  187. ASSERT_TRUE(result.UnboundMaybeEmpty == unbound);
  188. ASSERT_TRUE(result.UnboundNonEmpty == unbound);
  189. ASSERT_TRUE(result.ParsedKeywords == parsedKeywords);
  190. ASSERT_TRUE(result.GetKeywordErrors().size() == keywordErrors.size());
  191. for (auto const& ke : result.GetKeywordErrors()) {
  192. auto const ki = keywordErrors.find(ke.first);
  193. ASSERT_TRUE(ki != keywordErrors.end());
  194. ASSERT_TRUE(ke.second == ki->second);
  195. }
  196. return true;
  197. }
  198. bool testArgumentParserDynamic()
  199. {
  200. Result result;
  201. std::vector<std::string> unparsedArguments;
  202. std::function<ArgumentParser::Continue(cm::string_view, cm::string_view)>
  203. func4 = [&result](cm::string_view key,
  204. cm::string_view arg) -> ArgumentParser::Continue {
  205. return result.Func4(key, arg);
  206. };
  207. static_cast<ArgumentParser::ParseResult&>(result) =
  208. cmArgumentParser<void>{}
  209. .Bind("OPTION_1"_s, result.Option1)
  210. .Bind("OPTION_2"_s, result.Option2)
  211. .Bind("STRING_1"_s, result.String1)
  212. .Bind("STRING_2"_s, result.String2)
  213. .Bind("STRING_3"_s, result.String3)
  214. .Bind("STRING_4"_s, result.String4)
  215. .Bind("LIST_1"_s, result.List1)
  216. .Bind("LIST_2"_s, result.List2)
  217. .Bind("LIST_3"_s, result.List3)
  218. .Bind("LIST_4"_s, result.List4)
  219. .Bind("LIST_5"_s, result.List5)
  220. .Bind("LIST_6"_s, result.List6)
  221. .Bind("MULTI_1"_s, result.Multi1)
  222. .Bind("MULTI_2"_s, result.Multi2)
  223. .Bind("MULTI_3"_s, result.Multi3)
  224. .Bind("MULTI_4"_s, result.Multi4)
  225. .Bind("FUNC_0"_s,
  226. [&result](cm::string_view arg) -> ArgumentParser::Continue {
  227. return result.Func0(arg);
  228. })
  229. .Bind("FUNC_1"_s,
  230. [&result](cm::string_view arg) -> ArgumentParser::Continue {
  231. return result.Func1(arg);
  232. })
  233. .Bind("FUNC_2a"_s,
  234. [&result](cm::string_view key, cm::string_view arg)
  235. -> ArgumentParser::Continue { return result.Func2(key, arg); })
  236. .Bind("FUNC_2b"_s,
  237. [&result](cm::string_view key, cm::string_view arg)
  238. -> ArgumentParser::Continue { return result.Func2(key, arg); })
  239. .Bind("FUNC_3"_s,
  240. [&result](cm::string_view arg) -> ArgumentParser::Continue {
  241. return result.Func3(arg);
  242. })
  243. .Bind("FUNC_4a"_s, func4)
  244. .Bind("FUNC_4b"_s, func4)
  245. .BindParsedKeywords(result.ParsedKeywords)
  246. .Parse(args, &unparsedArguments);
  247. return verifyResult(result, unparsedArguments);
  248. }
  249. static auto const parserStaticFunc4 =
  250. [](Result& result, cm::string_view key,
  251. cm::string_view arg) -> ArgumentParser::Continue {
  252. return result.Func4(key, arg);
  253. };
  254. static auto const parserStatic = //
  255. cmArgumentParser<Result>{}
  256. .Bind("OPTION_1"_s, &Result::Option1)
  257. .Bind("OPTION_2"_s, &Result::Option2)
  258. .Bind("STRING_1"_s, &Result::String1)
  259. .Bind("STRING_2"_s, &Result::String2)
  260. .Bind("STRING_3"_s, &Result::String3)
  261. .Bind("STRING_4"_s, &Result::String4)
  262. .Bind("LIST_1"_s, &Result::List1)
  263. .Bind("LIST_2"_s, &Result::List2)
  264. .Bind("LIST_3"_s, &Result::List3)
  265. .Bind("LIST_4"_s, &Result::List4)
  266. .Bind("LIST_5"_s, &Result::List5)
  267. .Bind("LIST_6"_s, &Result::List6)
  268. .Bind("MULTI_1"_s, &Result::Multi1)
  269. .Bind("MULTI_2"_s, &Result::Multi2)
  270. .Bind("MULTI_3"_s, &Result::Multi3)
  271. .Bind("MULTI_4"_s, &Result::Multi4)
  272. .Bind("FUNC_0"_s, &Result::Func0)
  273. .Bind("FUNC_1"_s, &Result::Func1)
  274. .Bind("FUNC_2a"_s, &Result::Func2)
  275. .Bind("FUNC_2b"_s, &Result::Func2)
  276. .Bind("FUNC_3"_s,
  277. [](Result& result, cm::string_view arg) -> ArgumentParser::Continue {
  278. return result.Func3(arg);
  279. })
  280. .Bind("FUNC_4a"_s, parserStaticFunc4)
  281. .Bind("FUNC_4b"_s, parserStaticFunc4)
  282. .BindParsedKeywords(&Result::ParsedKeywords)
  283. /* keep semicolon on own line */;
  284. bool testArgumentParserStatic()
  285. {
  286. std::vector<std::string> unparsedArguments;
  287. Result const result = parserStatic.Parse(args, &unparsedArguments);
  288. return verifyResult(result, unparsedArguments);
  289. }
  290. bool testArgumentParserStaticBool()
  291. {
  292. std::vector<std::string> unparsedArguments;
  293. Result result;
  294. ASSERT_TRUE(parserStatic.Parse(result, args, &unparsedArguments) == false);
  295. return verifyResult(result, unparsedArguments);
  296. }
  297. } // namespace
  298. int testArgumentParser(int /*unused*/, char* /*unused*/ [])
  299. {
  300. if (!testArgumentParserDynamic()) {
  301. std::cout << "While executing testArgumentParserDynamic().\n";
  302. return -1;
  303. }
  304. if (!testArgumentParserStatic()) {
  305. std::cout << "While executing testArgumentParserStatic().\n";
  306. return -1;
  307. }
  308. if (!testArgumentParserStaticBool()) {
  309. std::cout << "While executing testArgumentParserStaticBool().\n";
  310. return -1;
  311. }
  312. return 0;
  313. }