required_test.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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.hpp>
  6. using namespace boost::program_options;
  7. #include <string>
  8. #include <iostream>
  9. #include <fstream>
  10. using namespace std;
  11. #include "minitest.hpp"
  12. void required_throw_test()
  13. {
  14. options_description opts;
  15. opts.add_options()
  16. ("cfgfile,c", value<string>()->required(), "the configfile")
  17. ("fritz,f", value<string>()->required(), "the output file")
  18. ;
  19. variables_map vm;
  20. bool thrown = false;
  21. {
  22. // This test must throw exception
  23. string cmdline = "prg -f file.txt";
  24. vector< string > tokens = split_unix(cmdline);
  25. thrown = false;
  26. try {
  27. store(command_line_parser(tokens).options(opts).run(), vm);
  28. notify(vm);
  29. }
  30. catch (required_option& e) {
  31. BOOST_CHECK_EQUAL(e.what(), string("the option '--cfgfile' is required but missing"));
  32. thrown = true;
  33. }
  34. BOOST_CHECK(thrown);
  35. }
  36. {
  37. // This test mustn't throw exception
  38. string cmdline = "prg -c config.txt";
  39. vector< string > tokens = split_unix(cmdline);
  40. thrown = false;
  41. try {
  42. store(command_line_parser(tokens).options(opts).run(), vm);
  43. notify(vm);
  44. }
  45. catch (required_option& e) {
  46. thrown = true;
  47. }
  48. BOOST_CHECK(!thrown);
  49. }
  50. }
  51. void simple_required_test(const char* config_file)
  52. {
  53. options_description opts;
  54. opts.add_options()
  55. ("cfgfile,c", value<string>()->required(), "the configfile")
  56. ("fritz,f", value<string>()->required(), "the output file")
  57. ;
  58. variables_map vm;
  59. bool thrown = false;
  60. {
  61. // This test must throw exception
  62. string cmdline = "prg -f file.txt";
  63. vector< string > tokens = split_unix(cmdline);
  64. thrown = false;
  65. try {
  66. // options coming from different sources
  67. store(command_line_parser(tokens).options(opts).run(), vm);
  68. store(parse_config_file<char>(config_file, opts), vm);
  69. notify(vm);
  70. }
  71. catch (required_option& e) {
  72. thrown = true;
  73. }
  74. BOOST_CHECK(!thrown);
  75. }
  76. }
  77. void multiname_required_test()
  78. {
  79. options_description opts;
  80. opts.add_options()
  81. ("foo,bar", value<string>()->required(), "the foo")
  82. ;
  83. variables_map vm;
  84. bool thrown = false;
  85. {
  86. // This test must throw exception
  87. string cmdline = "prg --bar file.txt";
  88. vector< string > tokens = split_unix(cmdline);
  89. thrown = false;
  90. try {
  91. // options coming from different sources
  92. store(command_line_parser(tokens).options(opts).run(), vm);
  93. notify(vm);
  94. }
  95. catch (required_option& e) {
  96. thrown = true;
  97. }
  98. BOOST_CHECK(!thrown);
  99. }
  100. }
  101. int main(int /*argc*/, char* av[])
  102. {
  103. required_throw_test();
  104. simple_required_test(av[1]);
  105. multiname_required_test();
  106. return 0;
  107. }