File: positional_options_test.cpp

package info (click to toggle)
boost1.83 1.83.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 545,632 kB
  • sloc: cpp: 3,857,086; xml: 125,552; ansic: 34,414; python: 25,887; asm: 5,276; sh: 4,799; ada: 1,681; makefile: 1,629; perl: 1,212; pascal: 1,139; sql: 810; yacc: 478; ruby: 102; lisp: 24; csh: 6
file content (91 lines) | stat: -rw-r--r-- 2,817 bytes parent folder | download | duplicates (18)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Copyright Vladimir Prus 2004.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt
// or copy at http://www.boost.org/LICENSE_1_0.txt)

#include <boost/program_options/positional_options.hpp>
#include <boost/program_options/options_description.hpp>
#include <boost/program_options/parsers.hpp>
using namespace boost::program_options;
// We'll use po::value everywhere to workaround vc6 bug.
namespace po = boost::program_options;


#include <boost/limits.hpp>

#include "minitest.hpp"

#include <vector>
using namespace std;

void test_positional_options()
{
    positional_options_description p;
    p.add("first", 1);

    BOOST_CHECK_EQUAL(p.max_total_count(), 1u);
    BOOST_CHECK_EQUAL(p.name_for_position(0), "first");

    p.add("second", 2);

    BOOST_CHECK_EQUAL(p.max_total_count(), 3u);
    BOOST_CHECK_EQUAL(p.name_for_position(0), "first");
    BOOST_CHECK_EQUAL(p.name_for_position(1), "second");
    BOOST_CHECK_EQUAL(p.name_for_position(2), "second");

    p.add("third", -1);

    BOOST_CHECK_EQUAL(p.max_total_count(), (std::numeric_limits<unsigned>::max)());
    BOOST_CHECK_EQUAL(p.name_for_position(0), "first");
    BOOST_CHECK_EQUAL(p.name_for_position(1), "second");
    BOOST_CHECK_EQUAL(p.name_for_position(2), "second");
    BOOST_CHECK_EQUAL(p.name_for_position(3), "third");
    BOOST_CHECK_EQUAL(p.name_for_position(10000), "third");
}

void test_parsing()
{
    options_description desc;
    desc.add_options()
        ("first", po::value<int>())
        ("second", po::value<int>())
        ("input-file", po::value< vector<string> >())
        ("some-other", po::value<string>())
    ;

    positional_options_description p;
    p.add("input-file", 2).add("some-other", 1);

    vector<string> args;
    args.push_back("--first=10");
    args.push_back("file1");
    args.push_back("--second=10");
    args.push_back("file2");
    args.push_back("file3");

    // Check that positional options are handled.
    parsed_options parsed = 
        command_line_parser(args).options(desc).positional(p).run();

    BOOST_REQUIRE(parsed.options.size() == 5);
    BOOST_CHECK_EQUAL(parsed.options[1].string_key, "input-file");
    BOOST_CHECK_EQUAL(parsed.options[1].value[0], "file1");
    BOOST_CHECK_EQUAL(parsed.options[3].string_key, "input-file");
    BOOST_CHECK_EQUAL(parsed.options[3].value[0], "file2");
    BOOST_CHECK_EQUAL(parsed.options[4].value[0], "file3");

    args.push_back("file4");

    // Check that excessive number of positional options is detected.
    BOOST_CHECK_THROW(command_line_parser(args).options(desc).positional(p)
                      .run(),
                      too_many_positional_options_error);
}

int main(int, char* [])
{
    test_positional_options();
    test_parsing();
    return 0;
}