如何使用Boost Program Options提取已解析选项的序列?

scr*_*ink 5 c++ boost boost-program-options

我正在使用Boost Graph和Program Options构建图形生成器.例如,有两种类型的组件C和W,每种组件具有1个源,1个接收器和一些附加参数以指定其间的拓扑.我希望能够按照命令行参数的顺序将它们拼接在一起.

例如:

./bin/make_graph -c4,5,1 -w3,3 -c3,1,2
Run Code Online (Sandbox Code Playgroud)

应创建类似于以下内容的图形:

C -- W -- C
Run Code Online (Sandbox Code Playgroud)

但:

./bin/make_graph -c4,5,1 -c3,1,2 -w3,3
Run Code Online (Sandbox Code Playgroud)

应创建类似于以下内容的图形:

C -- C -- W
Run Code Online (Sandbox Code Playgroud)

使用boost :: program_options,我无法确定如何提取确切的顺序,因为它使用value_type == vector <string>(在我的情况下)将相同string_key的选项"组合"到一个映射中.

通过迭代地图,订单就会丢失.有没有办法不重复解析,但每次解析一个选项时都有一个叫做(可能是回调)的函数?我找不到这方面的文件.还有其他建议吗?

为了说服你,我没有说明这一点,这就是我到目前为止所做的:

    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;
    }
Run Code Online (Sandbox Code Playgroud)

我意识到我还可以将类型作为参数包含在内并轻松解决这个问题,例如:

./bin/make_graph --component c,4,5,1 --component w,3,3 --component c,3,1,2
Run Code Online (Sandbox Code Playgroud)

但是我正朝着自己编写解析器/验证器的方向发展(甚至可能不使用Boost程序选项):

./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
Run Code Online (Sandbox Code Playgroud)

你们怎么建议我以优雅的方式做到这一点?

提前致谢!

PS:我在发布之前在SO上搜索了"[boost] +顺序程序选项"和"[boost-program-options] + order"(以及它们的变体),所以如果结果是这样,我会事先道歉重复.

Emi*_*ier 2

这个怎么样:

./bin/make_graph c,4,5,1 c,3,1,2 w,3,3
Run Code Online (Sandbox Code Playgroud)

其中"c,4,5,1""c,3,1,2""w,3,3"位置参数,它们(按顺序)存储在 a 中(就像本教程std::vector<std::string>中一样)。然后使用Boost.Tokenizer或从每个参数字符串中提取子标记。--input-fileboost::algorithm::split

如果图形可能很复杂,您应该考虑让用户可以指定包含图形参数的输入文件。Boost.Program_Options 可以解析使用与命令行选项相同语法的用户配置文件。