testArgumentParser.cxx 12 KB

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