2010-06-30 4 views
7

그래서 Boost program_options 라이브러리의 예제 중 하나를 사용하고 다중 값/벡터 값 중 하나에 대한 기본값을 설정하려고 시도했지만 보이지 않습니다. 일하다. 제 생각에는 suggested here to work입니다. 나는이 작은 변화를 컴파일 할 때boost program_options multiple values ​​problem

po::options_description config("Configuration"); 
    config.add_options() 
     ("optimization", po::value<int>(&opt)->default_value(10), 
       "optimization level") 
     ("include-path,I", o::value< vector<string> >()->default_value(vector<string>(),"SOMETHING")->composing(), "include path") 
     ; 

, 나는 더 -I 옵션이 전달되지 않은 경우 "뭔가"가 include-에 추가됩니다 기대 :

내가 수정 한 것은 라인 (40)에 대한에 경로 인수 목록

아무도 모르는 이유가 있습니까? 여기

는 완전한 소스 코드입니다은 "DEFAULT_VALUE"방법

#include <boost/program_options.hpp> 
namespace po = boost::program_options; 


#include <iostream> 
#include <fstream> 
#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>(cout, " ")); 
    return os; 
} 


int main(int ac, char* av[]) 
{ 
    try { 
     int opt; 

     // Declare a group of options that will be 
     // allowed only on command line 
     po::options_description generic("Generic options"); 
     generic.add_options() 
      ("version,v", "print version string") 
      ("help", "produce help message")  
      ; 

     // Declare a group of options that will be 
     // allowed both on command line and in 
     // config file 
     po::options_description config("Configuration"); 
     config.add_options() 
      ("optimization", po::value<int>(&opt)->default_value(10), 
        "optimization level") 
      ("include-path,I", 
       po::value< vector<string> >()->default_value(vector<string>(),"SOMETHING")->composing(), 
       "include path") 
      ; 

     // Hidden options, will be allowed both on command line and 
     // in config file, but will not be shown to the user. 
     po::options_description hidden("Hidden options"); 
     hidden.add_options() 
      ("input-file", po::value< vector<string> >(), "input file") 
      ; 


     po::options_description cmdline_options; 
     cmdline_options.add(generic).add(config).add(hidden); 

     po::options_description config_file_options; 
     config_file_options.add(config).add(hidden); 

     po::options_description visible("Allowed options"); 
     visible.add(generic).add(config); 

     po::positional_options_description p; 
     p.add("input-file", -1); 

     po::variables_map vm; 
     store(po::command_line_parser(ac, av). 
       options(cmdline_options).positional(p).run(), vm); 

     ifstream ifs("multiple_sources.cfg"); 
     store(parse_config_file(ifs, config_file_options), vm); 
     notify(vm); 

     if (vm.count("help")) { 
      cout << visible << "\n"; 
      return 0; 
     } 

     if (vm.count("version")) { 
      cout << "Multiple sources example, version 1.0\n"; 
      return 0; 
     } 

     if (vm.count("include-path")) 
     { 
      cout << "Include paths are: " 
       << vm["include-path"].as< vector<string> >() << "\n"; 
     } 

     if (vm.count("input-file")) 
     { 
      cout << "Input files are: " 
       << vm["input-file"].as< vector<string> >() << "\n"; 
     } 

     cout << "Optimization level is " << opt << "\n";     
    } 
    catch(exception& e) 
    { 
     cout << e.what() << "\n"; 
     return 1; 
    }  
    return 0; 
} 

답변

13

첫 번째 매개 변수는 당신이 두 번째 값은 텍스트 표현되고 귀하의 옵션이 될하고자하는 실제 값 (이다 부스트가 그것을 추론 할 수 없을 때 --help에 표시). "

po::value< vector<string> >()->default_value(
     vector<string>(1, "SOMETHING"), "SOMETHING")->composing(), 

이 방법은, 기본 값은 하나의 요소"뭔가 "와 벡터라고 말하는, 당신은 표시하도록 :

그래서, 문제에 대한 솔루션을 작성하는 것입니다 뭔가 "와 같은 도움 :

Configuration: 
    --optimization arg (=10)    optimization level 
    -I [ --include-path ] arg (=SOMETHING) 
             include path 
+0

아, 완벽하게, 감사합니다. – Petriborg

+1

또한 booster 네임 스페이스에 도우미 함수 "템플릿 ostream & operator << (ostream & os, const vector & v)"를 추가하면 program_options가 찾은 다음 "-> default_value (myvector) ", 그래서 귀하의 기본 옵션은 자동으로 텍스트 표현으로 변환됩니다. –

관련 문제