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 | |
6 | |
7 | #include <boost/program_options/cmdline.hpp> |
8 | #include <boost/program_options/options_description.hpp> |
9 | #include <boost/program_options/parsers.hpp> |
10 | #include <boost/program_options/detail/cmdline.hpp> |
11 | using namespace boost::program_options; |
12 | using boost::program_options::detail::cmdline; |
13 | |
14 | #include <iostream> |
15 | #include <sstream> |
16 | #include <vector> |
17 | #include <cassert> |
18 | using namespace std; |
19 | |
20 | #include "minitest.hpp" |
21 | |
22 | |
23 | // Test free function collect_unrecognized() |
24 | // |
25 | // it collects the tokens of all not registered options. It can be used |
26 | // to pass them to an own parser implementation |
27 | |
28 | |
29 | |
30 | void test_unrecognize_cmdline() |
31 | { |
32 | options_description desc; |
33 | |
34 | string content = "prg --input input.txt --optimization 4 --opt option" ; |
35 | vector< string > tokens = split_unix(cmdline: content); |
36 | |
37 | cmdline cmd(tokens); |
38 | cmd.set_options_description(desc); |
39 | cmd.allow_unregistered(); |
40 | |
41 | vector< option > opts = cmd.run(); |
42 | vector< string > result = collect_unrecognized(options: opts, mode: include_positional); |
43 | |
44 | BOOST_CHECK_EQUAL(result.size(), 7); |
45 | BOOST_CHECK_EQUAL(result[0], "prg" ); |
46 | BOOST_CHECK_EQUAL(result[1], "--input" ); |
47 | BOOST_CHECK_EQUAL(result[2], "input.txt" ); |
48 | BOOST_CHECK_EQUAL(result[3], "--optimization" ); |
49 | BOOST_CHECK_EQUAL(result[4], "4" ); |
50 | BOOST_CHECK_EQUAL(result[5], "--opt" ); |
51 | BOOST_CHECK_EQUAL(result[6], "option" ); |
52 | } |
53 | |
54 | |
55 | |
56 | void test_unrecognize_config() |
57 | { |
58 | |
59 | options_description desc; |
60 | |
61 | string content = |
62 | " input = input.txt\n" |
63 | " optimization = 4\n" |
64 | " opt = option\n" |
65 | ; |
66 | |
67 | stringstream ss(content); |
68 | vector< option > opts = parse_config_file(ss, desc, allow_unregistered: true).options; |
69 | vector< string > result = collect_unrecognized(options: opts, mode: include_positional); |
70 | |
71 | BOOST_CHECK_EQUAL(result.size(), 6); |
72 | BOOST_CHECK_EQUAL(result[0], "input" ); |
73 | BOOST_CHECK_EQUAL(result[1], "input.txt" ); |
74 | BOOST_CHECK_EQUAL(result[2], "optimization" ); |
75 | BOOST_CHECK_EQUAL(result[3], "4" ); |
76 | BOOST_CHECK_EQUAL(result[4], "opt" ); |
77 | BOOST_CHECK_EQUAL(result[5], "option" ); |
78 | } |
79 | |
80 | |
81 | |
82 | int main(int /*ac*/, char** /*av*/) |
83 | { |
84 | test_unrecognize_cmdline(); |
85 | test_unrecognize_config(); |
86 | |
87 | return 0; |
88 | } |
89 | |