|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r57971 - in trunk: boost/program_options libs/program_options/build libs/program_options/src libs/program_options/test
From: s.ochsenknecht_at_[hidden]
Date: 2009-11-27 12:47:53
Author: s_ochsenknecht
Date: 2009-11-27 12:47:51 EST (Fri, 27 Nov 2009)
New Revision: 57971
URL: http://svn.boost.org/trac/boost/changeset/57971
Log:
add general split function, Fixes #2561
Added:
trunk/libs/program_options/src/split.cpp (contents, props changed)
trunk/libs/program_options/test/split_test.cpp (contents, props changed)
Text files modified:
trunk/boost/program_options/parsers.hpp | 14 ++++++++++++++
trunk/libs/program_options/build/Jamfile.v2 | 4 ++--
trunk/libs/program_options/src/options_description.cpp | 2 +-
trunk/libs/program_options/test/Jamfile.v2 | 1 +
4 files changed, 18 insertions(+), 3 deletions(-)
Modified: trunk/boost/program_options/parsers.hpp
==============================================================================
--- trunk/boost/program_options/parsers.hpp (original)
+++ trunk/boost/program_options/parsers.hpp 2009-11-27 12:47:51 EST (Fri, 27 Nov 2009)
@@ -202,6 +202,20 @@
BOOST_PROGRAM_OPTIONS_DECL parsed_options
parse_environment(const options_description&, const char* prefix);
+ /** Splits a given string to a collection of single strings which
+ can be passed to command_line_parser. The second parameter is
+ used to specify a collection of possible seperator chars used
+ for splitting. The seperator is defaulted to space " ".
+ */
+ BOOST_PROGRAM_OPTIONS_DECL std::vector<std::string>
+ split(const std::string& cmdline, const std::string& sep = " ");
+
+#ifndef BOOST_NO_STD_WSTRING
+ /** @overload */
+ BOOST_PROGRAM_OPTIONS_DECL std::vector<std::wstring>
+ split(const std::wstring& cmdline, const std::wstring& sep = L" ");
+#endif
+
#ifdef _WIN32
/** Parses the char* string which is passed to WinMain function on
windows. This function is provided for convenience, and because it's
Modified: trunk/libs/program_options/build/Jamfile.v2
==============================================================================
--- trunk/libs/program_options/build/Jamfile.v2 (original)
+++ trunk/libs/program_options/build/Jamfile.v2 2009-11-27 12:47:51 EST (Fri, 27 Nov 2009)
@@ -6,7 +6,7 @@
SOURCES =
cmdline config_file options_description parsers variables_map
value_semantic positional_options utf8_codecvt_facet
- convert winmain
+ convert winmain split
;
lib boost_program_options
@@ -16,4 +16,4 @@
: <link>shared:<define>BOOST_PROGRAM_OPTIONS_DYN_LINK=1
;
-boost-install boost_program_options ;
\ No newline at end of file
+boost-install boost_program_options ;
Modified: trunk/libs/program_options/src/options_description.cpp
==============================================================================
--- trunk/libs/program_options/src/options_description.cpp (original)
+++ trunk/libs/program_options/src/options_description.cpp 2009-11-27 12:47:51 EST (Fri, 27 Nov 2009)
@@ -8,7 +8,7 @@
#define BOOST_PROGRAM_OPTIONS_SOURCE
#include <boost/program_options/config.hpp>
#include <boost/program_options/options_description.hpp>
-// FIXME: this is only to get multiple_occureces class
+// FIXME: this is only to get multiple_occurences class
// should move that to a separate headers.
#include <boost/program_options/parsers.hpp>
Added: trunk/libs/program_options/src/split.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/program_options/src/split.cpp 2009-11-27 12:47:51 EST (Fri, 27 Nov 2009)
@@ -0,0 +1,65 @@
+// Copyright Sascha Ochsenknecht 2009.
+// Distributed under the Boost Software License, Version 1.0.
+// (See accompanying file LICENSE_1_0.txt
+// or copy at http://www.boost.org/LICENSE_1_0.txt)
+
+#define BOOST_PROGRAM_OPTIONS_SOURCE
+#include <boost/program_options/parsers.hpp>
+#include <string>
+#include <vector>
+
+namespace boost { namespace program_options { namespace detail {
+
+
+ template<class charT>
+ std::vector<std::basic_string<charT> >
+ split(const std::basic_string<charT>& cmdline, const std::basic_string<charT>& sep)
+ {
+ std::vector<std::basic_string<charT> > result;
+ if (!cmdline.empty())
+ {
+ std::basic_string<charT> sub(cmdline), val;
+ std::size_t pos;
+
+ while (sub.size() > 0)
+ {
+ if ((pos = sub.find_first_of(sep)) != sub.npos)
+ {
+ val = sub.substr(0,pos);
+ sub = sub.substr(pos+1);
+ }
+ else
+ {
+ val = sub;
+ sub.erase();
+ }
+ if (!val.empty())
+ {
+ result.push_back(val);
+ }
+ }
+ }
+ return result;
+ }
+
+}}}
+
+namespace boost { namespace program_options {
+
+ // Take a command line string and splits in into tokens, according
+ // to the given collection of seperators chars.
+ BOOST_PROGRAM_OPTIONS_DECL std::vector<std::string>
+ split(const std::string& cmdline, const std::string& sep)
+ {
+ return detail::split(cmdline, sep);
+ }
+
+#ifndef BOOST_NO_STD_WSTRING
+ BOOST_PROGRAM_OPTIONS_DECL std::vector<std::wstring>
+ split(const std::wstring& cmdline, const std::wstring& sep)
+ {
+ return detail::split(cmdline, sep);
+ }
+#endif
+
+}}
Modified: trunk/libs/program_options/test/Jamfile.v2
==============================================================================
--- trunk/libs/program_options/test/Jamfile.v2 (original)
+++ trunk/libs/program_options/test/Jamfile.v2 2009-11-27 12:47:51 EST (Fri, 27 Nov 2009)
@@ -28,6 +28,7 @@
[ po-test unicode_test.cpp ]
[ po-test winmain.cpp ]
[ po-test exception_test.cpp ]
+ [ po-test split_test.cpp ]
;
exe test_convert : test_convert.cpp ;
Added: trunk/libs/program_options/test/split_test.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/program_options/test/split_test.cpp 2009-11-27 12:47:51 EST (Fri, 27 Nov 2009)
@@ -0,0 +1,104 @@
+// Copyright Sascha Ochsenknecht 2009.
+// Distributed under the Boost Software License, Version 1.0.
+// (See accompanying file LICENSE_1_0.txt
+// or copy at http://www.boost.org/LICENSE_1_0.txt)
+
+
+#include <boost/program_options/parsers.hpp>
+#include <boost/program_options/options_description.hpp>
+#include <boost/program_options/variables_map.hpp>
+#include <boost/program_options/cmdline.hpp>
+using namespace boost::program_options;
+
+#include <iostream>
+#include <sstream>
+#include <vector>
+#include <cassert>
+using namespace std;
+
+#include "minitest.hpp"
+
+void check_value(const string& option, const string& value)
+{
+ BOOST_CHECK(option == value);
+}
+
+void split_whitespace(const options_description& description)
+{
+
+ const char* cmdline = "prg --input input.txt \t --optimization 4 \t\n --opt option";
+
+ vector< string > tokens = split(cmdline, " \t\n");
+
+ BOOST_REQUIRE(tokens.size() == 7);
+ check_value(tokens[0], "prg");
+ check_value(tokens[1], "--input");
+ check_value(tokens[2], "input.txt");
+ check_value(tokens[3], "--optimization");
+ check_value(tokens[4], "4");
+ check_value(tokens[5], "--opt");
+ check_value(tokens[6], "option");
+
+ variables_map vm;
+ store(command_line_parser(tokens).options(description).run(), vm);
+ notify(vm);
+}
+
+void split_equalsign(const options_description& description)
+{
+
+ const char* cmdline = "prg --input=input.txt --optimization=4 --opt=option";
+
+ vector< string > tokens = split(cmdline, "= ");
+
+ BOOST_REQUIRE(tokens.size() == 7);
+ check_value(tokens[0], "prg");
+ check_value(tokens[1], "--input");
+ check_value(tokens[2], "input.txt");
+ check_value(tokens[3], "--optimization");
+ check_value(tokens[4], "4");
+ check_value(tokens[5], "--opt");
+ check_value(tokens[6], "option");
+
+ variables_map vm;
+ store(command_line_parser(tokens).options(description).run(), vm);
+ notify(vm);
+}
+
+void split_semi(const options_description& description)
+{
+
+ const char* cmdline = "prg;--input input.txt;--optimization 4;--opt option";
+
+ vector< string > tokens = split(cmdline, "; ");
+
+ BOOST_REQUIRE(tokens.size() == 7);
+ check_value(tokens[0], "prg");
+ check_value(tokens[1], "--input");
+ check_value(tokens[2], "input.txt");
+ check_value(tokens[3], "--optimization");
+ check_value(tokens[4], "4");
+ check_value(tokens[5], "--opt");
+ check_value(tokens[6], "option");
+
+ variables_map vm;
+ store(command_line_parser(tokens).options(description).run(), vm);
+ notify(vm);
+}
+
+
+int main(int /*ac*/, char** /*av*/)
+{
+ options_description desc;
+ desc.add_options()
+ ("input,i", value<string>(), "the input file")
+ ("optimization,O", value<unsigned>(), "optimization level")
+ ("opt,o", value<string>(), "misc option")
+ ;
+
+ split_whitespace(desc);
+ split_equalsign(desc);
+ split_semi(desc);
+
+ return 0;
+}
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