C ++:升压program_options:参数多个列表 [英] C++: Boost program_options: Multiple lists of arguments

查看:214
本文介绍了C ++:升压program_options:参数多个列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在与工作的boost :: program_options 。我的节目应该采取作为参数(除其他事项...)任意长度的名单的任意数量。例如,用户应当能够调用

I'm currently working with boost::program_options. My program is supposed to take as arguments (amongst other things...) an arbitrary number of 'lists' of arbitrary length. For example, the user should be able to call

./myprogram -list item1 item2 item3 -list item1 item2 -list item1 item2

显然,我不想得到一个列表/向量的所有项一个接一个地作为结果,但(在这种情况下)三个列表/载体(或,例如,将含有载体的一种载体元素),每个列表两个或三个项目(每个项目应该是一个字符串,但我想这并不重要)。
正如我之前所说的,列表的数量(以及每个列表项的!数)应该是任意的。
我怎样才能做到这一点与的boost :: program_options

推荐答案

这可以在不一大堆额外的code完成。秘密是分开存储步骤解析步骤中,也做了这样的回答

This can be done without a whole lot of extra code. The secret is to separate the parsing step from the storage step, as also done in this answer.

解析器将返回键/值结构的容器作为选项是从用户psented $ P $。如果一个选项多次提交则容器将每个选项提交一个单独的条目。这是很简单的扫描特定的选项,但是我们要组织其值。

The parser will return a container of key/value structs as the options are presented from the user. If an option is submitted multiple times then the container will have a separate entry for each option submission. It is quite straightforward to scan for a particular option and organize its values however we want.

下面是打印出一个单独的行每​​个输入多令牌选项的例子:

Here's an example that prints out each input multi-token option on a separate line:

#include <iostream>
#include <string>
#include <vector>

#include <boost/program_options.hpp>

namespace po = boost::program_options;

int main(int argc, char *argv[]) {
   // Define a multi-token option.
   po::options_description desc("Allowed options");
   desc.add_options()
      ("list", po::value<std::vector<std::string>>()->multitoken(), "multiple values");

   // Just parse the options without storing them in a map.
   po::parsed_options parsed_options = po::command_line_parser(argc, argv)
      .options(desc)
      .run();

   // Build list of multi-valued option instances. We iterate through
   // each command-line option, whether it is repeated or not. We
   // accumulate the values for our multi-valued option in a
   // container.
   std::vector<std::vector<std::string>> lists;
   for (const po::option& o : parsed_options.options) {
      if (o.string_key == "list")
         lists.push_back(o.value);
   }

   // If we had other normal options, we would store them in a map
   // here. In this demo program it isn't really necessary because
   // we are only interested in our special multi-valued option.
   po::variables_map vm;
   po::store(parsed_options, vm);

   // Print out the multi-valued option, each separate instance on its
   // own line.
   for (size_t i = 0; i < lists.size(); ++i) {
      for (size_t j = 0; j < lists[i].size(); ++j)
         std::cout << lists[i][j] << ' ';
      std::cout << '\n';
   }

   return 0;
}

这是一个示例调用(住在coliru ):

And here's a sample invocation (live at coliru):

$ ./po --list 1 2 3 --list foo bar --list how now brown cow
1 2 3 
foo bar 
how now brown cow 

这篇关于C ++:升压program_options:参数多个列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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