2017-02-24 110 views
3

当我在命令行中键入a.out -i file0 file1,我想要的选项-i同时接收file0file1但是,-i只接收file0但不file1提升程序选项多个值的选项

但是,我发现,我不得不键入a.out -i file0 -i file1使-i选项同时接收file0file1

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

您的检举'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”)' –

+2

@SeanCline考虑将它作为答案而不是评论。 –

回答

0

从肖恩·克莱因:

检举你的价值多令牌应使其行为像您期望。

("input-file,i", po::value<vector<std::string>>()->multitoken(), "input file")