Vector arguments in Boost Program Options
This is a late answer but I hope it helps someone. You could easily use the same technique in item #1, except you need to add another validation on the number of items in your vector:
from rcollyer's example:
namespace po = boost::program_options;
po::option_descriptions desc("");
desc.add_options()
("opt", po::value<std::vector<int> >()->multitoken(), "description");
po::variables_map vm;
po::store(po::parse_command_line(argc, argv, desc), vm);
po::notify(vm);
vector<int> opts;
if (!vm["opt"].empty() && (opts = vm["opt"].as<vector<int> >()).size() == 2) {
// good to go
}
For the first part, this should work
namespace po = boost::program_options;
po::option_descriptions desc("");
desc.add_options()
("opt", po::value<std::vector<int> >()->multitoken(), "description");
The second part, requires a bit more work. The function po::value
returns a po::typed_value< T, charT >
on which you'll have to override the behavior of several functions, as follows
template< typename T, typename charT = char >
class fixed_tokens_typed_value : public po::typed_value< T, charT > {
unsigned _min, _max;
typedef po::typed_value< T, charT > base;
public:
fixed_tokens_typed_value( T * t, unsigned min, unsigned max )
: _min(min), _max(max), base( t ) {
base::multitoken();
}
virtual multi_typed_value* min_tokens( unsigned min ) {
_min = min;
return *this;
}
unsigned min_tokens() const {return _min;}
virtual multi_typed_value* max_tokens( unsigned max ) {
_max = max;
return *this;
}
unsigned max_tokens() const {return _max;}
base* zero_tokens() {
_min = _max = 0;
base::zero_tokens();
return *this;
}
}
which needs to be accompanied by
template< typename T >
fixed_tokens_typed_value< T >
fixed_tokens_value(unsigned min, unsigned max) {
return fixed_tokens_typed_value< T >(0, min, max ); }
template< typename T >
fixed_tokens_typed_value< T >
fixed_tokens_value(T * t, unsigned min, unsigned max) {
fixed_tokens_typed_value< T >* r = new
fixed_tokens_typed_value< T >(t, min, max);
return r; }
Then
desc.add_options()
("opt", po::fixed_tokens_value<std::vector<int> >(2,2), "description");
should work. I have not yet had a chance to test it, so it likely contains a few bugs. But, at a minimum, should give you an idea of what you need.