2010-06-30 4 views
7

Vì vậy, tôi đang làm việc với một ví dụ cho thư viện Boost program_options và tôi muốn thử đặt giá trị mặc định cho một trong nhiều giá trị/vector-giá trị, nhưng dường như không làm việc. Như tôi nghĩ là suggested here to work.boost program_options multiple values ​​problems

Những gì tôi đã sửa đổi là vào khoảng dòng 40:

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") 
     ; 

Khi tôi biên dịch thay đổi nhỏ này, tôi hy vọng rằng khi không có tùy chọn -I được truyền rằng "cái gì đó" được thêm vào bao gồm- danh sách đối số đường dẫn.

Có ai có bất kỳ ý tưởng nào tại sao điều này không đúng không?

Dưới đây là toàn bộ mã nguồn:

#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; 
} 

Trả lời

13

Đối với phương pháp "DEFAULT_VALUE", tham số đầu tiên là giá trị thực sự mà bạn muốn lựa chọn của bạn được, giá trị thứ hai là chỉ có đại diện văn bản (đối với hiển thị trong --help) khi tăng không thể suy ra nó.

Vì vậy, giải pháp cho vấn đề của bạn là viết:

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

Bằng cách này, bạn đang nói rằng giá trị mặc định là một véc tơ với một yếu tố duy nhất "cái gì đó", và rằng bạn muốn hiển thị " SOMETHING "trong trợ giúp, chẳng hạn như:

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

Ah hoàn toàn rõ ràng, cảm ơn. – Petriborg

+1

Ngoài ra, nếu bạn đặt chức năng trợ giúp của bạn "template ostream & operator << (ostream & os, const vector & v)" vào không gian tên tăng, program_options sẽ tìm thấy nó, và bạn có thể sử dụng "-> default_value (myvector) ", do đó, các tùy chọn mặc định của bạn sẽ được tự động chuyển đổi thành phần trình bày văn bản. –