对于第一部分,这应该有效
namespace po = boost::program_options;
po::option_descriptions desc("");
desc.add_options()
("opt", po::value<std::vector<int> >()->multitoken(), "description");
第二部分,需要更多的工作。该函数po::value
返回一个po::typed_value< T, charT >
,您必须在其上覆盖多个函数的行为,如下所示
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;
}
}
需要伴随的
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; }
然后
desc.add_options()
("opt", po::fixed_tokens_value<std::vector<int> >(2,2), "description");
应该管用。我还没有机会测试它,所以它可能包含一些错误。但是,至少,应该让你知道你需要什么。