【发布时间】:2017-04-20 06:50:06
【问题描述】:
我为std::vector<double> 编写了以下自定义验证器。
typedef vector<double> coordinate;
void validate(boost::any& v,
const vector<string>& values,
coordinate*, int) {
std::cout << "Custom validator called\n";
coordinate c;
vector<double> dvalues;
for(vector<string>::const_iterator it = values.begin();
it != values.end();
++it) {
stringstream ss(*it);
copy(istream_iterator<double>(ss), istream_iterator<double>(),
back_inserter(dvalues));
if(!ss.eof()) {
std::cerr << "SS EOF\n";
throw po::invalid_option_value("Invalid coordinate specification sseof");
}
}
if(dvalues.size() != 2) {
std::cerr << "dvalues size\n";
throw po::invalid_option_value("Invalid coordinate specification dvalues size");
}
c.push_back(dvalues[0]);
c.push_back(dvalues[1]);
v = c;
}
我以以下方式添加选项:
coordinate c;
// Setup options.
po::options_description desc("Options");
desc.add_options()
("instruments.prop", po::value<coordinate>( &c )->multitoken(),
"plugin names" );
程序根本没有使用自定义验证器。如果正在使用我的验证器,我没有收到应该打印的消息“调用自定义验证器”。相反,我得到了这个错误:
在抛出一个实例后调用终止 'boost::exception_detail::clone_impl
'what(): 选项'instruments.name' 的参数('1 2.9')无效 Aborted (core dumped)
我的配置文件如下所示:
[仪器]
道具= 1 2.9
关于如何解析配置文件中的多个参数而不将它们写在单独的行中的任何想法,如下所示:
[仪器]
道具 = 1
道具 = 2.9
【问题讨论】:
标签: c++ validation c++11 boost