提升程序选项多个值的选项
问题描述:
当我在命令行中键入a.out -i file0 file1
,我想要的选项-i
同时接收file0
和file1
但是,-i
只接收file0
但不file1
提升程序选项多个值的选项
但是,我发现,我不得不键入a.out -i file0 -i file1
使-i
选项同时接收file0
和file1
能boost::program_options
做到这一点?适应
代码从http://www.boost.org/doc/libs/1_62_0/libs/program_options/example/options_description.cpp
#include <boost/program_options.hpp>
using namespace boost;
namespace po = boost::program_options;
#include <iostream>
#include <algorithm>
#include <iterator>
using namespace std;
// A helper function to simplify the main part.
template<class T>
ostream& operator<<(ostream& os, const vector<T>& v)
{
copy(v.begin(), v.end(), ostream_iterator<T>(os, " "));
return os;
}
int main(int ac, char* av[])
{
try {
int opt;
int portnum;
po::options_description desc("Allowed options");
desc.add_options()
("help", "produce help message")
("input-file,i", po::value< vector<std::string> >(), "input "
"file")
;
po::variables_map vm;
po::store(po::command_line_parser(ac, av).
options(desc).run(), vm);
po::notify(vm);
if (vm.count("help")) {
cout << "Usage: options_description [options]\n";
cout << desc;
return 0;
}
if (vm.count("input-file"))
{
cout << "Input files are: "
<< vm["input-file"].as< vector<std::string> >() << "\n";
}
}
catch(std::exception& e)
{
cout << e.what() << "\n";
return 1;
}
return 0;
}
答
从肖恩·克莱因:
检举你的价值多令牌应使其行为像您期望。
("input-file,i", po::value<vector<std::string>>()->multitoken(), "input file")
您的检举'value'如['multitoken'](http://www.boost.org/doc/libs/1_63_0/doc/html/boost/program_options/typed_value.html#idp908724720-bb)应该让它按照你的期望行事。 '(“input-file,i”,po :: value>() - > multitoken(),“input file”)' –
@SeanCline考虑将它作为答案而不是评论。 –