1 | // Copyright Vladimir Prus 2002-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 | |
---|
7 | #include <boost/program_options/parsers.hpp> |
---|
8 | #include <boost/program_options/options_description.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 | #include <boost/function.hpp> |
---|
14 | using namespace boost; |
---|
15 | |
---|
16 | #define BOOST_INCLUDE_MAIN // for testing, include rather than link |
---|
17 | #include <boost/test/test_tools.hpp> |
---|
18 | |
---|
19 | #include <sstream> |
---|
20 | using namespace std; |
---|
21 | |
---|
22 | #include <cstdlib> // for putenv |
---|
23 | |
---|
24 | #define TEST_CHECK_THROW(expression, exception, description) \ |
---|
25 | try \ |
---|
26 | { \ |
---|
27 | expression; \ |
---|
28 | BOOST_ERROR(description);\ |
---|
29 | throw 10; \ |
---|
30 | } \ |
---|
31 | catch(exception &) \ |
---|
32 | { \ |
---|
33 | } |
---|
34 | |
---|
35 | pair<string, vector< vector<string> > > msp(const string& s1) |
---|
36 | { |
---|
37 | return std::make_pair(s1, vector< vector<string> >()); |
---|
38 | } |
---|
39 | |
---|
40 | |
---|
41 | pair<string, vector< vector<string> > > msp(const string& s1, const string& s2) |
---|
42 | { |
---|
43 | vector< vector<string> > v(1); |
---|
44 | v[0].push_back(s2); |
---|
45 | return std::make_pair(s1, v); |
---|
46 | } |
---|
47 | |
---|
48 | void check_value(const option& option, const char* name, const char* value) |
---|
49 | { |
---|
50 | BOOST_CHECK(option.string_key == name); |
---|
51 | BOOST_REQUIRE(option.value.size() == 1); |
---|
52 | BOOST_CHECK(option.value.front() == value); |
---|
53 | } |
---|
54 | |
---|
55 | vector<string> sv(char* array[], unsigned size) |
---|
56 | { |
---|
57 | vector<string> r; |
---|
58 | for (unsigned i = 0; i < size; ++i) |
---|
59 | r.push_back(array[i]); |
---|
60 | return r; |
---|
61 | } |
---|
62 | |
---|
63 | pair<string, string> additional_parser(const std::string&) |
---|
64 | { |
---|
65 | return pair<string, string>(); |
---|
66 | } |
---|
67 | |
---|
68 | void test_command_line() |
---|
69 | { |
---|
70 | // The following commented out blocks used to test parsing |
---|
71 | // command line without syntax specification behaviour. |
---|
72 | // It is disabled now and probably will never be enabled again: |
---|
73 | // it is not possible to figure out what command line means without |
---|
74 | // user's help. |
---|
75 | #if 0 |
---|
76 | char* cmdline1[] = { "--a", "--b=12", "-f", "-g4", "-", "file" }; |
---|
77 | |
---|
78 | options_and_arguments a1 = |
---|
79 | parse_command_line(cmdline1, |
---|
80 | cmdline1 + sizeof(cmdline1)/sizeof(cmdline1[0])); |
---|
81 | |
---|
82 | BOOST_REQUIRE(a1.options().size() == 4); |
---|
83 | BOOST_CHECK(a1.options()[0] == msp("a", "")); |
---|
84 | BOOST_CHECK(a1.options()[1] == msp("b", "12")); |
---|
85 | BOOST_CHECK(a1.options()[2] == msp("-f", "")); |
---|
86 | BOOST_CHECK(a1.options()[3] == msp("-g", "4")); |
---|
87 | BOOST_REQUIRE(a1.arguments().size() == 2); |
---|
88 | BOOST_CHECK(a1.arguments()[0] == "-"); |
---|
89 | BOOST_CHECK(a1.arguments()[1] == "file"); |
---|
90 | |
---|
91 | char* cmdline2[] = { "--a", "--", "file" }; |
---|
92 | |
---|
93 | options_and_arguments a2 = |
---|
94 | parse_command_line(cmdline2, |
---|
95 | cmdline2 + sizeof(cmdline2)/sizeof(cmdline2[0])); |
---|
96 | |
---|
97 | BOOST_REQUIRE(a2.options().size() == 1); |
---|
98 | BOOST_CHECK(a2.options()[0] == msp("a", "")); |
---|
99 | BOOST_CHECK(a2.arguments().size() == 1); |
---|
100 | BOOST_CHECK(a2.arguments()[0] == "file"); |
---|
101 | #endif |
---|
102 | |
---|
103 | options_description desc; |
---|
104 | desc.add_options() |
---|
105 | ("foo,f", new untyped_value(), "") |
---|
106 | // Explicit qualification is a workaround for vc6 |
---|
107 | ("bar,b", po::value<std::string>(), "") |
---|
108 | ("baz", new untyped_value()) |
---|
109 | ("plug*", new untyped_value()) |
---|
110 | ; |
---|
111 | char* cmdline3_[] = { "--foo=12", "-f4", "--bar=11", "-b4", |
---|
112 | "--plug3=10"}; |
---|
113 | vector<string> cmdline3 = sv(cmdline3_, |
---|
114 | sizeof(cmdline3_)/sizeof(cmdline3_[0])); |
---|
115 | vector<option> a3 = |
---|
116 | command_line_parser(cmdline3).options(desc).run().options; |
---|
117 | |
---|
118 | BOOST_CHECK_EQUAL(a3.size(), 5u); |
---|
119 | |
---|
120 | check_value(a3[0], "foo", "12"); |
---|
121 | check_value(a3[1], "foo", "4"); |
---|
122 | check_value(a3[2], "bar", "11"); |
---|
123 | check_value(a3[3], "bar", "4"); |
---|
124 | check_value(a3[4], "plug3", "10"); |
---|
125 | |
---|
126 | // Regression test: check that '0' as style is interpreted as |
---|
127 | // 'default_style' |
---|
128 | vector<option> a4 = |
---|
129 | parse_command_line(5, cmdline3_, desc, 0, additional_parser).options; |
---|
130 | |
---|
131 | BOOST_CHECK_EQUAL(a4.size(), 4u); |
---|
132 | check_value(a4[0], "foo", "4"); |
---|
133 | check_value(a4[1], "bar", "11"); |
---|
134 | |
---|
135 | |
---|
136 | } |
---|
137 | |
---|
138 | void test_config_file() |
---|
139 | { |
---|
140 | options_description desc; |
---|
141 | desc.add_options() |
---|
142 | ("gv1", new untyped_value) |
---|
143 | ("gv2", new untyped_value) |
---|
144 | ("plug*", new untyped_value) |
---|
145 | ("m1.v1", new untyped_value) |
---|
146 | ("m1.v2", new untyped_value) |
---|
147 | ("b", bool_switch()) |
---|
148 | ; |
---|
149 | |
---|
150 | const char content1[] = |
---|
151 | " gv1 = 0#asd\n" |
---|
152 | "plug3 = 7\n" |
---|
153 | "b = true\n" |
---|
154 | "[m1]\n" |
---|
155 | "v1 = 1\n" |
---|
156 | "\n" |
---|
157 | "v2 = 2\n" |
---|
158 | ; |
---|
159 | |
---|
160 | stringstream ss(content1); |
---|
161 | vector<option> a1 = parse_config_file(ss, desc).options; |
---|
162 | BOOST_REQUIRE(a1.size() == 5); |
---|
163 | check_value(a1[0], "gv1", "0"); |
---|
164 | check_value(a1[1], "plug3", "7"); |
---|
165 | check_value(a1[2], "b", "true"); |
---|
166 | check_value(a1[3], "m1.v1", "1"); |
---|
167 | check_value(a1[4], "m1.v2", "2"); |
---|
168 | |
---|
169 | } |
---|
170 | |
---|
171 | void test_environment() |
---|
172 | { |
---|
173 | options_description desc; |
---|
174 | desc.add_options() |
---|
175 | ("foo", new untyped_value, "") |
---|
176 | ("bar", new untyped_value, "") |
---|
177 | ; |
---|
178 | |
---|
179 | #if defined(_WIN32) && ! defined(__BORLANDC__) |
---|
180 | _putenv("PO_TEST_FOO=1"); |
---|
181 | #else |
---|
182 | putenv("PO_TEST_FOO=1"); |
---|
183 | #endif |
---|
184 | parsed_options p = parse_environment(desc, "PO_TEST_"); |
---|
185 | |
---|
186 | BOOST_REQUIRE(p.options.size() == 1); |
---|
187 | BOOST_CHECK(p.options[0].string_key == "foo"); |
---|
188 | BOOST_REQUIRE(p.options[0].value.size() == 1); |
---|
189 | BOOST_CHECK(p.options[0].value[0] == "1"); |
---|
190 | |
---|
191 | //TODO: since 'bar' does not allow a value, it cannot appear in environemt, |
---|
192 | // which already has a value. |
---|
193 | } |
---|
194 | |
---|
195 | void test_unregistered() |
---|
196 | { |
---|
197 | options_description desc; |
---|
198 | |
---|
199 | char* cmdline1_[] = { "--foo=12", "--bar", "1"}; |
---|
200 | vector<string> cmdline1 = sv(cmdline1_, |
---|
201 | sizeof(cmdline1_)/sizeof(cmdline1_[0])); |
---|
202 | vector<option> a1 = |
---|
203 | command_line_parser(cmdline1).options(desc).allow_unregistered().run() |
---|
204 | .options; |
---|
205 | |
---|
206 | BOOST_REQUIRE(a1.size() == 3); |
---|
207 | BOOST_CHECK(a1[0].string_key == "foo"); |
---|
208 | BOOST_CHECK(a1[0].unregistered == true); |
---|
209 | BOOST_REQUIRE(a1[0].value.size() == 1); |
---|
210 | BOOST_CHECK(a1[0].value[0] == "12"); |
---|
211 | BOOST_CHECK(a1[1].string_key == "bar"); |
---|
212 | BOOST_CHECK(a1[1].unregistered == true); |
---|
213 | BOOST_CHECK(a1[2].string_key == ""); |
---|
214 | BOOST_CHECK(a1[2].unregistered == false); |
---|
215 | |
---|
216 | |
---|
217 | vector<string> a2 = collect_unrecognized(a1, include_positional); |
---|
218 | BOOST_CHECK(a2[0] == "--foo=12"); |
---|
219 | BOOST_CHECK(a2[1] == "--bar"); |
---|
220 | BOOST_CHECK(a2[2] == "1"); |
---|
221 | |
---|
222 | |
---|
223 | |
---|
224 | |
---|
225 | } |
---|
226 | |
---|
227 | int test_main(int, char* []) |
---|
228 | { |
---|
229 | test_command_line(); |
---|
230 | test_config_file(); |
---|
231 | test_environment(); |
---|
232 | test_unregistered(); |
---|
233 | return 0; |
---|
234 | } |
---|
235 | |
---|