🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

776 lines
21 KiB

  1. #include "catch.hpp"
  2. #include <initializer_list>
  3. #include "cxxopts.hpp"
  4. class Argv {
  5. public:
  6. Argv(std::initializer_list<const char*> args)
  7. : m_argv(new char*[args.size()])
  8. , m_argc(static_cast<int>(args.size()))
  9. {
  10. int i = 0;
  11. auto iter = args.begin();
  12. while (iter != args.end()) {
  13. auto len = strlen(*iter) + 1;
  14. auto ptr = std::unique_ptr<char[]>(new char[len]);
  15. strcpy(ptr.get(), *iter);
  16. m_args.push_back(std::move(ptr));
  17. m_argv.get()[i] = m_args.back().get();
  18. ++iter;
  19. ++i;
  20. }
  21. }
  22. char** argv() const {
  23. return m_argv.get();
  24. }
  25. int argc() const {
  26. return m_argc;
  27. }
  28. private:
  29. std::vector<std::unique_ptr<char[]>> m_args;
  30. std::unique_ptr<char*[]> m_argv;
  31. int m_argc;
  32. };
  33. TEST_CASE("Basic options", "[options]")
  34. {
  35. cxxopts::Options options("tester", " - test basic options");
  36. options.add_options()
  37. ("long", "a long option")
  38. ("s,short", "a short option")
  39. ("value", "an option with a value", cxxopts::value<std::string>())
  40. ("a,av", "a short option with a value", cxxopts::value<std::string>())
  41. ("6,six", "a short number option")
  42. ("p, space", "an option with space between short and long")
  43. ("nothing", "won't exist", cxxopts::value<std::string>())
  44. ;
  45. Argv argv({
  46. "tester",
  47. "--long",
  48. "-s",
  49. "--value",
  50. "value",
  51. "-a",
  52. "b",
  53. "-6",
  54. "-p",
  55. "--space",
  56. });
  57. char** actual_argv = argv.argv();
  58. auto argc = argv.argc();
  59. auto result = options.parse(argc, actual_argv);
  60. CHECK(result.count("long") == 1);
  61. CHECK(result.count("s") == 1);
  62. CHECK(result.count("value") == 1);
  63. CHECK(result.count("a") == 1);
  64. CHECK(result["value"].as<std::string>() == "value");
  65. CHECK(result["a"].as<std::string>() == "b");
  66. CHECK(result.count("6") == 1);
  67. CHECK(result.count("p") == 2);
  68. CHECK(result.count("space") == 2);
  69. auto& arguments = result.arguments();
  70. REQUIRE(arguments.size() == 7);
  71. CHECK(arguments[0].key() == "long");
  72. CHECK(arguments[0].value() == "true");
  73. CHECK(arguments[0].as<bool>() == true);
  74. CHECK(arguments[1].key() == "short");
  75. CHECK(arguments[2].key() == "value");
  76. CHECK(arguments[3].key() == "av");
  77. CHECK_THROWS_AS(result["nothing"].as<std::string>(), cxxopts::option_has_no_value_exception&);
  78. }
  79. TEST_CASE("Short options", "[options]")
  80. {
  81. cxxopts::Options options("test_short", " - test short options");
  82. options.add_options()
  83. ("a", "a short option", cxxopts::value<std::string>());
  84. Argv argv({"test_short", "-a", "value"});
  85. auto actual_argv = argv.argv();
  86. auto argc = argv.argc();
  87. auto result = options.parse(argc, actual_argv);
  88. CHECK(result.count("a") == 1);
  89. CHECK(result["a"].as<std::string>() == "value");
  90. REQUIRE_THROWS_AS(options.add_options()("", "nothing option"),
  91. cxxopts::invalid_option_format_error&);
  92. }
  93. TEST_CASE("No positional", "[positional]")
  94. {
  95. cxxopts::Options options("test_no_positional",
  96. " - test no positional options");
  97. Argv av({"tester", "a", "b", "def"});
  98. char** argv = av.argv();
  99. auto argc = av.argc();
  100. auto result = options.parse(argc, argv);
  101. REQUIRE(argc == 4);
  102. CHECK(strcmp(argv[1], "a") == 0);
  103. }
  104. TEST_CASE("All positional", "[positional]")
  105. {
  106. std::vector<std::string> positional;
  107. cxxopts::Options options("test_all_positional", " - test all positional");
  108. options.add_options()
  109. ("positional", "Positional parameters",
  110. cxxopts::value<std::vector<std::string>>(positional))
  111. ;
  112. Argv av({"tester", "a", "b", "c"});
  113. auto argc = av.argc();
  114. auto argv = av.argv();
  115. std::vector<std::string> pos_names = {"positional"};
  116. options.parse_positional(pos_names.begin(), pos_names.end());
  117. auto result = options.parse(argc, argv);
  118. REQUIRE(argc == 1);
  119. REQUIRE(positional.size() == 3);
  120. CHECK(positional[0] == "a");
  121. CHECK(positional[1] == "b");
  122. CHECK(positional[2] == "c");
  123. }
  124. TEST_CASE("Some positional explicit", "[positional]")
  125. {
  126. cxxopts::Options options("positional_explicit", " - test positional");
  127. options.add_options()
  128. ("input", "Input file", cxxopts::value<std::string>())
  129. ("output", "Output file", cxxopts::value<std::string>())
  130. ("positional", "Positional parameters",
  131. cxxopts::value<std::vector<std::string>>())
  132. ;
  133. options.parse_positional({"input", "output", "positional"});
  134. Argv av({"tester", "--output", "a", "b", "c", "d"});
  135. char** argv = av.argv();
  136. auto argc = av.argc();
  137. auto result = options.parse(argc, argv);
  138. CHECK(argc == 1);
  139. CHECK(result.count("output"));
  140. CHECK(result["input"].as<std::string>() == "b");
  141. CHECK(result["output"].as<std::string>() == "a");
  142. auto& positional = result["positional"].as<std::vector<std::string>>();
  143. REQUIRE(positional.size() == 2);
  144. CHECK(positional[0] == "c");
  145. CHECK(positional[1] == "d");
  146. }
  147. TEST_CASE("No positional with extras", "[positional]")
  148. {
  149. cxxopts::Options options("posargmaster", "shows incorrect handling");
  150. options.add_options()
  151. ("dummy", "oh no", cxxopts::value<std::string>())
  152. ;
  153. Argv av({"extras", "--", "a", "b", "c", "d"});
  154. char** argv = av.argv();
  155. auto argc = av.argc();
  156. auto old_argv = argv;
  157. auto old_argc = argc;
  158. options.parse(argc, argv);
  159. REQUIRE(argc == old_argc - 1);
  160. CHECK(argv[0] == std::string("extras"));
  161. CHECK(argv[1] == std::string("a"));
  162. }
  163. TEST_CASE("Positional not valid", "[positional]") {
  164. cxxopts::Options options("positional_invalid", "invalid positional argument");
  165. options.add_options()
  166. ("long", "a long option", cxxopts::value<std::string>())
  167. ;
  168. options.parse_positional("something");
  169. Argv av({"foobar", "bar", "baz"});
  170. char** argv = av.argv();
  171. auto argc = av.argc();
  172. CHECK_THROWS_AS(options.parse(argc, argv), cxxopts::option_not_exists_exception&);
  173. }
  174. TEST_CASE("Empty with implicit value", "[implicit]")
  175. {
  176. cxxopts::Options options("empty_implicit", "doesn't handle empty");
  177. options.add_options()
  178. ("implicit", "Has implicit", cxxopts::value<std::string>()
  179. ->implicit_value("foo"));
  180. Argv av({"implicit", "--implicit="});
  181. char** argv = av.argv();
  182. auto argc = av.argc();
  183. auto result = options.parse(argc, argv);
  184. REQUIRE(result.count("implicit") == 1);
  185. REQUIRE(result["implicit"].as<std::string>() == "");
  186. }
  187. TEST_CASE("Boolean without implicit value", "[implicit]")
  188. {
  189. cxxopts::Options options("no_implicit", "bool without an implicit value");
  190. options.add_options()
  191. ("bool", "Boolean without implicit", cxxopts::value<bool>()
  192. ->no_implicit_value());
  193. SECTION("When no value provided") {
  194. Argv av({"no_implicit", "--bool"});
  195. char** argv = av.argv();
  196. auto argc = av.argc();
  197. CHECK_THROWS_AS(options.parse(argc, argv), cxxopts::missing_argument_exception&);
  198. }
  199. SECTION("With equal-separated true") {
  200. Argv av({"no_implicit", "--bool=true"});
  201. char** argv = av.argv();
  202. auto argc = av.argc();
  203. auto result = options.parse(argc, argv);
  204. CHECK(result.count("bool") == 1);
  205. CHECK(result["bool"].as<bool>() == true);
  206. }
  207. SECTION("With equal-separated false") {
  208. Argv av({"no_implicit", "--bool=false"});
  209. char** argv = av.argv();
  210. auto argc = av.argc();
  211. auto result = options.parse(argc, argv);
  212. CHECK(result.count("bool") == 1);
  213. CHECK(result["bool"].as<bool>() == false);
  214. }
  215. SECTION("With space-separated true") {
  216. Argv av({"no_implicit", "--bool", "true"});
  217. char** argv = av.argv();
  218. auto argc = av.argc();
  219. auto result = options.parse(argc, argv);
  220. CHECK(result.count("bool") == 1);
  221. CHECK(result["bool"].as<bool>() == true);
  222. }
  223. SECTION("With space-separated false") {
  224. Argv av({"no_implicit", "--bool", "false"});
  225. char** argv = av.argv();
  226. auto argc = av.argc();
  227. auto result = options.parse(argc, argv);
  228. CHECK(result.count("bool") == 1);
  229. CHECK(result["bool"].as<bool>() == false);
  230. }
  231. }
  232. TEST_CASE("Default values", "[default]")
  233. {
  234. cxxopts::Options options("defaults", "has defaults");
  235. options.add_options()
  236. ("default", "Has implicit", cxxopts::value<int>()->default_value("42"))
  237. ("v,vector", "Default vector", cxxopts::value<std::vector<int>>()
  238. ->default_value("1,4"))
  239. ;
  240. SECTION("Sets defaults") {
  241. Argv av({"implicit"});
  242. char** argv = av.argv();
  243. auto argc = av.argc();
  244. auto result = options.parse(argc, argv);
  245. CHECK(result.count("default") == 0);
  246. CHECK(result["default"].as<int>() == 42);
  247. auto& v = result["vector"].as<std::vector<int>>();
  248. REQUIRE(v.size() == 2);
  249. CHECK(v[0] == 1);
  250. CHECK(v[1] == 4);
  251. }
  252. SECTION("When values provided") {
  253. Argv av({"implicit", "--default", "5"});
  254. char** argv = av.argv();
  255. auto argc = av.argc();
  256. auto result = options.parse(argc, argv);
  257. CHECK(result.count("default") == 1);
  258. CHECK(result["default"].as<int>() == 5);
  259. }
  260. }
  261. TEST_CASE("Parse into a reference", "[reference]")
  262. {
  263. int value = 0;
  264. cxxopts::Options options("into_reference", "parses into a reference");
  265. options.add_options()
  266. ("ref", "A reference", cxxopts::value(value));
  267. Argv av({"into_reference", "--ref", "42"});
  268. auto argv = av.argv();
  269. auto argc = av.argc();
  270. auto result = options.parse(argc, argv);
  271. CHECK(result.count("ref") == 1);
  272. CHECK(value == 42);
  273. }
  274. TEST_CASE("Integers", "[options]")
  275. {
  276. cxxopts::Options options("parses_integers", "parses integers correctly");
  277. options.add_options()
  278. ("positional", "Integers", cxxopts::value<std::vector<int>>());
  279. Argv av({"ints", "--", "5", "6", "-6", "0", "0xab", "0xAf", "0x0"});
  280. char** argv = av.argv();
  281. auto argc = av.argc();
  282. options.parse_positional("positional");
  283. auto result = options.parse(argc, argv);
  284. REQUIRE(result.count("positional") == 7);
  285. auto& positional = result["positional"].as<std::vector<int>>();
  286. REQUIRE(positional.size() == 7);
  287. CHECK(positional[0] == 5);
  288. CHECK(positional[1] == 6);
  289. CHECK(positional[2] == -6);
  290. CHECK(positional[3] == 0);
  291. CHECK(positional[4] == 0xab);
  292. CHECK(positional[5] == 0xaf);
  293. CHECK(positional[6] == 0x0);
  294. }
  295. TEST_CASE("Leading zero integers", "[options]")
  296. {
  297. cxxopts::Options options("parses_integers", "parses integers correctly");
  298. options.add_options()
  299. ("positional", "Integers", cxxopts::value<std::vector<int>>());
  300. Argv av({"ints", "--", "05", "06", "0x0ab", "0x0001"});
  301. char** argv = av.argv();
  302. auto argc = av.argc();
  303. options.parse_positional("positional");
  304. auto result = options.parse(argc, argv);
  305. REQUIRE(result.count("positional") == 4);
  306. auto& positional = result["positional"].as<std::vector<int>>();
  307. REQUIRE(positional.size() == 4);
  308. CHECK(positional[0] == 5);
  309. CHECK(positional[1] == 6);
  310. CHECK(positional[2] == 0xab);
  311. CHECK(positional[3] == 0x1);
  312. }
  313. TEST_CASE("Unsigned integers", "[options]")
  314. {
  315. cxxopts::Options options("parses_unsigned", "detects unsigned errors");
  316. options.add_options()
  317. ("positional", "Integers", cxxopts::value<std::vector<unsigned int>>());
  318. Argv av({"ints", "--", "-2"});
  319. char** argv = av.argv();
  320. auto argc = av.argc();
  321. options.parse_positional("positional");
  322. CHECK_THROWS_AS(options.parse(argc, argv), cxxopts::argument_incorrect_type&);
  323. }
  324. TEST_CASE("Integer bounds", "[integer]")
  325. {
  326. cxxopts::Options options("integer_boundaries", "check min/max integer");
  327. options.add_options()
  328. ("positional", "Integers", cxxopts::value<std::vector<int8_t>>());
  329. SECTION("No overflow")
  330. {
  331. Argv av({"ints", "--", "127", "-128", "0x7f", "-0x80", "0x7e"});
  332. auto argv = av.argv();
  333. auto argc = av.argc();
  334. options.parse_positional("positional");
  335. auto result = options.parse(argc, argv);
  336. REQUIRE(result.count("positional") == 5);
  337. auto& positional = result["positional"].as<std::vector<int8_t>>();
  338. CHECK(positional[0] == 127);
  339. CHECK(positional[1] == -128);
  340. CHECK(positional[2] == 0x7f);
  341. CHECK(positional[3] == -0x80);
  342. CHECK(positional[4] == 0x7e);
  343. }
  344. }
  345. TEST_CASE("Overflow on boundary", "[integer]")
  346. {
  347. using namespace cxxopts::values;
  348. int8_t si;
  349. uint8_t ui;
  350. CHECK_THROWS_AS((integer_parser("128", si)), cxxopts::argument_incorrect_type&);
  351. CHECK_THROWS_AS((integer_parser("-129", si)), cxxopts::argument_incorrect_type&);
  352. CHECK_THROWS_AS((integer_parser("256", ui)), cxxopts::argument_incorrect_type&);
  353. CHECK_THROWS_AS((integer_parser("-0x81", si)), cxxopts::argument_incorrect_type&);
  354. CHECK_THROWS_AS((integer_parser("0x80", si)), cxxopts::argument_incorrect_type&);
  355. CHECK_THROWS_AS((integer_parser("0x100", ui)), cxxopts::argument_incorrect_type&);
  356. }
  357. TEST_CASE("Integer overflow", "[options]")
  358. {
  359. using namespace cxxopts::values;
  360. cxxopts::Options options("reject_overflow", "rejects overflowing integers");
  361. options.add_options()
  362. ("positional", "Integers", cxxopts::value<std::vector<int8_t>>());
  363. Argv av({"ints", "--", "128"});
  364. auto argv = av.argv();
  365. auto argc = av.argc();
  366. options.parse_positional("positional");
  367. CHECK_THROWS_AS(options.parse(argc, argv), cxxopts::argument_incorrect_type&);
  368. int integer = 0;
  369. CHECK_THROWS_AS((integer_parser("23423423423", integer)), cxxopts::argument_incorrect_type&);
  370. CHECK_THROWS_AS((integer_parser("234234234234", integer)), cxxopts::argument_incorrect_type&);
  371. }
  372. TEST_CASE("Floats", "[options]")
  373. {
  374. cxxopts::Options options("parses_floats", "parses floats correctly");
  375. options.add_options()
  376. ("double", "Double precision", cxxopts::value<double>())
  377. ("positional", "Floats", cxxopts::value<std::vector<float>>());
  378. Argv av({"floats", "--double", "0.5", "--", "4", "-4", "1.5e6", "-1.5e6"});
  379. char** argv = av.argv();
  380. auto argc = av.argc();
  381. options.parse_positional("positional");
  382. auto result = options.parse(argc, argv);
  383. REQUIRE(result.count("double") == 1);
  384. REQUIRE(result.count("positional") == 4);
  385. CHECK(result["double"].as<double>() == 0.5);
  386. auto& positional = result["positional"].as<std::vector<float>>();
  387. CHECK(positional[0] == 4);
  388. CHECK(positional[1] == -4);
  389. CHECK(positional[2] == 1.5e6);
  390. CHECK(positional[3] == -1.5e6);
  391. }
  392. TEST_CASE("Invalid integers", "[integer]") {
  393. cxxopts::Options options("invalid_integers", "rejects invalid integers");
  394. options.add_options()
  395. ("positional", "Integers", cxxopts::value<std::vector<int>>());
  396. Argv av({"ints", "--", "Ae"});
  397. char **argv = av.argv();
  398. auto argc = av.argc();
  399. options.parse_positional("positional");
  400. CHECK_THROWS_AS(options.parse(argc, argv), cxxopts::argument_incorrect_type&);
  401. }
  402. TEST_CASE("Booleans", "[boolean]") {
  403. cxxopts::Options options("parses_floats", "parses floats correctly");
  404. options.add_options()
  405. ("bool", "A Boolean", cxxopts::value<bool>())
  406. ("debug", "Debugging", cxxopts::value<bool>())
  407. ("timing", "Timing", cxxopts::value<bool>())
  408. ("verbose", "Verbose", cxxopts::value<bool>())
  409. ("dry-run", "Dry Run", cxxopts::value<bool>())
  410. ("noExplicitDefault", "No Explicit Default", cxxopts::value<bool>())
  411. ("defaultTrue", "Timing", cxxopts::value<bool>()->default_value("true"))
  412. ("defaultFalse", "Timing", cxxopts::value<bool>()->default_value("false"))
  413. ("others", "Other arguments", cxxopts::value<std::vector<std::string>>())
  414. ;
  415. options.parse_positional("others");
  416. Argv av({"booleans", "--bool=false", "--debug=true", "--timing", "--verbose=1", "--dry-run=0", "extra"});
  417. char** argv = av.argv();
  418. auto argc = av.argc();
  419. auto result = options.parse(argc, argv);
  420. REQUIRE(result.count("bool") == 1);
  421. REQUIRE(result.count("debug") == 1);
  422. REQUIRE(result.count("timing") == 1);
  423. REQUIRE(result.count("verbose") == 1);
  424. REQUIRE(result.count("dry-run") == 1);
  425. REQUIRE(result.count("noExplicitDefault") == 0);
  426. REQUIRE(result.count("defaultTrue") == 0);
  427. REQUIRE(result.count("defaultFalse") == 0);
  428. CHECK(result["bool"].as<bool>() == false);
  429. CHECK(result["debug"].as<bool>() == true);
  430. CHECK(result["timing"].as<bool>() == true);
  431. CHECK(result["verbose"].as<bool>() == true);
  432. CHECK(result["dry-run"].as<bool>() == false);
  433. CHECK(result["noExplicitDefault"].as<bool>() == false);
  434. CHECK(result["defaultTrue"].as<bool>() == true);
  435. CHECK(result["defaultFalse"].as<bool>() == false);
  436. REQUIRE(result.count("others") == 1);
  437. }
  438. TEST_CASE("std::vector", "[vector]") {
  439. std::vector<double> vector;
  440. cxxopts::Options options("vector", " - tests vector");
  441. options.add_options()
  442. ("vector", "an vector option", cxxopts::value<std::vector<double>>(vector));
  443. Argv av({"vector", "--vector", "1,-2.1,3,4.5"});
  444. char** argv = av.argv();
  445. auto argc = av.argc();
  446. options.parse(argc, argv);
  447. REQUIRE(vector.size() == 4);
  448. CHECK(vector[0] == 1);
  449. CHECK(vector[1] == -2.1);
  450. CHECK(vector[2] == 3);
  451. CHECK(vector[3] == 4.5);
  452. }
  453. #ifdef CXXOPTS_HAS_OPTIONAL
  454. TEST_CASE("std::optional", "[optional]") {
  455. std::optional<std::string> optional;
  456. cxxopts::Options options("optional", " - tests optional");
  457. options.add_options()
  458. ("optional", "an optional option", cxxopts::value<std::optional<std::string>>(optional));
  459. Argv av({"optional", "--optional", "foo"});
  460. char** argv = av.argv();
  461. auto argc = av.argc();
  462. options.parse(argc, argv);
  463. REQUIRE(optional.has_value());
  464. CHECK(*optional == "foo");
  465. }
  466. #endif
  467. TEST_CASE("Unrecognised options", "[options]") {
  468. cxxopts::Options options("unknown_options", " - test unknown options");
  469. options.add_options()
  470. ("long", "a long option")
  471. ("s,short", "a short option");
  472. Argv av({
  473. "unknown_options",
  474. "--unknown",
  475. "--long",
  476. "-su",
  477. "--another_unknown",
  478. });
  479. char** argv = av.argv();
  480. auto argc = av.argc();
  481. SECTION("Default behaviour") {
  482. CHECK_THROWS_AS(options.parse(argc, argv), cxxopts::option_not_exists_exception&);
  483. }
  484. SECTION("After allowing unrecognised options") {
  485. options.allow_unrecognised_options();
  486. CHECK_NOTHROW(options.parse(argc, argv));
  487. REQUIRE(argc == 3);
  488. CHECK_THAT(argv[1], Catch::Equals("--unknown"));
  489. }
  490. }
  491. TEST_CASE("Allow bad short syntax", "[options]") {
  492. cxxopts::Options options("unknown_options", " - test unknown options");
  493. options.add_options()
  494. ("long", "a long option")
  495. ("s,short", "a short option");
  496. Argv av({
  497. "unknown_options",
  498. "-some_bad_short",
  499. });
  500. char** argv = av.argv();
  501. auto argc = av.argc();
  502. SECTION("Default behaviour") {
  503. CHECK_THROWS_AS(options.parse(argc, argv), cxxopts::option_syntax_exception&);
  504. }
  505. SECTION("After allowing unrecognised options") {
  506. options.allow_unrecognised_options();
  507. CHECK_NOTHROW(options.parse(argc, argv));
  508. REQUIRE(argc == 2);
  509. CHECK_THAT(argv[1], Catch::Equals("-some_bad_short"));
  510. }
  511. }
  512. TEST_CASE("Invalid option syntax", "[options]") {
  513. cxxopts::Options options("invalid_syntax", " - test invalid syntax");
  514. Argv av({
  515. "invalid_syntax",
  516. "--a",
  517. });
  518. char** argv = av.argv();
  519. auto argc = av.argc();
  520. SECTION("Default behaviour") {
  521. CHECK_THROWS_AS(options.parse(argc, argv), cxxopts::option_syntax_exception&);
  522. }
  523. }
  524. TEST_CASE("Options empty", "[options]") {
  525. cxxopts::Options options("Options list empty", " - test empty option list");
  526. options.add_options();
  527. options.add_options("");
  528. options.add_options("", {});
  529. options.add_options("test");
  530. Argv argv_({
  531. "test",
  532. "--unknown"
  533. });
  534. auto argc = argv_.argc();
  535. char** argv = argv_.argv();
  536. CHECK(options.groups().empty());
  537. CHECK_THROWS_AS(options.parse(argc, argv), cxxopts::option_not_exists_exception&);
  538. }
  539. TEST_CASE("Initializer list with group", "[options]") {
  540. cxxopts::Options options("Initializer list group", " - test initializer list with group");
  541. options.add_options("", {
  542. {"a, address", "server address", cxxopts::value<std::string>()->default_value("127.0.0.1")},
  543. {"p, port", "server port", cxxopts::value<std::string>()->default_value("7110"), "PORT"},
  544. });
  545. cxxopts::Option help{"h,help", "Help"};
  546. options.add_options("TEST_GROUP", {
  547. {"t, test", "test option"},
  548. help
  549. });
  550. Argv argv({
  551. "test",
  552. "--address",
  553. "10.0.0.1",
  554. "-p",
  555. "8000",
  556. "-t",
  557. });
  558. char** actual_argv = argv.argv();
  559. auto argc = argv.argc();
  560. auto result = options.parse(argc, actual_argv);
  561. CHECK(options.groups().size() == 2);
  562. CHECK(result.count("address") == 1);
  563. CHECK(result.count("port") == 1);
  564. CHECK(result.count("test") == 1);
  565. CHECK(result.count("help") == 0);
  566. CHECK(result["address"].as<std::string>() == "10.0.0.1");
  567. CHECK(result["port"].as<std::string>() == "8000");
  568. CHECK(result["test"].as<bool>() == true);
  569. }
  570. TEST_CASE("Option add with add_option(string, Option)", "[options]") {
  571. cxxopts::Options options("Option add with add_option", " - test Option add with add_option(string, Option)");
  572. cxxopts::Option option_1("t,test", "test option", cxxopts::value<int>()->default_value("7"), "TEST");
  573. options.add_option("", option_1);
  574. options.add_option("TEST", {"a,aggregate", "test option 2", cxxopts::value<int>(), "AGGREGATE"});
  575. Argv argv_({
  576. "test",
  577. "--test",
  578. "5",
  579. "-a",
  580. "4"
  581. });
  582. auto argc = argv_.argc();
  583. char** argv = argv_.argv();
  584. auto result = options.parse(argc, argv);
  585. CHECK(result.arguments().size()==2);
  586. CHECK(options.groups().size() == 2);
  587. CHECK(result.count("address") == 0);
  588. CHECK(result.count("aggregate") == 1);
  589. CHECK(result.count("test") == 1);
  590. CHECK(result["aggregate"].as<int>() == 4);
  591. CHECK(result["test"].as<int>() == 5);
  592. }