1
0

testArgumentParser.cxx 13 KB

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