exception_test.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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 test_ambiguous()
  17. {
  18. options_description desc;
  19. desc.add_options()
  20. ("cfgfile,c", value<string>()->multitoken(), "the config file")
  21. ("output,c", value<string>(), "the output file")
  22. ("output,o", value<string>(), "the output file")
  23. ;
  24. const char* cmdline[] = {"program", "-c", "file", "-o", "anotherfile"};
  25. variables_map vm;
  26. try {
  27. store(parse_command_line(sizeof(cmdline)/sizeof(const char*),
  28. const_cast<char**>(cmdline), desc), vm);
  29. }
  30. catch (ambiguous_option& e)
  31. {
  32. BOOST_CHECK_EQUAL(e.alternatives().size(), 2);
  33. BOOST_CHECK_EQUAL(e.get_option_name(), "-c");
  34. BOOST_CHECK_EQUAL(e.alternatives()[0], "cfgfile");
  35. BOOST_CHECK_EQUAL(e.alternatives()[1], "output");
  36. }
  37. }
  38. void test_ambiguous_long()
  39. {
  40. options_description desc;
  41. desc.add_options()
  42. ("cfgfile,c", value<string>()->multitoken(), "the config file")
  43. ("output,c", value<string>(), "the output file")
  44. ("output,o", value<string>(), "the output file")
  45. ;
  46. const char* cmdline[] = {"program", "--cfgfile", "file", "--output", "anotherfile"};
  47. variables_map vm;
  48. try {
  49. store(parse_command_line(sizeof(cmdline)/sizeof(const char*),
  50. const_cast<char**>(cmdline), desc), vm);
  51. }
  52. catch (ambiguous_option& e)
  53. {
  54. BOOST_CHECK_EQUAL(e.alternatives().size(), 2);
  55. BOOST_CHECK_EQUAL(e.get_option_name(), "--output");
  56. BOOST_CHECK_EQUAL(e.alternatives()[0], "output");
  57. BOOST_CHECK_EQUAL(e.alternatives()[1], "output");
  58. }
  59. }
  60. void test_ambiguous_multiple_long_names()
  61. {
  62. options_description desc;
  63. desc.add_options()
  64. ("cfgfile,foo,c", value<string>()->multitoken(), "the config file")
  65. ("output,foo,o", value<string>(), "the output file")
  66. ;
  67. const char* cmdline[] = {"program", "--foo", "file"};
  68. variables_map vm;
  69. try {
  70. store(parse_command_line(sizeof(cmdline)/sizeof(const char*),
  71. const_cast<char**>(cmdline), desc), vm);
  72. }
  73. catch (ambiguous_option& e)
  74. {
  75. BOOST_CHECK_EQUAL(e.alternatives().size(), 2);
  76. BOOST_CHECK_EQUAL(e.get_option_name(), "--foo");
  77. BOOST_CHECK_EQUAL(e.alternatives()[0], "cfgfile");
  78. BOOST_CHECK_EQUAL(e.alternatives()[1], "output");
  79. }
  80. }
  81. void test_unknown_option()
  82. {
  83. options_description desc;
  84. desc.add_options()
  85. ("cfgfile,c", value<string>(), "the configfile")
  86. ;
  87. const char* cmdline[] = {"program", "-c", "file", "-f", "anotherfile"};
  88. variables_map vm;
  89. try {
  90. store(parse_command_line(sizeof(cmdline)/sizeof(const char*),
  91. const_cast<char**>(cmdline), desc), vm);
  92. }
  93. catch (unknown_option& e)
  94. {
  95. BOOST_CHECK_EQUAL(e.get_option_name(), "-f");
  96. BOOST_CHECK_EQUAL(string(e.what()), "unrecognised option '-f'");
  97. }
  98. }
  99. void test_multiple_values()
  100. {
  101. options_description desc;
  102. desc.add_options()
  103. ("cfgfile,c", value<string>()->multitoken(), "the config file")
  104. ("output,o", value<string>(), "the output file")
  105. ;
  106. const char* cmdline[] = { "program", "-o", "fritz", "hugo", "--cfgfile", "file", "c", "-o", "text.out" };
  107. variables_map vm;
  108. try {
  109. store(parse_command_line(sizeof(cmdline)/sizeof(const char*),
  110. const_cast<char**>(cmdline), desc), vm);
  111. notify(vm);
  112. }
  113. catch (validation_error& e)
  114. {
  115. // TODO: this is currently validation_error, shouldn't it be multiple_values ???
  116. //
  117. // multiple_values is thrown only at one place untyped_value::xparse(),
  118. // but I think this can never be reached
  119. // because: untyped_value always has one value and this is filtered before reach specific
  120. // validation and parsing
  121. //
  122. BOOST_CHECK_EQUAL(e.get_option_name(), "--cfgfile");
  123. BOOST_CHECK_EQUAL(string(e.what()), "option '--cfgfile' only takes a single argument");
  124. }
  125. }
  126. void test_multiple_occurrences()
  127. {
  128. options_description desc;
  129. desc.add_options()
  130. ("cfgfile,c", value<string>(), "the configfile")
  131. ;
  132. const char* cmdline[] = {"program", "--cfgfile", "file", "-c", "anotherfile"};
  133. variables_map vm;
  134. try {
  135. store(parse_command_line(sizeof(cmdline)/sizeof(const char*),
  136. const_cast<char**>(cmdline), desc), vm);
  137. notify(vm);
  138. }
  139. catch (multiple_occurrences& e)
  140. {
  141. BOOST_CHECK_EQUAL(e.get_option_name(), "--cfgfile");
  142. BOOST_CHECK_EQUAL(string(e.what()), "option '--cfgfile' cannot be specified more than once");
  143. }
  144. }
  145. void test_multiple_occurrences_with_different_names()
  146. {
  147. options_description desc;
  148. desc.add_options()
  149. ("cfgfile,config-file,c", value<string>(), "the configfile")
  150. ;
  151. const char* cmdline[] = {"program", "--config-file", "file", "--cfgfile", "anotherfile"};
  152. variables_map vm;
  153. try {
  154. store(parse_command_line(sizeof(cmdline)/sizeof(const char*),
  155. const_cast<char**>(cmdline), desc), vm);
  156. notify(vm);
  157. }
  158. catch (multiple_occurrences& e)
  159. {
  160. BOOST_CHECK( (e.get_option_name() == "--cfgfile") || (e.get_option_name() == "--config-file"));
  161. BOOST_CHECK(
  162. (string(e.what()) == "option '--cfgfile' cannot be specified more than once") ||
  163. (string(e.what()) == "option '--config-file' cannot be specified more than once")
  164. );
  165. }
  166. }
  167. void test_multiple_occurrences_with_non_key_names()
  168. {
  169. options_description desc;
  170. desc.add_options()
  171. ("cfgfile,config-file,c", value<string>(), "the configfile")
  172. ;
  173. const char* cmdline[] = {"program", "--config-file", "file", "-c", "anotherfile"};
  174. variables_map vm;
  175. try {
  176. store(parse_command_line(sizeof(cmdline)/sizeof(const char*),
  177. const_cast<char**>(cmdline), desc), vm);
  178. notify(vm);
  179. }
  180. catch (multiple_occurrences& e)
  181. {
  182. BOOST_CHECK_EQUAL(e.get_option_name(), "--cfgfile");
  183. BOOST_CHECK_EQUAL(string(e.what()), "option '--cfgfile' cannot be specified more than once");
  184. }
  185. }
  186. void test_missing_value()
  187. {
  188. options_description desc;
  189. desc.add_options()
  190. ("cfgfile,c", value<string>()->multitoken(), "the config file")
  191. ("output,o", value<string>(), "the output file")
  192. ;
  193. // missing value for option '-c'
  194. const char* cmdline[] = { "program", "-c", "-c", "output.txt"};
  195. variables_map vm;
  196. try {
  197. store(parse_command_line(sizeof(cmdline)/sizeof(const char*),
  198. const_cast<char**>(cmdline), desc), vm);
  199. notify(vm);
  200. }
  201. catch (invalid_command_line_syntax& e)
  202. {
  203. BOOST_CHECK_EQUAL(e.kind(), invalid_syntax::missing_parameter);
  204. BOOST_CHECK_EQUAL(e.tokens(), "--cfgfile");
  205. }
  206. }
  207. int main(int /*ac*/, char** /*av*/)
  208. {
  209. test_ambiguous();
  210. test_ambiguous_long();
  211. test_ambiguous_multiple_long_names();
  212. test_unknown_option();
  213. test_multiple_values();
  214. test_multiple_occurrences();
  215. test_multiple_occurrences_with_different_names();
  216. test_multiple_occurrences_with_non_key_names();
  217. test_missing_value();
  218. return 0;
  219. }