C++ Boost Program Options 中的向量参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8175723/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 18:07:39  来源:igfitidea点击:

Vector arguments in Boost Program Options

c++boostboost-program-options

提问by Szabolcs

I have two related questions:

我有两个相关的问题:

  1. What is the simplestway to allow passing a series of values, using Boost Program Options? My aim is to avoid prog --opt 1 --opt 2 --opt 3and have prog --opt 1 2 3instead.

  2. What is the simplest way to have an option that takes exactlytwo numbers, e.g. prog --opt 137 42?

  1. 使用 Boost Program Options 允许传递一系列值的最简单方法是什么?我的目标是避免prog --opt 1 --opt 2 --opt 3并拥有prog --opt 1 2 3

  2. 拥有一个恰好需要两个数字的选项的最简单方法是什么,例如prog --opt 137 42

(I don't need any "free" program parameters.)

(我不需要任何“免费”程序参数。)

采纳答案by rcollyer

For the first part, this should work

对于第一部分,这应该有效

namespace po = boost::program_options;
po::option_descriptions desc("");

desc.add_options()
 ("opt", po::value<std::vector<int> >()->multitoken(), "description");

The second part, requires a bit more work. The function po::valuereturns a po::typed_value< T, charT >on which you'll have to override the behavior of several functions, as follows

第二部分,需要更多的工作。该函数po::value返回一个po::typed_value< T, charT >,您必须在其上覆盖多个函数的行为,如下所示

template< typename T, typename charT = char >
class fixed_tokens_typed_value : public po::typed_value< T, charT > {
   unsigned _min, _max;

   typedef po::typed_value< T, charT > base;

 public:

   fixed_tokens_typed_value( T * t, unsigned min, unsigned max ) 
     : _min(min), _max(max), base( t ) {
       base::multitoken();
   }

   virtual multi_typed_value* min_tokens( unsigned min ) {
       _min = min;
       return *this;
   }
   unsigned min_tokens() const {return _min;}

   virtual multi_typed_value* max_tokens( unsigned max ) {
       _max = max;
       return *this;
   }
   unsigned max_tokens() const {return _max;}

   base* zero_tokens() {
       _min = _max = 0;
       base::zero_tokens();
       return *this;
   }
}

which needs to be accompanied by

这需要伴随

template< typename T >
fixed_tokens_typed_value< T > 
fixed_tokens_value(unsigned min, unsigned max) {
    return fixed_tokens_typed_value< T >(0, min, max ); }

template< typename T >
fixed_tokens_typed_value< T > 
fixed_tokens_value(T * t, unsigned min, unsigned max) {
    fixed_tokens_typed_value< T >* r = new
                   fixed_tokens_typed_value< T >(t, min, max);
    return r; }

Then

然后

desc.add_options()
 ("opt", po::fixed_tokens_value<std::vector<int> >(2,2), "description");

should work. I have not yet had a chance to test it, so it likely contains a few bugs. But, at a minimum, should give you an idea of what you need.

应该管用。我还没有机会测试它,所以它可能包含一些错误。但是,至少应该让您了解您需要什么。

回答by Bee

This is a late answer but I hope it helps someone. You could easily use the same technique in item #1, except you need to add another validation on the number of items in your vector:

这是一个迟到的答案,但我希望它可以帮助某人。您可以轻松地在第 1 项中使用相同的技术,但您需要对向量中的项数添加另一个验证:

from rcollyer's example:

来自 rcollyer 的例子:

namespace po = boost::program_options;
po::option_descriptions desc("");

desc.add_options()
 ("opt", po::value<std::vector<int> >()->multitoken(), "description");

po::variables_map vm;
po::store(po::parse_command_line(argc, argv, desc), vm);
po::notify(vm); 

vector<int> opts;
if (!vm["opt"].empty() && (opts = vm["opt"].as<vector<int> >()).size() == 2) {
  // good to go
}