Skip to content

Commit f4e7fb0

Browse files
author
Sascha Ochsenknecht
committed
config file parser now stores original_tokens, Fixes #2727
[SVN r58233]
1 parent f00e305 commit f4e7fb0

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-0
lines changed

src/config_file.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ namespace boost { namespace program_options { namespace detail {
111111
this->value().value.clear();
112112
this->value().value.push_back(value);
113113
this->value().unregistered = !registered;
114+
this->value().original_tokens.clear();
115+
this->value().original_tokens.push_back(name);
116+
this->value().original_tokens.push_back(value);
114117
break;
115118

116119
} else {

test/Jamfile.v2

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ test-suite program_options :
2929
[ po-test winmain.cpp ]
3030
[ po-test exception_test.cpp ]
3131
[ po-test split_test.cpp ]
32+
[ po-test unrecognized_test.cpp ]
3233
;
3334

3435
exe test_convert : test_convert.cpp ;

test/unrecognized_test.cpp

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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(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(opts, 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, true).options;
69+
vector< string > result = collect_unrecognized(opts, 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+
}

0 commit comments

Comments
 (0)