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

Subject: Re: [Boost-bugs] [Boost C++ Libraries] #3265: parse vectors
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2012-12-27 10:21:09


#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
Resolution: | Keywords:
----------------------------------------------------------+-----------------

Comment (by Diederick C. Niehorster <dcnieho@…>):

 Hmm, here is a slightly updated version of the patch. Some usage of std
 wasn't prefixed with std:: and the call to split should split on any
 whitespace (tabs are fine too after all) and have token compressing on in
 case e.g. multiple spaces delimit two entries.

 {{{
 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,41 @@
                  /* 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));
 +
 + std::vector<std::basic_string<charT>> value;
 +
 + // test if vector notation is used in input
 + if (*s[i].begin() == '(' && *s[i].rbegin() == ')')
 + {
 + // test if a vector of strings is requested
 + if (is_same<T, std::string>::value||is_same<T,
 std::wstring>::value)
 + {
 + /** Strings needs special treatment, cant simply
 split
 + on space character as that might be part of
 the
 + string. Using split_winmain we can allow for
 the
 + same grammar within the brackets () as for
 strings
 + on the command line, that is, e.g., use "" to
 + encapsulate strings with spaces. For complete
 + grammar, see links in the split_winmain
 source. */
 + value = split_winmain(s[i].substr(1,
 s[i].size()-2));
 + }
 + else
 + {
 + split( value, s[i].substr(1, s[i].size()-2),
 is_space(), token_compress_on );
 + }
 + }
 + 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]));}}}
 }}}

 Best and thanks,
 Dee

-- 
Ticket URL: <https://svn.boost.org/trac/boost/ticket/3265#comment:4>
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:11 UTC