gcc 4.7.2 / boost 1.58.0
我正在尝试看起来像这样的代码,几乎完全取自文档中的示例:
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
它显示了在没有(位置)参数的情况下运行时的用法。为什么在使用位置命令行参数时抛出它?
答案 0 :(得分: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;
}