[Boost-bugs] [Boost C++ Libraries] #3265: parse vectors

Subject: [Boost-bugs] [Boost C++ Libraries] #3265: parse vectors
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2009-07-15 03:09:12


#3265: parse vectors
---------------------------------------------------------+------------------
 Reporter: Diederick C. Niehorster <dcnieho@…> | Owner: vladimir_prus
     Type: Feature Requests | Status: new
Milestone: Boost 1.40.0 | Component: program_options
  Version: Boost 1.39.0 | Severity: Optimization
 Keywords: |
---------------------------------------------------------+------------------
 As discussed on the boost users mailinglist, when a user specified a
 vector as return target in their options_description, that is:

 {{{("opt", po::value<float>&fA), "")}}}

 instead of

 {{{("vecopt", po::value<vector<float>>&vfa), "")}}}

 a simple syntax for specifying such a vector (that is, an option that can
 occur multiple times), on the command line, in a config file or even in an
 environment variable (sic!) can be used.

 Compare (for command line) [--test "(1 2 3)"] with [--test 1 --test 2
 --test 3].

 Adding support for this syntax in the validate() function for vectors
 ensures that the braces (feel free to change them to another symbol that
 makes better sense to you) will only be processed specially when the user
 specified a vector as output variable. It will not break any existing
 option input systems defined by users.

 Note that it will still be possible to specify options multiple times, the
 values of which will be accumulated in the vector. One could even supply
 multiple vector-syntax inputs for the same option, which would then get
 concatenated together.

 The code is not yet totally finished, I need to add support for handling
 vectors of strings, marked as TODO in the code.

 proposed diff:

 {{{
 Index: value_semantic.hpp
 ===================================================================
 --- value_semantic.hpp (revision 54915)
 +++ value_semantic.hpp (working copy)
 @@ -8,6 +8,8 @@

  #include <boost/throw_exception.hpp>

 +#include <boost/algorithm/string.hpp>
 +
  namespace boost { namespace program_options {

      extern BOOST_PROGRAM_OPTIONS_DECL std::string arg;
 @@ -124,7 +126,8 @@
  #endif

      /** Validates sequences. Allows multiple values per option occurrence
 - and multiple occurrences. */
 + and multiple occurrences. This function is only called when user
 + supplied vector<T> as datatype in option_description */
      template<class T, class charT>
      void validate(boost::any& v,
                    const std::vector<std::basic_string<charT> >& s,
 @@ -142,11 +145,37 @@
                  /* We call validate so that if user provided
                     a validator for class T, we use it even
                     when parsing vector<T>. */
 - boost::any a;
 - std::vector<std::basic_string<charT> > v;
 - v.push_back(s[i]);
 - validate(a, v, (T*)0, 0);
 - tv->push_back(boost::any_cast<T>(a));
 +
 + vector<std::basic_string<charT>> value;
 +
 + // test if vector notation is used in input
 + if (*s[i].begin() == '(' && *s[i].rbegin() == ')')
 + {
 + // test if it is a vector of strings
 + if (is_same<T, string>::value||is_same<T,
 wstring>::value)
 + {
 + /** TODO: needs special treatment, cant simply
 split
 + on space character
 + For now, proceed as before */
 + value.push_back(s[i]);
 + }
 + else
 + {
 + split( value, s[i].substr(1, s[i].size()-2),
 is_any_of(
 " ") );
 + }
 + }
 + else
 + value.push_back(s[i]);
 +
 + // validate option values
 + for (unsigned j = 0; j < value.size(); j++)
 + {
 + boost::any a;
 + std::vector<std::basic_string<charT> > v;
 + v.push_back(value[j]);
 + validate(a, v, (T*)0, 0);
 + tv->push_back(boost::any_cast<T>(a));
 + }
              }
              catch(const bad_lexical_cast& /*e*/) {
                  boost::throw_exception(invalid_option_value(s[i]));
 }}}

-- 
Ticket URL: <https://svn.boost.org/trac/boost/ticket/3265>
Boost C++ Libraries <http://www.boost.org/>
Boost provides free peer-reviewed portable C++ source libraries.

This archive was generated by hypermail 2.1.7 : 2017-02-16 18:50:00 UTC