1
0

testArgumentParser.cxx 13 KB

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