2015-05-29 36 views
1

GCC 4.7.2 /升压1.58.0问题与升压:: program_options和位置参数

我想要的代码看起来像这样,从实例文档中几乎完全采取:

namespace po = boost::program_options; 

po::options_description desc("Allowed options"); 

desc.add_options() 
    ("help","produce help message") 
; 

po::positional_options_description pos_desc; 

pos_desc.add("input-file",-1); 

po::variables_map vm; 

// The following line throws an std::logic_error 
// what() - error_with_option_name::m_option_style can only be one of 
//   [0, allow_dash_for_short, allow_slash_for_short, allow_long_disguise 
//   or allow_long] 
po::store(po::command_line_parser(argc,argv).options(desc) 
              .positional(pos_desc) 
              .run(), 
      vm); 

... 

一个logic_error例外得到投掷在由注释指示的路线,当我执行的应用程序:

myapp filename1

在没有(位置)参数的情况下运行时显示使用情况。为什么在使用位置命令行参数时抛出?

回答

2

您需要将“输入文件”参数添加到desc,然后到pos_desc,而不仅仅是后者。这是我通常做的一个例子。

namespace po = boost::program_options; 

string fin_name; 

try { 
    po::options_description all_opt("Options"); 
    all_opt.add_options() 
    ("help,h", "produce help message") 
    ("input,i", po::value<string>(&fin_name), 
    "input files with histograms") 
    ; 

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

    po::variables_map vm; 
    po::store(po::command_line_parser(argc, argv) 
    .options(all_opt).positional(pos).run(), vm); 
    if (argc == 1 || vm.count("help")) { 
    cout << all_opt << endl; 
    return 0; 
    } 
    po::notify(vm); 
} 
catch(exception& e) { 
    cerr << "\033[31mError: " << e.what() <<"\033[0m"<< endl; 
    return 1; 
} 
+0

明天工作将会出现这种情况... –