• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright Vladimir Prus 2004.
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 
6 #include <boost/program_options/positional_options.hpp>
7 #include <boost/program_options/options_description.hpp>
8 #include <boost/program_options/parsers.hpp>
9 using namespace boost::program_options;
10 // We'll use po::value everywhere to workaround vc6 bug.
11 namespace po = boost::program_options;
12 
13 
14 #include <boost/limits.hpp>
15 
16 #include "minitest.hpp"
17 
18 #include <vector>
19 using namespace std;
20 
test_positional_options()21 void test_positional_options()
22 {
23     positional_options_description p;
24     p.add("first", 1);
25 
26     BOOST_CHECK_EQUAL(p.max_total_count(), 1u);
27     BOOST_CHECK_EQUAL(p.name_for_position(0), "first");
28 
29     p.add("second", 2);
30 
31     BOOST_CHECK_EQUAL(p.max_total_count(), 3u);
32     BOOST_CHECK_EQUAL(p.name_for_position(0), "first");
33     BOOST_CHECK_EQUAL(p.name_for_position(1), "second");
34     BOOST_CHECK_EQUAL(p.name_for_position(2), "second");
35 
36     p.add("third", -1);
37 
38     BOOST_CHECK_EQUAL(p.max_total_count(), (std::numeric_limits<unsigned>::max)());
39     BOOST_CHECK_EQUAL(p.name_for_position(0), "first");
40     BOOST_CHECK_EQUAL(p.name_for_position(1), "second");
41     BOOST_CHECK_EQUAL(p.name_for_position(2), "second");
42     BOOST_CHECK_EQUAL(p.name_for_position(3), "third");
43     BOOST_CHECK_EQUAL(p.name_for_position(10000), "third");
44 }
45 
test_parsing()46 void test_parsing()
47 {
48     options_description desc;
49     desc.add_options()
50         ("first", po::value<int>())
51         ("second", po::value<int>())
52         ("input-file", po::value< vector<string> >())
53         ("some-other", po::value<string>())
54     ;
55 
56     positional_options_description p;
57     p.add("input-file", 2).add("some-other", 1);
58 
59     vector<string> args;
60     args.push_back("--first=10");
61     args.push_back("file1");
62     args.push_back("--second=10");
63     args.push_back("file2");
64     args.push_back("file3");
65 
66     // Check that positional options are handled.
67     parsed_options parsed =
68         command_line_parser(args).options(desc).positional(p).run();
69 
70     BOOST_REQUIRE(parsed.options.size() == 5);
71     BOOST_CHECK_EQUAL(parsed.options[1].string_key, "input-file");
72     BOOST_CHECK_EQUAL(parsed.options[1].value[0], "file1");
73     BOOST_CHECK_EQUAL(parsed.options[3].string_key, "input-file");
74     BOOST_CHECK_EQUAL(parsed.options[3].value[0], "file2");
75     BOOST_CHECK_EQUAL(parsed.options[4].value[0], "file3");
76 
77     args.push_back("file4");
78 
79     // Check that excessive number of positional options is detected.
80     BOOST_CHECK_THROW(command_line_parser(args).options(desc).positional(p)
81                       .run(),
82                       too_many_positional_options_error);
83 }
84 
main(int,char * [])85 int main(int, char* [])
86 {
87     test_positional_options();
88     test_parsing();
89     return 0;
90 }
91 
92