Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r52440 - in branches/release/libs/program_options: src test
From: ghost_at_[hidden]
Date: 2009-04-17 05:45:01


Author: vladimir_prus
Date: 2009-04-17 05:45:00 EDT (Fri, 17 Apr 2009)
New Revision: 52440
URL: http://svn.boost.org/trac/boost/changeset/52440

Log:
Prevent multitoken options from eating tokens after '--' separator.

Thanks to Gevorg Voskanyan for the bug report and the testcase.

Text files modified:
   branches/release/libs/program_options/src/cmdline.cpp | 11 +++++++++++
   branches/release/libs/program_options/test/parsers_test.cpp | 29 +++++++++++++++++++++++++++++
   2 files changed, 40 insertions(+), 0 deletions(-)

Modified: branches/release/libs/program_options/src/cmdline.cpp
==============================================================================
--- branches/release/libs/program_options/src/cmdline.cpp (original)
+++ branches/release/libs/program_options/src/cmdline.cpp 2009-04-17 05:45:00 EDT (Fri, 17 Apr 2009)
@@ -23,6 +23,7 @@
 #include <cassert>
 #include <cstring>
 #include <cctype>
+#include <climits>
 
 #include <cstdio>
 
@@ -288,6 +289,14 @@
                     if (!opt2.string_key.empty())
                         break;
 
+ if (opt2.position_key == INT_MAX)
+ {
+ // We use INT_MAX to mark positional options that
+ // were found after the '--' terminator and therefore
+ // should stay positional forever.
+ break;
+ }
+
                     assert(opt2.value.size() == 1);
                     
                     opt.value.push_back(opt2.value[0]);
@@ -543,6 +552,8 @@
             {
                 option opt;
                 opt.value.push_back(args[i]);
+ opt.original_tokens.push_back(args[i]);
+ opt.position_key = INT_MAX;
                 result.push_back(opt);
             }
             args.clear();

Modified: branches/release/libs/program_options/test/parsers_test.cpp
==============================================================================
--- branches/release/libs/program_options/test/parsers_test.cpp (original)
+++ branches/release/libs/program_options/test/parsers_test.cpp 2009-04-17 05:45:00 EDT (Fri, 17 Apr 2009)
@@ -163,6 +163,35 @@
     BOOST_CHECK_EQUAL(a5[1].value[1], "2");
     BOOST_CHECK_EQUAL(a5[1].value[2], "3");
     check_value(a5[2], "-x", "8");
+
+
+ po::options_description desc4( "" );
+ desc4.add_options()
+ ( "multitoken,m",
+ po::value< std::vector< std::string > >()->multitoken(),
+ "values"
+ )
+ ( "file",
+ po::value< std::string >(),
+ "the file to process"
+ )
+ ;
+
+ po::positional_options_description p;
+ p.add( "file", 1 );
+
+ char* cmdline6[] = {"", "-m", "token1", "token2", "--", "some_file"};
+ vector<option> a6 =
+ command_line_parser(6, cmdline6).options(desc4).positional(p)
+ .run().options;
+ BOOST_CHECK_EQUAL(a6.size(), 2u);
+ BOOST_REQUIRE(a6[0].value.size() == 2);
+ BOOST_CHECK_EQUAL(a6[0].string_key, "multitoken");
+ BOOST_CHECK_EQUAL(a6[0].value[0], "token1");
+ BOOST_CHECK_EQUAL(a6[0].value[1], "token2");
+ BOOST_CHECK_EQUAL(a6[1].string_key, "file");
+ BOOST_REQUIRE(a6[1].value.size() == 1);
+ BOOST_CHECK_EQUAL(a6[1].value[0], "some_file");
 }
 
 void test_config_file()


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk