Skip to content

Commit 3c6401b

Browse files
committed
Prevent multitoken options from eating tokens after '--' separator.
Thanks to Gevorg Voskanyan for the bug report and the testcase. [SVN r52440]
1 parent ed4847e commit 3c6401b

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

src/cmdline.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <cassert>
2424
#include <cstring>
2525
#include <cctype>
26+
#include <climits>
2627

2728
#include <cstdio>
2829

@@ -288,6 +289,14 @@ namespace boost { namespace program_options { namespace detail {
288289
if (!opt2.string_key.empty())
289290
break;
290291

292+
if (opt2.position_key == INT_MAX)
293+
{
294+
// We use INT_MAX to mark positional options that
295+
// were found after the '--' terminator and therefore
296+
// should stay positional forever.
297+
break;
298+
}
299+
291300
assert(opt2.value.size() == 1);
292301

293302
opt.value.push_back(opt2.value[0]);
@@ -543,6 +552,8 @@ namespace boost { namespace program_options { namespace detail {
543552
{
544553
option opt;
545554
opt.value.push_back(args[i]);
555+
opt.original_tokens.push_back(args[i]);
556+
opt.position_key = INT_MAX;
546557
result.push_back(opt);
547558
}
548559
args.clear();

test/parsers_test.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,35 @@ void test_command_line()
163163
BOOST_CHECK_EQUAL(a5[1].value[1], "2");
164164
BOOST_CHECK_EQUAL(a5[1].value[2], "3");
165165
check_value(a5[2], "-x", "8");
166+
167+
168+
po::options_description desc4( "" );
169+
desc4.add_options()
170+
( "multitoken,m",
171+
po::value< std::vector< std::string > >()->multitoken(),
172+
"values"
173+
)
174+
( "file",
175+
po::value< std::string >(),
176+
"the file to process"
177+
)
178+
;
179+
180+
po::positional_options_description p;
181+
p.add( "file", 1 );
182+
183+
char* cmdline6[] = {"", "-m", "token1", "token2", "--", "some_file"};
184+
vector<option> a6 =
185+
command_line_parser(6, cmdline6).options(desc4).positional(p)
186+
.run().options;
187+
BOOST_CHECK_EQUAL(a6.size(), 2u);
188+
BOOST_REQUIRE(a6[0].value.size() == 2);
189+
BOOST_CHECK_EQUAL(a6[0].string_key, "multitoken");
190+
BOOST_CHECK_EQUAL(a6[0].value[0], "token1");
191+
BOOST_CHECK_EQUAL(a6[0].value[1], "token2");
192+
BOOST_CHECK_EQUAL(a6[1].string_key, "file");
193+
BOOST_REQUIRE(a6[1].value.size() == 1);
194+
BOOST_CHECK_EQUAL(a6[1].value[0], "some_file");
166195
}
167196

168197
void test_config_file()

0 commit comments

Comments
 (0)