parsers_test.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. // Copyright Vladimir Prus 2002-2004.
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt
  4. // or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #include <boost/program_options/parsers.hpp>
  6. #include <boost/program_options/options_description.hpp>
  7. #include <boost/program_options/variables_map.hpp>
  8. using namespace boost::program_options;
  9. // We'll use po::value everywhere to workaround vc6 bug.
  10. namespace po = boost::program_options;
  11. #include <boost/function.hpp>
  12. using namespace boost;
  13. #include <sstream>
  14. #include <iostream>
  15. #include <iomanip>
  16. using namespace std;
  17. #if defined(__sun)
  18. #include <stdlib.h> // for putenv on solaris
  19. #else
  20. #include <cstdlib> // for putenv
  21. #endif
  22. #include "minitest.hpp"
  23. #define TEST_CHECK_THROW(expression, exception, description) \
  24. try \
  25. { \
  26. expression; \
  27. BOOST_ERROR(description);\
  28. throw 10; \
  29. } \
  30. catch(exception &) \
  31. { \
  32. }
  33. pair<string, vector< vector<string> > > msp(const string& s1)
  34. {
  35. return std::make_pair(s1, vector< vector<string> >());
  36. }
  37. pair<string, vector< vector<string> > > msp(const string& s1, const string& s2)
  38. {
  39. vector< vector<string> > v(1);
  40. v[0].push_back(s2);
  41. return std::make_pair(s1, v);
  42. }
  43. void check_value(const option& option, const char* name, const char* value)
  44. {
  45. BOOST_CHECK(option.string_key == name);
  46. BOOST_REQUIRE(option.value.size() == 1);
  47. BOOST_CHECK(option.value.front() == value);
  48. }
  49. vector<string> sv(const char* array[], unsigned size)
  50. {
  51. vector<string> r;
  52. for (unsigned i = 0; i < size; ++i)
  53. r.push_back(array[i]);
  54. return r;
  55. }
  56. pair<string, string> additional_parser(const std::string&)
  57. {
  58. return pair<string, string>();
  59. }
  60. namespace command_line {
  61. #if 0
  62. // The following commented out blocks used to test parsing
  63. // command line without syntax specification behaviour.
  64. // It is disabled now and probably will never be enabled again:
  65. // it is not possible to figure out what command line means without
  66. // user's help.
  67. void test_parsing_without_specifying_options() {
  68. char* cmdline1[] = { "--a", "--b=12", "-f", "-g4", "-", "file" };
  69. options_and_arguments a1 = parse_command_line(cmdline1,
  70. cmdline1 + sizeof(cmdline1) / sizeof(cmdline1[0]));
  71. BOOST_REQUIRE(a1.options().size() == 4);
  72. BOOST_CHECK(a1.options()[0] == msp("a", ""));
  73. BOOST_CHECK(a1.options()[1] == msp("b", "12"));
  74. BOOST_CHECK(a1.options()[2] == msp("-f", ""));
  75. BOOST_CHECK(a1.options()[3] == msp("-g", "4"));
  76. BOOST_REQUIRE(a1.arguments().size() == 2);
  77. BOOST_CHECK(a1.arguments()[0] == "-");
  78. BOOST_CHECK(a1.arguments()[1] == "file");
  79. char* cmdline2[] = { "--a", "--", "file" };
  80. options_and_arguments a2 = parse_command_line(cmdline2,
  81. cmdline2 + sizeof(cmdline2) / sizeof(cmdline2[0]));
  82. BOOST_REQUIRE(a2.options().size() == 1);
  83. BOOST_CHECK(a2.options()[0] == msp("a", ""));
  84. BOOST_CHECK(a2.arguments().size() == 1);
  85. BOOST_CHECK(a2.arguments()[0] == "file");
  86. }
  87. #endif
  88. void test_many_different_options() {
  89. options_description desc;
  90. desc.add_options()
  91. ("foo,f", new untyped_value(), "")
  92. ( // Explicit qualification is a workaround for vc6
  93. "bar,b", po::value<std::string>(), "")
  94. ("car,voiture", new untyped_value())
  95. ("dog,dawg", new untyped_value())
  96. ("baz", new untyped_value())
  97. ("plug*", new untyped_value());
  98. const char* cmdline3_[] = { "--foo=12", "-f4", "--bar=11", "-b4",
  99. "--voiture=15", "--dawg=16", "--dog=17", "--plug3=10" };
  100. vector<string> cmdline3 = sv(cmdline3_,
  101. sizeof(cmdline3_) / sizeof(const char*));
  102. vector<option> a3 =
  103. command_line_parser(cmdline3).options(desc).run().options;
  104. BOOST_CHECK_EQUAL(a3.size(), 8u);
  105. check_value(a3[0], "foo", "12");
  106. check_value(a3[1], "foo", "4");
  107. check_value(a3[2], "bar", "11");
  108. check_value(a3[3], "bar", "4");
  109. check_value(a3[4], "car", "15");
  110. check_value(a3[5], "dog", "16");
  111. check_value(a3[6], "dog", "17");
  112. check_value(a3[7], "plug3", "10");
  113. // Regression test: check that '0' as style is interpreted as
  114. // 'default_style'
  115. vector<option> a4 = parse_command_line(
  116. sizeof(cmdline3_) / sizeof(const char*), cmdline3_, desc, 0,
  117. additional_parser).options;
  118. // The default style is unix-style, where the first argument on the command-line
  119. // is the name of a binary, not an option value, so that should be ignored
  120. BOOST_CHECK_EQUAL(a4.size(), 7u);
  121. check_value(a4[0], "foo", "4");
  122. check_value(a4[1], "bar", "11");
  123. check_value(a4[2], "bar", "4");
  124. check_value(a4[3], "car", "15");
  125. check_value(a4[4], "dog", "16");
  126. check_value(a4[5], "dog", "17");
  127. check_value(a4[6], "plug3", "10");
  128. }
  129. void test_not_crashing_with_empty_string_values() {
  130. // Check that we don't crash on empty values of type 'string'
  131. const char* cmdline4[] = { "", "--open", "" };
  132. options_description desc2;
  133. desc2.add_options()("open", po::value<string>());
  134. variables_map vm;
  135. po::store(
  136. po::parse_command_line(sizeof(cmdline4) / sizeof(const char*),
  137. const_cast<char**>(cmdline4), desc2), vm);
  138. }
  139. void test_multitoken() {
  140. const char* cmdline5[] = { "", "-p7", "-o", "1", "2", "3", "-x8" };
  141. options_description desc3;
  142. desc3.add_options()
  143. (",p", po::value<string>())
  144. (",o", po::value<string>()->multitoken())
  145. (",x", po::value<string>());
  146. vector<option> a5 = parse_command_line(
  147. sizeof(cmdline5) / sizeof(const char*),
  148. const_cast<char**>(cmdline5), desc3, 0, additional_parser).options;
  149. BOOST_CHECK_EQUAL(a5.size(), 3u);
  150. check_value(a5[0], "-p", "7");
  151. BOOST_REQUIRE(a5[1].value.size() == 3);
  152. BOOST_CHECK_EQUAL(a5[1].string_key, "-o");
  153. BOOST_CHECK_EQUAL(a5[1].value[0], "1");
  154. BOOST_CHECK_EQUAL(a5[1].value[1], "2");
  155. BOOST_CHECK_EQUAL(a5[1].value[2], "3");
  156. check_value(a5[2], "-x", "8");
  157. }
  158. void test_multitoken_and_multiname() {
  159. const char* cmdline[] = { "program", "-fone", "-b", "two", "--foo", "three", "four", "-zfive", "--fee", "six" };
  160. options_description desc;
  161. desc.add_options()
  162. ("bar,b", po::value<string>())
  163. ("foo,fee,f", po::value<string>()->multitoken())
  164. ("fizbaz,baz,z", po::value<string>());
  165. vector<option> parsed_options = parse_command_line(
  166. sizeof(cmdline) / sizeof(const char*),
  167. const_cast<char**>(cmdline), desc, 0, additional_parser).options;
  168. BOOST_CHECK_EQUAL(parsed_options.size(), 5u);
  169. check_value(parsed_options[0], "foo", "one");
  170. check_value(parsed_options[1], "bar", "two");
  171. BOOST_CHECK_EQUAL(parsed_options[2].string_key, "foo");
  172. BOOST_REQUIRE(parsed_options[2].value.size() == 2);
  173. BOOST_CHECK_EQUAL(parsed_options[2].value[0], "three");
  174. BOOST_CHECK_EQUAL(parsed_options[2].value[1], "four");
  175. check_value(parsed_options[3], "fizbaz", "five");
  176. check_value(parsed_options[4], "foo", "six");
  177. const char* cmdline_2[] = { "program", "-fone", "-b", "two", "--fee", "three", "four", "-zfive", "--foo", "six" };
  178. parsed_options = parse_command_line(
  179. sizeof(cmdline_2) / sizeof(const char*),
  180. const_cast<char**>(cmdline_2), desc, 0, additional_parser).options;
  181. BOOST_CHECK_EQUAL(parsed_options.size(), 5u);
  182. check_value(parsed_options[0], "foo", "one");
  183. check_value(parsed_options[1], "bar", "two");
  184. BOOST_CHECK_EQUAL(parsed_options[2].string_key, "foo");
  185. BOOST_REQUIRE(parsed_options[2].value.size() == 2);
  186. BOOST_CHECK_EQUAL(parsed_options[2].value[0], "three");
  187. BOOST_CHECK_EQUAL(parsed_options[2].value[1], "four");
  188. check_value(parsed_options[3], "fizbaz", "five");
  189. check_value(parsed_options[4], "foo", "six");
  190. }
  191. void test_multitoken_vector_option() {
  192. po::options_description desc4("");
  193. desc4.add_options()
  194. ("multitoken,multi-token,m", po::value<std::vector<std::string> >()->multitoken(), "values")
  195. ("file", po::value<std::string>(), "the file to process");
  196. po::positional_options_description p;
  197. p.add("file", 1);
  198. const char* cmdline6[] = { "", "-m", "token1", "token2", "--", "some_file" };
  199. vector<option> a6 =
  200. command_line_parser(sizeof(cmdline6) / sizeof(const char*),
  201. const_cast<char**>(cmdline6)).options(desc4).positional(p).run().options;
  202. BOOST_CHECK_EQUAL(a6.size(), 2u);
  203. BOOST_REQUIRE(a6[0].value.size() == 2);
  204. BOOST_CHECK_EQUAL(a6[0].string_key, "multitoken");
  205. BOOST_CHECK_EQUAL(a6[0].value[0], "token1");
  206. BOOST_CHECK_EQUAL(a6[0].value[1], "token2");
  207. BOOST_CHECK_EQUAL(a6[1].string_key, "file");
  208. BOOST_REQUIRE(a6[1].value.size() == 1);
  209. BOOST_CHECK_EQUAL(a6[1].value[0], "some_file");
  210. }
  211. } // namespace command_line
  212. void test_command_line()
  213. {
  214. #if 0
  215. command_line::test_parsing_without_specifying_options();
  216. #endif
  217. command_line::test_many_different_options();
  218. // Check that we don't crash on empty values of type 'string'
  219. command_line::test_not_crashing_with_empty_string_values();
  220. command_line::test_multitoken();
  221. command_line::test_multitoken_vector_option();
  222. command_line::test_multitoken_and_multiname();
  223. }
  224. void test_config_file(const char* config_file)
  225. {
  226. options_description desc;
  227. desc.add_options()
  228. ("gv1", new untyped_value)
  229. ("gv2", new untyped_value)
  230. ("empty_value", new untyped_value)
  231. ("plug*", new untyped_value)
  232. ("m1.v1", new untyped_value)
  233. ("m1.v2", new untyped_value)
  234. ("m1.v3,alias3", new untyped_value)
  235. ("b", bool_switch())
  236. ;
  237. const char content1[] =
  238. " gv1 = 0#asd\n"
  239. "empty_value = \n"
  240. "plug3 = 7\n"
  241. "b = true\n"
  242. "[m1]\n"
  243. "v1 = 1\n"
  244. "\n"
  245. "v2 = 2\n"
  246. "v3 = 3\n"
  247. ;
  248. stringstream ss(content1);
  249. vector<option> a1 = parse_config_file(ss, desc).options;
  250. BOOST_REQUIRE(a1.size() == 7);
  251. check_value(a1[0], "gv1", "0");
  252. check_value(a1[1], "empty_value", "");
  253. check_value(a1[2], "plug3", "7");
  254. check_value(a1[3], "b", "true");
  255. check_value(a1[4], "m1.v1", "1");
  256. check_value(a1[5], "m1.v2", "2");
  257. check_value(a1[6], "m1.v3", "3");
  258. // same test, but now options come from file
  259. vector<option> a2 = parse_config_file<char>(config_file, desc).options;
  260. BOOST_REQUIRE(a2.size() == 7);
  261. check_value(a2[0], "gv1", "0");
  262. check_value(a2[1], "empty_value", "");
  263. check_value(a2[2], "plug3", "7");
  264. check_value(a2[3], "b", "true");
  265. check_value(a2[4], "m1.v1", "1");
  266. check_value(a2[5], "m1.v2", "2");
  267. check_value(a2[6], "m1.v3", "3");
  268. }
  269. void test_environment()
  270. {
  271. options_description desc;
  272. desc.add_options()
  273. ("foo", new untyped_value, "")
  274. ("bar", new untyped_value, "")
  275. ;
  276. #if (defined(_WIN32) && ! defined(__BORLANDC__)) || (defined(__CYGWIN__))
  277. _putenv("PO_TEST_FOO=1");
  278. #else
  279. putenv(const_cast<char*>("PO_TEST_FOO=1"));
  280. #endif
  281. parsed_options p = parse_environment(desc, "PO_TEST_");
  282. BOOST_REQUIRE(p.options.size() == 1);
  283. BOOST_CHECK (p.options[0].string_key == "foo");
  284. BOOST_REQUIRE(p.options[0].value.size() == 1);
  285. BOOST_CHECK (p.options[0].value[0] == "1");
  286. //TODO: since 'bar' does not allow a value, it cannot appear in environemt,
  287. // which already has a value.
  288. }
  289. void test_unregistered()
  290. {
  291. options_description desc;
  292. const char* cmdline1_[] = { "--foo=12", "--bar", "1"};
  293. vector<string> cmdline1 = sv(cmdline1_,
  294. sizeof(cmdline1_)/sizeof(const char*));
  295. vector<option> a1 =
  296. command_line_parser(cmdline1).options(desc).allow_unregistered().run()
  297. .options;
  298. BOOST_REQUIRE(a1.size() == 3);
  299. BOOST_CHECK(a1[0].string_key == "foo");
  300. BOOST_CHECK(a1[0].unregistered == true);
  301. BOOST_REQUIRE(a1[0].value.size() == 1);
  302. BOOST_CHECK(a1[0].value[0] == "12");
  303. BOOST_CHECK(a1[1].string_key == "bar");
  304. BOOST_CHECK(a1[1].unregistered == true);
  305. BOOST_CHECK(a1[2].string_key == "");
  306. BOOST_CHECK(a1[2].unregistered == false);
  307. vector<string> a2 = collect_unrecognized(a1, include_positional);
  308. BOOST_CHECK(a2[0] == "--foo=12");
  309. BOOST_CHECK(a2[1] == "--bar");
  310. BOOST_CHECK(a2[2] == "1");
  311. // Test that storing unregisted options has no effect
  312. variables_map vm;
  313. store(command_line_parser(cmdline1).options(desc).
  314. allow_unregistered().run(),
  315. vm);
  316. BOOST_CHECK_EQUAL(vm.size(), 0u);
  317. const char content1[] =
  318. "gv1 = 0\n"
  319. "[m1]\n"
  320. "v1 = 1\n"
  321. ;
  322. stringstream ss(content1);
  323. vector<option> a3 = parse_config_file(ss, desc, true).options;
  324. BOOST_REQUIRE(a3.size() == 2);
  325. cout << "XXX" << a3[0].value.front() << "\n";
  326. check_value(a3[0], "gv1", "0");
  327. check_value(a3[1], "m1.v1", "1");
  328. }
  329. int main(int, char* av[])
  330. {
  331. test_command_line();
  332. test_config_file(av[1]);
  333. test_environment();
  334. test_unregistered();
  335. return 0;
  336. }