[program_options] comma separated options

Hi,
Someone asked me if it was possible to "easily" specify an option that
would allow to specify a vector of values.
The first options that came in mind was to use "multitoken". But that
solution had to drawback in that situation:
- ambiguous with respect to positional parameters
- clumsy when you want to allow options to be specified in files, since
you have to write:
option 12
option 42
...
instead of:
option 12 42 19
So I though about using a custom validator, which implies introducing a
new type too, if I understand correctly.
Something along the lines of:
=======================
...
template<typename T>
struct option_sequence {
std::vector<T> values;
};
template<typename T>
void validate(boost::any& v,
const std::vectorstd::string& values,
option_sequence<T>* target_type, int)
{
namespace po = boost::program_options;
namespace alg = boost::algorithm;
std::vector<T> result;
typedef std::vectorstd::string strings;
for ( strings::const_iterator iter = values.begin(); iter !=
values.end();
++iter ) {
strings tks;
alg::split(tks, *iter, alg::is_any_of(":"));
for( strings::const_iterator tk = tks.begin(); tk != tks.end();
++tk) {
result.push_back(boost::lexical_cast<T>(*tk));
}
}
v = option_sequence<T>();
boost::any_cast
participants (1)
-
Alain Miniussi