处理好与Boost的program_options复杂的选项 [英] Handle complex options with Boost's program_options
问题描述
我有使用不同的多层次模型生成图表的程序。每个多级模型包括生成一个较小的种子图形(比方说,50个节点),其可从几种模式中创建的。(例如 - 对于每个可能的边缘,选择以概率p以包括它)
I have a program that generates graphs using different multi-level models. Each multi-level model consists of a generation of a smaller seed graph (say, 50 nodes) which can be created from several models (for example - for each possible edge, choose to include it with probability p).
种子图形生成后,将图形展开成一个较大的(例如1000个节点),利用另一组模型之一
After the seed graph generation, the graph is expanded into a larger one (say 1000 nodes), using one of another set of models.
在每两个阶段,每一个模型需要不同数目的参数。
In each of the two stages, each model require a different number of parameters.
我想是有program_options解析不同的可能的参数,根据该模型的名称。
I would like to be have program_options parse the different possible parameters, according to the names of the models.
举例来说,假设我有两个种子图形模型:SA,它具有1的参数,以及SB,它有两个。也为扩展的一部分,我有两种型号:A和B,再以1和2的参数,分别为。我希望能够做一些事情,如:
For example, say I have two seed graphs models: SA, which has 1 parameters, and SB, which has two. Also for the expansion part, I have two models: A and B, again with 1 and 2 parameters, respectively. I would like to be able do something like:
./graph_generator --seed=SA 0.1 --expansion=A 0.2
./graph_generator --seed=SB 0.1 3 --expansion=A 0.2
./graph_generator --seed=SA 0.1 --expansion=B 10 20
./graph_generator --seed=SB 0.1 3 --expansion=B 10 20
和正确分析的参数。是,即使可能吗?
and have the parameters parsed correctly. Is that even possible?
推荐答案
通过使用的自定义验证和<一个href=\"http://www.boost.org/doc/libs/release/doc/html/boost/program_options/typed_value.html#id941480-bb\">boost::program_options::value::multitoken,你可以达到预期的效果:
By using a custom validator and boost::program_options::value::multitoken, you can achieve the desired result:
#include <iostream>
#include <boost/lexical_cast.hpp>
#include <boost/optional.hpp>
#include <boost/program_options.hpp>
// Holds parameters for seed/expansion model
struct Model
{
std::string type;
boost::optional<float> param1;
boost::optional<float> param2;
};
// Called by program_options to parse a set of Model arguments
void validate(boost::any& v, const std::vector<std::string>& values,
Model*, int)
{
Model model;
// Extract tokens from values string vector and populate Model struct.
if (values.size() == 0)
{
throw boost::program_options::validation_error(
"Invalid model specification");
}
model.type = values.at(0); // Should validate for A/B
if (values.size() >= 2)
model.param1 = boost::lexical_cast<float>(values.at(1));
if (values.size() >= 3)
model.param2 = boost::lexical_cast<float>(values.at(2));
v = model;
}
int main(int argc, char* argv[])
{
Model seedModel, expansionModel;
namespace po = boost::program_options;
po::options_description options("Generic options");
options.add_options()
("seed",
po::value<Model>(&seedModel)->multitoken(),
"seed graph model")
("expansion",
po::value<Model>(&expansionModel)->multitoken(),
"expansion model")
;
po::variables_map vm;
po::store(po::parse_command_line(argc, argv, options), vm);
po::notify(vm);
std::cout << "Seed type: " << seedModel.type << "\n";
if (seedModel.param1)
std::cout << "Seed param1: " << *(seedModel.param1) << "\n";
if (seedModel.param2)
std::cout << "Seed param2: " << *(seedModel.param2) << "\n";
std::cout << "Expansion type: " << expansionModel.type << "\n";
if (expansionModel.param1)
std::cout << "Expansion param1: " << *(expansionModel.param1) << "\n";
if (expansionModel.param2)
std::cout << "Expansion param2: " << *(expansionModel.param2) << "\n";
return 0;
}
的验证
功能可能需要更多的严谨性,但你的想法。
The validate
function probably needs more rigor, but you get the idea.
这编译并为我工作。
这篇关于处理好与Boost的program_options复杂的选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!