split_test.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. // Copyright Sascha Ochsenknecht 2009.
  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. #include <boost/program_options/cmdline.hpp>
  9. using namespace boost::program_options;
  10. #include <iostream>
  11. #include <sstream>
  12. #include <vector>
  13. #include <cassert>
  14. using namespace std;
  15. #include "minitest.hpp"
  16. void check_value(const string& option, const string& value)
  17. {
  18. BOOST_CHECK(option == value);
  19. }
  20. void split_whitespace(const options_description& description)
  21. {
  22. const char* cmdline = "prg --input input.txt \r --optimization 4 \t --opt \n option";
  23. vector< string > tokens = split_unix(cmdline, " \t\n\r");
  24. BOOST_REQUIRE(tokens.size() == 7);
  25. check_value(tokens[0], "prg");
  26. check_value(tokens[1], "--input");
  27. check_value(tokens[2], "input.txt");
  28. check_value(tokens[3], "--optimization");
  29. check_value(tokens[4], "4");
  30. check_value(tokens[5], "--opt");
  31. check_value(tokens[6], "option");
  32. variables_map vm;
  33. store(command_line_parser(tokens).options(description).run(), vm);
  34. notify(vm);
  35. }
  36. void split_equalsign(const options_description& description)
  37. {
  38. const char* cmdline = "prg --input=input.txt --optimization=4 --opt=option";
  39. vector< string > tokens = split_unix(cmdline, "= ");
  40. BOOST_REQUIRE(tokens.size() == 7);
  41. check_value(tokens[0], "prg");
  42. check_value(tokens[1], "--input");
  43. check_value(tokens[2], "input.txt");
  44. check_value(tokens[3], "--optimization");
  45. check_value(tokens[4], "4");
  46. check_value(tokens[5], "--opt");
  47. check_value(tokens[6], "option");
  48. variables_map vm;
  49. store(command_line_parser(tokens).options(description).run(), vm);
  50. notify(vm);
  51. }
  52. void split_semi(const options_description& description)
  53. {
  54. const char* cmdline = "prg;--input input.txt;--optimization 4;--opt option";
  55. vector< string > tokens = split_unix(cmdline, "; ");
  56. BOOST_REQUIRE(tokens.size() == 7);
  57. check_value(tokens[0], "prg");
  58. check_value(tokens[1], "--input");
  59. check_value(tokens[2], "input.txt");
  60. check_value(tokens[3], "--optimization");
  61. check_value(tokens[4], "4");
  62. check_value(tokens[5], "--opt");
  63. check_value(tokens[6], "option");
  64. variables_map vm;
  65. store(command_line_parser(tokens).options(description).run(), vm);
  66. notify(vm);
  67. }
  68. void split_quotes(const options_description& description)
  69. {
  70. const char* cmdline = "prg --input \"input.txt input.txt\" --optimization 4 --opt \"option1 option2\"";
  71. vector< string > tokens = split_unix(cmdline, " ");
  72. BOOST_REQUIRE(tokens.size() == 7);
  73. check_value(tokens[0], "prg");
  74. check_value(tokens[1], "--input");
  75. check_value(tokens[2], "input.txt input.txt");
  76. check_value(tokens[3], "--optimization");
  77. check_value(tokens[4], "4");
  78. check_value(tokens[5], "--opt");
  79. check_value(tokens[6], "option1 option2");
  80. variables_map vm;
  81. store(command_line_parser(tokens).options(description).run(), vm);
  82. notify(vm);
  83. }
  84. void split_escape(const options_description& description)
  85. {
  86. const char* cmdline = "prg --input \\\"input.txt\\\" --optimization 4 --opt \\\"option1\\ option2\\\"";
  87. vector< string > tokens = split_unix(cmdline, " ");
  88. BOOST_REQUIRE(tokens.size() == 7);
  89. check_value(tokens[0], "prg");
  90. check_value(tokens[1], "--input");
  91. check_value(tokens[2], "\"input.txt\"");
  92. check_value(tokens[3], "--optimization");
  93. check_value(tokens[4], "4");
  94. check_value(tokens[5], "--opt");
  95. check_value(tokens[6], "\"option1 option2\"");
  96. variables_map vm;
  97. store(command_line_parser(tokens).options(description).run(), vm);
  98. notify(vm);
  99. }
  100. void split_single_quote(const options_description& description)
  101. {
  102. const char* cmdline = "prg --input 'input.txt input.txt' --optimization 4 --opt 'option1 option2'";
  103. vector< string > tokens = split_unix(cmdline, " ", "'");
  104. BOOST_REQUIRE(tokens.size() == 7);
  105. check_value(tokens[0], "prg");
  106. check_value(tokens[1], "--input");
  107. check_value(tokens[2], "input.txt input.txt");
  108. check_value(tokens[3], "--optimization");
  109. check_value(tokens[4], "4");
  110. check_value(tokens[5], "--opt");
  111. check_value(tokens[6], "option1 option2");
  112. variables_map vm;
  113. store(command_line_parser(tokens).options(description).run(), vm);
  114. notify(vm);
  115. }
  116. void split_defaults(const options_description& description)
  117. {
  118. const char* cmdline = "prg --input \t \'input file.txt\' \t --optimization 4 --opt \\\"option1\\ option2\\\"";
  119. vector< string > tokens = split_unix(cmdline);
  120. BOOST_REQUIRE(tokens.size() == 7);
  121. check_value(tokens[0], "prg");
  122. check_value(tokens[1], "--input");
  123. check_value(tokens[2], "input file.txt");
  124. check_value(tokens[3], "--optimization");
  125. check_value(tokens[4], "4");
  126. check_value(tokens[5], "--opt");
  127. check_value(tokens[6], "\"option1 option2\"");
  128. variables_map vm;
  129. store(command_line_parser(tokens).options(description).run(), vm);
  130. notify(vm);
  131. }
  132. int main(int /*ac*/, char** /*av*/)
  133. {
  134. options_description desc;
  135. desc.add_options()
  136. ("input,i", value<string>(), "the input file")
  137. ("optimization,O", value<unsigned>(), "optimization level")
  138. ("opt,o", value<string>(), "misc option")
  139. ;
  140. split_whitespace(desc);
  141. split_equalsign(desc);
  142. split_semi(desc);
  143. split_quotes(desc);
  144. split_escape(desc);
  145. split_single_quote(desc);
  146. split_defaults(desc);
  147. return 0;
  148. }