当我在命令行输入a.out -i file0 file1
时,我希望选项-i
同时接收file0
和file1
但是-i
只接收file0
1}}但不是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;
}
答案 0 :(得分:5)
来自Sean Cline:
将您的值标记为多重语音应该使其按预期运行。
get<T>()