testArgumentParser.cxx 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file LICENSE.rst or https://cmake.org/licensing for details. */
  3. #include <map>
  4. #include <string>
  5. #include <utility>
  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. #include "testCommon.h"
  13. namespace {
  14. struct Result : public ArgumentParser::ParseResult
  15. {
  16. bool Option1 = false;
  17. bool Option2 = false;
  18. std::string String1;
  19. cm::optional<std::string> String2;
  20. cm::optional<std::string> String3;
  21. ArgumentParser::Maybe<std::string> String4;
  22. ArgumentParser::NonEmpty<std::string> String5;
  23. ArgumentParser::NonEmpty<std::string> String6;
  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> Pos0;
  35. cm::optional<std::string> Pos1;
  36. cm::optional<std::string> Pos2;
  37. ArgumentParser::MaybeEmpty<std::vector<std::string>> TrailingPos;
  38. bool Func0_ = false;
  39. ArgumentParser::Continue Func0(cm::string_view)
  40. {
  41. Func0_ = true;
  42. return ArgumentParser::Continue::No;
  43. }
  44. std::string Func1_;
  45. ArgumentParser::Continue Func1(cm::string_view arg)
  46. {
  47. Func1_ = std::string(arg);
  48. return ArgumentParser::Continue::No;
  49. }
  50. std::map<std::string, std::vector<std::string>> Func2_;
  51. ArgumentParser::Continue Func2(cm::string_view key, cm::string_view arg)
  52. {
  53. Func2_[std::string(key)].emplace_back(arg);
  54. return key == "FUNC_2b" ? ArgumentParser::Continue::Yes
  55. : ArgumentParser::Continue::No;
  56. }
  57. std::vector<std::string> Func3_;
  58. ArgumentParser::Continue Func3(cm::string_view arg)
  59. {
  60. Func3_.emplace_back(arg);
  61. return ArgumentParser::Continue::Yes;
  62. }
  63. std::map<std::string, std::vector<std::string>> Func4_;
  64. ArgumentParser::Continue Func4(cm::string_view key, cm::string_view arg)
  65. {
  66. Func4_[std::string(key)].emplace_back(arg);
  67. return key == "FUNC_4b" ? ArgumentParser::Continue::Yes
  68. : ArgumentParser::Continue::No;
  69. }
  70. ArgumentParser::Maybe<std::string> UnboundMaybe{ 'u', 'n', 'b', 'o',
  71. 'u', 'n', 'd' };
  72. ArgumentParser::MaybeEmpty<std::vector<std::string>> UnboundMaybeEmpty{
  73. 1, "unbound"
  74. };
  75. ArgumentParser::NonEmpty<std::vector<std::string>> UnboundNonEmpty{
  76. 1, "unbound"
  77. };
  78. ArgumentParser::NonEmpty<std::string> UnboundNonEmptyStr{ 'u', 'n', 'b', 'o',
  79. 'u', 'n', 'd' };
  80. std::vector<cm::string_view> ParsedKeywords;
  81. };
  82. struct Derived : Result
  83. {
  84. };
  85. std::initializer_list<cm::string_view> const args = {
  86. /* clang-format off */
  87. "pos0", // position index 0
  88. "OPTION_1", // option
  89. "pos2", // position index 2, ignored because after keyword
  90. // "OPTION_2", // option that is not present
  91. "STRING_1", // string arg missing value
  92. "STRING_2", "foo", "bar", // string arg + unparsed value, presence captured
  93. // "STRING_3", // string arg that is not present
  94. "STRING_4", // string arg allowed to be missing value
  95. "STRING_5", "foo", // string arg that is not empty
  96. "STRING_6", "", // string arg that is empty
  97. "LIST_1", // list arg missing values
  98. "LIST_2", "foo", "bar", // list arg with 2 elems
  99. "LIST_3", "bar", // list arg ...
  100. "LIST_3", "foo", // ... with continuation
  101. "LIST_4", // list arg missing values, presence captured
  102. // "LIST_5", // list arg that is not present
  103. "LIST_6", // list arg allowed to be empty
  104. "MULTI_2", // multi list with 0 lists
  105. "MULTI_3", "foo", "bar", // multi list with first list with two elems
  106. "MULTI_3", "bar", "foo", // multi list with second list with two elems
  107. // "MULTI_4", // multi list arg that is not present
  108. "FUNC_0", // callback arg missing value
  109. "FUNC_1", "foo", "ign1", // callback with one arg + unparsed value
  110. "FUNC_2a", "foo", "ign2", // callback with keyword-dependent arg count
  111. "FUNC_2b", "bar", "zot", // callback with keyword-dependent arg count
  112. "FUNC_3", "foo", "bar", // callback with list arg ...
  113. "FUNC_4a", "foo", "ign4", // callback with keyword-dependent arg count
  114. "FUNC_4b", "bar", "zot", // callback with keyword-dependent arg count
  115. /* clang-format on */
  116. };
  117. struct ResultTrailingPos : public ArgumentParser::ParseResult
  118. {
  119. bool Option1 = false;
  120. ArgumentParser::NonEmpty<std::vector<std::string>> List1;
  121. cm::optional<std::string> Pos0;
  122. cm::optional<std::string> Pos1;
  123. ArgumentParser::NonEmpty<std::vector<std::string>> TrailingPos;
  124. };
  125. struct DerivedTrailingPos : ResultTrailingPos
  126. {
  127. };
  128. std::initializer_list<cm::string_view> const args_trailingpos = {
  129. /* clang-format off */
  130. "pos0", // position index 0
  131. "pos1", // position index 1
  132. "pos_trailing0", // trailing positional 0
  133. "pos_trailing1", // trailing positional 1
  134. "OPTION_1", // option
  135. "LIST_1", "foo", "bar", // list arg with 2 elems
  136. /* clang-format on */
  137. };
  138. bool verifyResult(Result const& result,
  139. std::vector<std::string> const& unparsedArguments)
  140. {
  141. static std::vector<std::string> const foobar = { "foo", "bar" };
  142. static std::vector<std::string> const barfoo = { "bar", "foo" };
  143. static std::vector<std::string> const unbound = { "unbound" };
  144. static std::vector<cm::string_view> const parsedKeywords = {
  145. /* clang-format off */
  146. "OPTION_1",
  147. "STRING_1",
  148. "STRING_2",
  149. "STRING_4",
  150. "STRING_5",
  151. "STRING_6",
  152. "LIST_1",
  153. "LIST_2",
  154. "LIST_3",
  155. "LIST_3",
  156. "LIST_4",
  157. "LIST_6",
  158. "MULTI_2",
  159. "MULTI_3",
  160. "MULTI_3",
  161. "FUNC_0",
  162. "FUNC_1",
  163. "FUNC_2a",
  164. "FUNC_2b",
  165. "FUNC_3",
  166. "FUNC_4a",
  167. "FUNC_4b",
  168. /* clang-format on */
  169. };
  170. static std::map<std::string, std::vector<std::string>> const func2map = {
  171. { "FUNC_2a", { "foo" } }, { "FUNC_2b", { "bar", "zot" } }
  172. };
  173. static std::map<std::string, std::vector<std::string>> const func4map = {
  174. { "FUNC_4a", { "foo" } }, { "FUNC_4b", { "bar", "zot" } }
  175. };
  176. static std::map<cm::string_view, std::string> const keywordErrors = {
  177. { "STRING_1"_s, " missing required value\n" },
  178. { "STRING_6"_s, " empty string not allowed\n" },
  179. { "LIST_1"_s, " missing required value\n" },
  180. { "LIST_4"_s, " missing required value\n" },
  181. { "FUNC_0"_s, " missing required value\n" }
  182. };
  183. static std::vector<std::string> const unparsed = { "pos2", "bar", "ign1",
  184. "ign2", "ign4" };
  185. ASSERT_TRUE(!result);
  186. ASSERT_TRUE(result.Option1);
  187. ASSERT_TRUE(!result.Option2);
  188. ASSERT_TRUE(result.String1.empty());
  189. ASSERT_TRUE(result.String2);
  190. ASSERT_TRUE(*result.String2 == "foo");
  191. ASSERT_TRUE(!result.String3);
  192. ASSERT_TRUE(result.String4.empty());
  193. ASSERT_TRUE(result.String5 == "foo");
  194. ASSERT_TRUE(result.String6.empty());
  195. ASSERT_TRUE(result.List1.empty());
  196. ASSERT_TRUE(result.List2 == foobar);
  197. ASSERT_TRUE(result.List3);
  198. ASSERT_TRUE(*result.List3 == barfoo);
  199. ASSERT_TRUE(result.List4);
  200. ASSERT_TRUE(result.List4->empty());
  201. ASSERT_TRUE(!result.List5);
  202. ASSERT_TRUE(result.List6);
  203. ASSERT_TRUE(result.List6->empty());
  204. ASSERT_TRUE(result.Multi1.empty());
  205. ASSERT_TRUE(result.Multi2.size() == 1);
  206. ASSERT_TRUE(result.Multi2[0].empty());
  207. ASSERT_TRUE(result.Multi3);
  208. ASSERT_TRUE((*result.Multi3).size() == 2);
  209. ASSERT_TRUE((*result.Multi3)[0] == foobar);
  210. ASSERT_TRUE((*result.Multi3)[1] == barfoo);
  211. ASSERT_TRUE(!result.Multi4);
  212. ASSERT_TRUE(result.Pos0 == "pos0");
  213. ASSERT_TRUE(!result.Pos1);
  214. ASSERT_TRUE(!result.Pos2);
  215. ASSERT_TRUE(result.TrailingPos.empty());
  216. ASSERT_TRUE(result.Func0_ == false);
  217. ASSERT_TRUE(result.Func1_ == "foo");
  218. ASSERT_TRUE(result.Func2_ == func2map);
  219. ASSERT_TRUE(result.Func3_ == foobar);
  220. ASSERT_TRUE(result.Func4_ == func4map);
  221. ASSERT_TRUE(unparsedArguments == unparsed);
  222. ASSERT_TRUE(result.UnboundMaybe == "unbound");
  223. ASSERT_TRUE(result.UnboundMaybeEmpty == unbound);
  224. ASSERT_TRUE(result.UnboundNonEmpty == unbound);
  225. ASSERT_TRUE(result.UnboundNonEmptyStr == "unbound");
  226. ASSERT_TRUE(result.ParsedKeywords == parsedKeywords);
  227. ASSERT_TRUE(result.GetKeywordErrors().size() == keywordErrors.size());
  228. for (auto const& ke : result.GetKeywordErrors()) {
  229. auto const ki = keywordErrors.find(ke.first);
  230. ASSERT_TRUE(ki != keywordErrors.end());
  231. ASSERT_TRUE(ke.second == ki->second);
  232. }
  233. return true;
  234. }
  235. bool verifyResult(ResultTrailingPos const& result,
  236. std::vector<std::string> const& unparsedArguments)
  237. {
  238. static std::vector<std::string> const foobar = { "foo", "bar" };
  239. static std::vector<std::string> const trailing = { "pos_trailing0",
  240. "pos_trailing1" };
  241. ASSERT_TRUE(result);
  242. ASSERT_TRUE(unparsedArguments.empty());
  243. ASSERT_TRUE(result.Option1);
  244. ASSERT_TRUE(result.List1 == foobar);
  245. ASSERT_TRUE(result.Pos0 == "pos0");
  246. ASSERT_TRUE(result.Pos1 == "pos1");
  247. ASSERT_TRUE(result.TrailingPos == trailing);
  248. return true;
  249. }
  250. bool testArgumentParserDynamic()
  251. {
  252. Result result;
  253. std::vector<std::string> unparsedArguments;
  254. std::function<ArgumentParser::Continue(cm::string_view, cm::string_view)>
  255. func4 = [&result](cm::string_view key,
  256. cm::string_view arg) -> ArgumentParser::Continue {
  257. return result.Func4(key, arg);
  258. };
  259. cmArgumentParser<void> parserDynamic;
  260. parserDynamic.Bind("OPTION_1"_s, result.Option1);
  261. ASSERT_TRUE(parserDynamic.HasKeyword("OPTION_1"_s));
  262. ASSERT_TRUE(!parserDynamic.HasKeyword("NOT_AN_OPTION"_s));
  263. static_cast<ArgumentParser::ParseResult&>(result) =
  264. cmArgumentParser<void>{}
  265. .Bind(0, result.Pos0)
  266. .Bind(1, result.Pos1)
  267. .Bind(2, result.Pos2)
  268. .BindTrailingArgs(result.TrailingPos)
  269. .Bind("OPTION_1"_s, result.Option1)
  270. .Bind("OPTION_2"_s, result.Option2)
  271. .Bind("STRING_1"_s, result.String1)
  272. .Bind("STRING_2"_s, result.String2)
  273. .Bind("STRING_3"_s, result.String3)
  274. .Bind("STRING_4"_s, result.String4)
  275. .Bind("STRING_5"_s, result.String5)
  276. .Bind("STRING_6"_s, result.String6)
  277. .Bind("LIST_1"_s, result.List1)
  278. .Bind("LIST_2"_s, result.List2)
  279. .Bind("LIST_3"_s, result.List3)
  280. .Bind("LIST_4"_s, result.List4)
  281. .Bind("LIST_5"_s, result.List5)
  282. .Bind("LIST_6"_s, result.List6)
  283. .Bind("MULTI_1"_s, result.Multi1)
  284. .Bind("MULTI_2"_s, result.Multi2)
  285. .Bind("MULTI_3"_s, result.Multi3)
  286. .Bind("MULTI_4"_s, result.Multi4)
  287. .Bind("FUNC_0"_s,
  288. [&result](cm::string_view arg) -> ArgumentParser::Continue {
  289. return result.Func0(arg);
  290. })
  291. .Bind("FUNC_1"_s,
  292. [&result](cm::string_view arg) -> ArgumentParser::Continue {
  293. return result.Func1(arg);
  294. })
  295. .Bind("FUNC_2a"_s,
  296. [&result](cm::string_view key, cm::string_view arg)
  297. -> ArgumentParser::Continue { return result.Func2(key, arg); })
  298. .Bind("FUNC_2b"_s,
  299. [&result](cm::string_view key, cm::string_view arg)
  300. -> ArgumentParser::Continue { return result.Func2(key, arg); })
  301. .Bind("FUNC_3"_s,
  302. [&result](cm::string_view arg) -> ArgumentParser::Continue {
  303. return result.Func3(arg);
  304. })
  305. .Bind("FUNC_4a"_s, func4)
  306. .Bind("FUNC_4b"_s, func4)
  307. .BindParsedKeywords(result.ParsedKeywords)
  308. .Parse(args, &unparsedArguments);
  309. if (!verifyResult(result, unparsedArguments)) {
  310. return false;
  311. }
  312. unparsedArguments.clear();
  313. ResultTrailingPos result_trailing;
  314. static_cast<ArgumentParser::ParseResult&>(result_trailing) =
  315. cmArgumentParser<void>{}
  316. .Bind(0, result_trailing.Pos0)
  317. .Bind(1, result_trailing.Pos1)
  318. .BindTrailingArgs(result_trailing.TrailingPos)
  319. .Bind("OPTION_1"_s, result_trailing.Option1)
  320. .Bind("LIST_1"_s, result_trailing.List1)
  321. .Parse(args_trailingpos, &unparsedArguments);
  322. return verifyResult(result_trailing, unparsedArguments);
  323. }
  324. static auto const parserStaticFunc4 =
  325. [](Result& result, cm::string_view key,
  326. cm::string_view arg) -> ArgumentParser::Continue {
  327. return result.Func4(key, arg);
  328. };
  329. #define BIND_ALL(name, resultType) \
  330. static auto const name = \
  331. cmArgumentParser<resultType>{} \
  332. .Bind(0, &resultType::Pos0) \
  333. .Bind(1, &resultType::Pos1) \
  334. .Bind(2, &resultType::Pos2) \
  335. .BindTrailingArgs(&resultType::TrailingPos) \
  336. .Bind("OPTION_1"_s, &resultType::Option1) \
  337. .Bind("OPTION_2"_s, &resultType::Option2) \
  338. .Bind("STRING_1"_s, &resultType::String1) \
  339. .Bind("STRING_2"_s, &resultType::String2) \
  340. .Bind("STRING_3"_s, &resultType::String3) \
  341. .Bind("STRING_4"_s, &resultType::String4) \
  342. .Bind("STRING_5"_s, &resultType::String5) \
  343. .Bind("STRING_6"_s, &resultType::String6) \
  344. .Bind("LIST_1"_s, &resultType::List1) \
  345. .Bind("LIST_2"_s, &resultType::List2) \
  346. .Bind("LIST_3"_s, &resultType::List3) \
  347. .Bind("LIST_4"_s, &resultType::List4) \
  348. .Bind("LIST_5"_s, &resultType::List5) \
  349. .Bind("LIST_6"_s, &resultType::List6) \
  350. .Bind("MULTI_1"_s, &resultType::Multi1) \
  351. .Bind("MULTI_2"_s, &resultType::Multi2) \
  352. .Bind("MULTI_3"_s, &resultType::Multi3) \
  353. .Bind("MULTI_4"_s, &resultType::Multi4) \
  354. .Bind("FUNC_0"_s, &resultType::Func0) \
  355. .Bind("FUNC_1"_s, &resultType::Func1) \
  356. .Bind("FUNC_2a"_s, &resultType::Func2) \
  357. .Bind("FUNC_2b"_s, &resultType::Func2) \
  358. .Bind("FUNC_3"_s, \
  359. [](resultType& result, cm::string_view arg) \
  360. -> ArgumentParser::Continue { return result.Func3(arg); }) \
  361. .Bind("FUNC_4a"_s, parserStaticFunc4) \
  362. .Bind("FUNC_4b"_s, parserStaticFunc4) \
  363. .BindParsedKeywords(&resultType::ParsedKeywords)
  364. BIND_ALL(parserStatic, Result);
  365. BIND_ALL(parserDerivedStatic, Derived);
  366. #define BIND_TRAILING(name, resultType) \
  367. static auto const name = cmArgumentParser<resultType>{} \
  368. .Bind(0, &resultType::Pos0) \
  369. .Bind(1, &resultType::Pos1) \
  370. .BindTrailingArgs(&resultType::TrailingPos) \
  371. .Bind("OPTION_1"_s, &resultType::Option1) \
  372. .Bind("LIST_1"_s, &resultType::List1)
  373. BIND_TRAILING(parserTrailingStatic, ResultTrailingPos);
  374. BIND_TRAILING(parserTrailingDerivedStatic, DerivedTrailingPos);
  375. bool testArgumentParserStatic()
  376. {
  377. ASSERT_TRUE(parserStatic.HasKeyword("OPTION_1"_s));
  378. ASSERT_TRUE(!parserStatic.HasKeyword("NOT_AN_OPTION"_s));
  379. std::vector<std::string> unparsedArguments;
  380. Result const result = parserStatic.Parse(args, &unparsedArguments);
  381. if (!verifyResult(result, unparsedArguments)) {
  382. return false;
  383. }
  384. unparsedArguments.clear();
  385. ResultTrailingPos const result_trailing =
  386. parserTrailingStatic.Parse(args_trailingpos, &unparsedArguments);
  387. return verifyResult(result_trailing, unparsedArguments);
  388. }
  389. bool testArgumentParserDerivedStatic()
  390. {
  391. ASSERT_TRUE(parserDerivedStatic.HasKeyword("OPTION_1"_s));
  392. ASSERT_TRUE(!parserDerivedStatic.HasKeyword("NOT_AN_OPTION"_s));
  393. std::vector<std::string> unparsedArguments;
  394. Derived const result = parserDerivedStatic.Parse(args, &unparsedArguments);
  395. if (!verifyResult(result, unparsedArguments)) {
  396. return false;
  397. }
  398. unparsedArguments.clear();
  399. ResultTrailingPos const result_trailing =
  400. parserTrailingDerivedStatic.Parse(args_trailingpos, &unparsedArguments);
  401. return verifyResult(result_trailing, unparsedArguments);
  402. }
  403. bool testArgumentParserStaticBool()
  404. {
  405. std::vector<std::string> unparsedArguments;
  406. Result result;
  407. ASSERT_TRUE(parserStatic.Parse(result, args, &unparsedArguments) == false);
  408. return verifyResult(result, unparsedArguments);
  409. }
  410. bool testArgumentParserTypes()
  411. {
  412. ArgumentParser::Maybe<std::string> maybeString;
  413. maybeString = std::string();
  414. maybeString = "";
  415. ArgumentParser::MaybeEmpty<std::vector<std::string>> maybeEmptyVecStr;
  416. maybeEmptyVecStr = std::vector<std::string>{};
  417. ArgumentParser::NonEmpty<std::string> nonEmptyString;
  418. nonEmptyString = std::string("x");
  419. nonEmptyString = "x";
  420. ArgumentParser::NonEmpty<std::vector<std::string>> nonEmptyVecStr;
  421. nonEmptyVecStr = std::vector<std::string>{ "" };
  422. return true;
  423. }
  424. } // namespace
  425. int testArgumentParser(int /*unused*/, char* /*unused*/[])
  426. {
  427. if (!testArgumentParserDynamic()) {
  428. std::cout << "While executing testArgumentParserDynamic().\n";
  429. return -1;
  430. }
  431. if (!testArgumentParserStatic()) {
  432. std::cout << "While executing testArgumentParserStatic().\n";
  433. return -1;
  434. }
  435. if (!testArgumentParserDerivedStatic()) {
  436. std::cout << "While executing testArgumentParserDerivedStatic().\n";
  437. return -1;
  438. }
  439. if (!testArgumentParserStaticBool()) {
  440. std::cout << "While executing testArgumentParserStaticBool().\n";
  441. return -1;
  442. }
  443. if (!testArgumentParserTypes()) {
  444. std::cout << "While executing testArgumentParserTypes().\n";
  445. return -1;
  446. }
  447. return 0;
  448. }