gcc 4.7.2 /提升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
它显示在不带(位置)参数的情况下运行时的用法。使用位置命令行参数时为什么会抛出?
您需要将“ input-file”参数添加到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;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句