id summary reporter owner description type status milestone component version severity resolution keywords cc 3265 parse vectors Diederick C. Niehorster Vladimir Prus "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&fA), """")}}} instead of {{{(""vecopt"", po::value>&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 +#include + 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 as datatype in option_description */ template void validate(boost::any& v, const std::vector >& 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. */ - boost::any a; - std::vector > v; - v.push_back(s[i]); - validate(a, v, (T*)0, 0); - tv->push_back(boost::any_cast(a)); + + vector> 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::value||is_same::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 > v; + v.push_back(value[j]); + validate(a, v, (T*)0, 0); + tv->push_back(boost::any_cast(a)); + } } catch(const bad_lexical_cast& /*e*/) { boost::throw_exception(invalid_option_value(s[i])); }}}" Feature Requests new Boost 1.40.0 program_options Boost 1.39.0 Optimization dcnieho@…