How does one extract the sequence of parsed options using Boost Program Options?

纵然是瞬间 提交于 2019-12-05 10:42:30

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;
}

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. EDIT: It also accesses a member of the options struct directly.

How about this:

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

Where "c,4,5,1", "c,3,1,2" and "w,3,3" are positional arguments which are stored (in order) in a std::vector<std::string> (just like --input-file in this tutorial) . Then use Boost.Tokenizer or boost::algorithm::split to extract the subtokens from each argument string.

If the graphs can be complex, you should consider making it possible for the user to specify an input file that contains the graph parameters. Boost.Program_Options can parse a user config file that uses the same syntax as the command line options.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!