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
5#include <boost/program_options.hpp>
6namespace po = boost::program_options;
7
8#include <boost/optional.hpp>
9
10#include <string>
11
12#include "minitest.hpp"
13
14std::vector<std::string> sv(const char* array[], unsigned size)
15{
16 std::vector<std::string> r;
17 for (unsigned i = 0; i < size; ++i)
18 r.push_back(x: array[i]);
19 return r;
20}
21
22void test_optional()
23{
24 boost::optional<int> foo, bar, baz;
25
26 po::options_description desc;
27 desc.add_options()
28 ("foo,f", po::value(v: &foo), "")
29 ("bar,b", po::value(v: &bar), "")
30 ("baz,z", po::value(v: &baz), "")
31 ;
32
33 const char* cmdline1_[] = { "--foo=12", "--bar", "1"};
34 std::vector<std::string> cmdline1 = sv(array: cmdline1_,
35 size: sizeof(cmdline1_)/sizeof(const char*));
36 po::variables_map vm;
37 po::store(options: po::command_line_parser(cmdline1).options(desc).run(), m&: vm);
38 po::notify(m&: vm);
39
40 BOOST_REQUIRE(!!foo);
41 BOOST_CHECK(*foo == 12);
42
43 BOOST_REQUIRE(!!bar);
44 BOOST_CHECK(*bar == 1);
45
46 BOOST_CHECK(!baz);
47}
48
49int main(int, char*[])
50{
51 test_optional();
52 return 0;
53}
54

source code of boost/libs/program_options/test/optional_test.cpp