我为kube-proxy.yaml
编写了以下自定义验证程序。
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;
}
程序根本没有使用自定义验证程序。我没有收到消息“调用自定义验证器”,如果我的验证器被使用,应该打印出来。相反,我得到了这个错误:
在抛出一个实例后终止调用 “增强:: exception_detail :: clone_impl
'what():选项'instruments.name'的参数('1 2.9')无效Aborted(core dumped)
我的配置文件如下:
[仪器]
prop = 1 2.9
关于如何从配置文件中解析多个参数的任何想法,而不是将它们写在如下的单独行中:
[仪器]
prop = 1
prop = 2.9
答案 0 :(得分:1)
您可以改为编写自定义转换:
<强> Live On Coliru 强>
#include <boost/program_options.hpp>
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/classification.hpp>
#include <fstream>
#include <iostream>
namespace po = boost::program_options;
typedef std::vector<double> coordinate;
int main() {
coordinate c;
// Setup options.
po::options_description desc("Options");
desc.add_options()
("instruments.prop", po::value<std::string>()->multitoken()->notifier([&c](std::string const& v) {
auto it = boost::make_split_iterator(v, boost::token_finder(boost::algorithm::is_any_of(" ,")));
std::transform(it, {}, back_inserter(c), [](auto& s) {
return boost::lexical_cast<double>(s);
});
}),
"plugin names" );
std::ifstream ifs("input.txt");
po::variables_map vm;
store(po::parse_config_file(ifs, desc, false), vm);
po::notify(vm);
std::copy(c.begin(), c.end(), std::ostream_iterator<double>(std::cout << "c: ", " "));
std::cout << "\n";
}
打印
c: 1 2.9