|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r58133 - in trunk: boost/program_options libs/program_options/src libs/program_options/test
From: s.ochsenknecht_at_[hidden]
Date: 2009-12-04 03:09:44
Author: s_ochsenknecht
Date: 2009-12-04 03:09:43 EST (Fri, 04 Dec 2009)
New Revision: 58133
URL: http://svn.boost.org/trac/boost/changeset/58133
Log:
enhance split_unix() to allow unix style splitting of command line string
Text files modified:
trunk/boost/program_options/parsers.hpp | 10 +++-
trunk/libs/program_options/src/split.cpp | 65 ++++++++++++++++------------------
trunk/libs/program_options/test/split_test.cpp | 74 +++++++++++++++++++++++++++++++++++++--
3 files changed, 107 insertions(+), 42 deletions(-)
Modified: trunk/boost/program_options/parsers.hpp
==============================================================================
--- trunk/boost/program_options/parsers.hpp (original)
+++ trunk/boost/program_options/parsers.hpp 2009-12-04 03:09:43 EST (Fri, 04 Dec 2009)
@@ -206,14 +206,18 @@
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 " ".
+ Splitting is done in a unix style way, with respect to quotes '"'
+ and escape characters '\'
*/
BOOST_PROGRAM_OPTIONS_DECL std::vector<std::string>
- split(const std::string& cmdline, const std::string& sep = " ");
-
+ split_unix(const std::string& cmdline, const std::string& seperator = " ",
+ const std::string& quote = "\"", const std::string& escape = "\\");
+
#ifndef BOOST_NO_STD_WSTRING
/** @overload */
BOOST_PROGRAM_OPTIONS_DECL std::vector<std::wstring>
- split(const std::wstring& cmdline, const std::wstring& sep = L" ");
+ split_unix(const std::wstring& cmdline, const std::wstring& seperator = L" ",
+ const std::wstring& quote = L"\"", const std::wstring& escape = L"\\");
#endif
#ifdef _WIN32
Modified: trunk/libs/program_options/src/split.cpp
==============================================================================
--- trunk/libs/program_options/src/split.cpp (original)
+++ trunk/libs/program_options/src/split.cpp 2009-12-04 03:09:43 EST (Fri, 04 Dec 2009)
@@ -4,62 +4,59 @@
// or copy at http://www.boost.org/LICENSE_1_0.txt)
#define BOOST_PROGRAM_OPTIONS_SOURCE
+
#include <boost/program_options/parsers.hpp>
+#include <boost/tokenizer.hpp>
+
#include <string>
#include <vector>
namespace boost { namespace program_options { namespace detail {
-
- template<class charT>
+ 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);
- }
- }
+ split_unix(
+ const std::basic_string<charT>& cmdline,
+ const std::basic_string<charT>& seperator,
+ const std::basic_string<charT>& quote,
+ const std::basic_string<charT>& escape)
+ {
+ typedef boost::tokenizer< boost::escaped_list_separator<charT>,
+ typename std::basic_string<charT>::const_iterator,
+ std::basic_string<charT> > tokenizerT;
+
+ tokenizerT tok(cmdline.begin(), cmdline.end(),
+ boost::escaped_list_separator< charT >(escape, seperator, quote));
+
+ std::vector< std::basic_string<charT> > result;
+ for (typename tokenizerT::iterator cur_token(tok.begin()), end_token(tok.end()); cur_token != end_token; ++cur_token) {
+ if (!cur_token->empty())
+ result.push_back(*cur_token);
}
- return result;
+ return result;
}
-}}}
+}}} // namespace
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)
+ split_unix(const std::string& cmdline, const std::string& seperator,
+ const std::string& quote, const std::string& escape)
{
- return detail::split(cmdline, sep);
+ return detail::split_unix< char >(cmdline, seperator, quote, escape);
}
#ifndef BOOST_NO_STD_WSTRING
BOOST_PROGRAM_OPTIONS_DECL std::vector<std::wstring>
- split(const std::wstring& cmdline, const std::wstring& sep)
+ split_unix(const std::wstring& cmdline, const std::wstring& seperator,
+ const std::wstring& quote, const std::wstring& escape)
{
- return detail::split(cmdline, sep);
+ return detail::split_unix< wchar_t >(cmdline, seperator, quote, escape);
}
#endif
-}}
+}} // namespace
+
Modified: trunk/libs/program_options/test/split_test.cpp
==============================================================================
--- trunk/libs/program_options/test/split_test.cpp (original)
+++ trunk/libs/program_options/test/split_test.cpp 2009-12-04 03:09:43 EST (Fri, 04 Dec 2009)
@@ -26,11 +26,12 @@
void split_whitespace(const options_description& description)
{
- const char* cmdline = "prg --input input.txt \t --optimization 4 \t\n --opt option";
+ const char* cmdline = "prg --input input.txt \r --optimization 4 \t --opt \n option";
- vector< string > tokens = split(cmdline, " \t\n");
+ vector< string > tokens = split_unix(cmdline, " \t\n\r");
BOOST_REQUIRE(tokens.size() == 7);
+
check_value(tokens[0], "prg");
check_value(tokens[1], "--input");
check_value(tokens[2], "input.txt");
@@ -49,8 +50,8 @@
const char* cmdline = "prg --input=input.txt --optimization=4 --opt=option";
- vector< string > tokens = split(cmdline, "= ");
-
+ vector< string > tokens = split_unix(cmdline, "= ");
+
BOOST_REQUIRE(tokens.size() == 7);
check_value(tokens[0], "prg");
check_value(tokens[1], "--input");
@@ -70,7 +71,7 @@
const char* cmdline = "prg;--input input.txt;--optimization 4;--opt option";
- vector< string > tokens = split(cmdline, "; ");
+ vector< string > tokens = split_unix(cmdline, "; ");
BOOST_REQUIRE(tokens.size() == 7);
check_value(tokens[0], "prg");
@@ -86,6 +87,66 @@
notify(vm);
}
+void split_quotes(const options_description& description)
+{
+ const char* cmdline = "prg --input \"input.txt input.txt\" --optimization 4 --opt \"option1 option2\"";
+
+ vector< string > tokens = split_unix(cmdline, " ");
+
+ BOOST_REQUIRE(tokens.size() == 7);
+ check_value(tokens[0], "prg");
+ check_value(tokens[1], "--input");
+ check_value(tokens[2], "input.txt input.txt");
+ check_value(tokens[3], "--optimization");
+ check_value(tokens[4], "4");
+ check_value(tokens[5], "--opt");
+ check_value(tokens[6], "option1 option2");
+
+ variables_map vm;
+ store(command_line_parser(tokens).options(description).run(), vm);
+ notify(vm);
+}
+
+void split_escape(const options_description& description)
+{
+ const char* cmdline = "prg --input \\\"input.txt\\\" --optimization 4 --opt \\\"option1\\ option2\\\"";
+
+ vector< string > tokens = split_unix(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], "\"option1 option2\"");
+
+ variables_map vm;
+ store(command_line_parser(tokens).options(description).run(), vm);
+ notify(vm);
+}
+
+
+void split_single_quote(const options_description& description)
+{
+ const char* cmdline = "prg --input 'input.txt input.txt' --optimization 4 --opt 'option1 option2'";
+
+ vector< string > tokens = split_unix(cmdline, " ", "'");
+
+ BOOST_REQUIRE(tokens.size() == 7);
+ check_value(tokens[0], "prg");
+ check_value(tokens[1], "--input");
+ check_value(tokens[2], "input.txt input.txt");
+ check_value(tokens[3], "--optimization");
+ check_value(tokens[4], "4");
+ check_value(tokens[5], "--opt");
+ check_value(tokens[6], "option1 option2");
+
+ variables_map vm;
+ store(command_line_parser(tokens).options(description).run(), vm);
+ notify(vm);
+}
int main(int /*ac*/, char** /*av*/)
{
@@ -99,6 +160,9 @@
split_whitespace(desc);
split_equalsign(desc);
split_semi(desc);
+ split_quotes(desc);
+ split_escape(desc);
+ split_single_quote(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