optional_test.cpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // Distributed under the Boost Software License, Version 1.0.
  2. // (See accompanying file LICENSE_1_0.txt
  3. // or copy at http://www.boost.org/LICENSE_1_0.txt)
  4. #include <boost/program_options.hpp>
  5. namespace po = boost::program_options;
  6. #include <boost/optional.hpp>
  7. #include <string>
  8. #include "minitest.hpp"
  9. std::vector<std::string> sv(const char* array[], unsigned size)
  10. {
  11. std::vector<std::string> r;
  12. for (unsigned i = 0; i < size; ++i)
  13. r.push_back(array[i]);
  14. return r;
  15. }
  16. void test_optional()
  17. {
  18. boost::optional<int> foo, bar, baz;
  19. po::options_description desc;
  20. desc.add_options()
  21. ("foo,f", po::value(&foo), "")
  22. ("bar,b", po::value(&bar), "")
  23. ("baz,z", po::value(&baz), "")
  24. ;
  25. const char* cmdline1_[] = { "--foo=12", "--bar", "1"};
  26. std::vector<std::string> cmdline1 = sv(cmdline1_,
  27. sizeof(cmdline1_)/sizeof(const char*));
  28. po::variables_map vm;
  29. po::store(po::command_line_parser(cmdline1).options(desc).run(), vm);
  30. po::notify(vm);
  31. BOOST_REQUIRE(!!foo);
  32. BOOST_CHECK(*foo == 12);
  33. BOOST_REQUIRE(!!bar);
  34. BOOST_CHECK(*bar == 1);
  35. BOOST_CHECK(!baz);
  36. }
  37. int main(int, char*[])
  38. {
  39. test_optional();
  40. return 0;
  41. }