一个人如何提取使用Boost程序选项解析选项的顺序? [英] How does one extract the sequence of parsed options using Boost Program Options?

查看:195
本文介绍了一个人如何提取使用Boost程序选项解析选项的顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立使用Boost图和程序选项的图形发生器。有,例如,两种类型的组件C和瓦,每1源,1片和一些额外的参数之间以指定拓扑。我希望能够通过命令行参数的顺序规定的顺序组合起来的。

I'm building a graph generator using Boost Graph and Program Options. There are, for example, two types of components C and W, each with 1 source, 1 sink and some additional parameters to specify topology in between. I'd like to be able to stitch them together in the sequence provided by the order of the command line arguments.

例如:

./bin/make_graph -c4,5,1 -w3,3 -c3,1,2

应该创建一个类似图如下:

Should create a graph resembling the following:

C -- W -- C

不过:<​​/ P>

But:

./bin/make_graph -c4,5,1 -c3,1,2 -w3,3

应该创建一个类似图如下:

Should create a graph resembling the following:

C -- C -- W

使用boost :: program_options,我无法确定如何提取的确切顺序,因为它组成一样string_key选项与VALUE_TYPE地图==矢量&lt;字符串>(在我的情况)。

Using boost::program_options, I was unable to determine how to extract the exact order since it "composes" the options of the same string_key into a map with value_type == vector< string > (in my case).

通过重复在地图上,订单会丢失。有没有一种方法,以不重复解析,但有一个名为(可能是一个回调),每一个选项解析时间功能?我找不到在这个方向上的文档。任何其他建议?

By iterating over the map, the order is lost. Is there a way to not duplicate the parsing, but have a function called (perhaps a callback) every time an option is parsed? I couldn't find documentation in this direction. Any other suggestions?

要说服你,我不是做这件事,这是我到目前为止有:

To convince you that I'm not making this up, here's what I have so far:

    namespace bpo = boost::program_options;
    std::vector<std::string> args_cat, args_grid, args_web;
    bpo::options_description desc("Program options:");
    desc.add_options()
            .operator ()("help,h","Displays this help message.")
            .operator ()("caterpillar,c",bpo::value< std::vector<std::string> >(&args_cat)->default_value( std::vector<std::string>(1,"4,7,2"), "4,7,2" ),"Caterpillar tree with 3 parameters")
            .operator ()("grid,g",bpo::value< std::vector<std::string> >(&args_grid)->default_value( std::vector<std::string>(1,"3,4"), "3,4" ),"Rectangular grid with 2 parameters")
            .operator ()("web,w",bpo::value< std::vector<std::string> >(&args_web)->default_value( std::vector<std::string>(1,"3,4"), "3,4" ),"Web with 2 parameters")
            ;
    bpo::variables_map ops;
    bpo::store(bpo::parse_command_line(argc,argv,desc),ops);
    bpo::notify(ops);
    if((argc < 2) || (ops.count("help"))) {
        std::cout << desc << std::endl;
        return;
    }
    //TODO: remove the following scope block after testing
    {
        typedef bpo::variables_map::iterator OptionsIterator;
        OptionsIterator it = ops.options.begin(), it_end = ops.options.end();
        while(it != it_end) {
            std::cout << it->first << ": ";
            BOOST_FOREACH(std::string value, it->second) {
                std::cout << value << " ";
            }
            std::cout << std::endl;
            ++it;
        }
        return;
    }

我意识到,我可能还包括类型作为参数,平凡解决这个问题,例如:

I realize that I could also include the type as a parameter and solve this problem trivially, e.g.:

./bin/make_graph --component c,4,5,1 --component w,3,3 --component c,3,1,2

但要搬进来编写解析器/验证自己的方向(即使不使用Boost程序选项也许):

but that's moving in the direction of writing a parser/validator myself (maybe even without using Boost Program Options):

./bin/make_graph --custom c,4,5,1,w,3,3,c,3,1,2
./bin/make_graph c,4,5,1,w,3,3,c,3,1,2

你们会如何建议我在一个优雅的方式做到这一点?

How would you guys recommend I do this in an elegant way?

在此先感谢!

PS:我已经搜查所以[助推] +顺序程序选项张贴在此之前和[升压程序选项] +订单(及其变体),所以我提前道歉,如果这轮出是重复的。

PS: I've searched on SO for "[boost] +sequence program options" and "[boost-program-options] +order" (and their variants) before posting this, so I apologize in advance if this turns out to be a duplicate.

推荐答案

由于张贴的问题,我做了一些挖掘,有一个黑客,与现有的例子我上面有工作的。

Since posting the question, I did some digging and have a "hack" that works with the existing examples I had above.

bpo::parsed_options p_ops = bpo::parse_command_line(argc,argv,desc);
typedef std::vector< bpo::basic_option<char> >::iterator OptionsIterator;
OptionsIterator it = p_ops.options.begin(), it_end = p_ops.options.end();
while(it != it_end) {
    std::cout << it->string_key << ": ";
    BOOST_FOREACH(std::string value, it->value) {
        std::cout << value << " ";
    }
    std::cout << std::endl;
    ++it;
}

我把它叫做一个黑客的原因是因为它访问的所有参数的字符串,一会从中提取的类型很像BPO :: variables_map确实与。至于&LT; T&GT;( )成员函数。编辑:这也访问选项成员结构直接

The reason I call it a hack is because it accesses all arguments as strings, and one would have to extract the types from it much like bpo::variables_map does with the .as<T>() member function. It also accesses a member of the options struct directly.

这篇关于一个人如何提取使用Boost程序选项解析选项的顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆