我想处理(注意,不要以任何方式使用)未知选项。
所以有这个:
http://www.boost.org/doc/libs/1_54_0/doc/html/program_options/howto.html#idp123440592
这可用于收集和使用任何未知选项,所以我可以这样做:
po::variables_map vm;
po::parsed_options parsed = po::command_line_parser(ac,av).options(desc).allow_unregistered().run();
{
vector<string> to_pass_further = collect_unrecognized(parsed.options, po::include_positional);
if (to_pass_further.size())
{
cout << "Unrecognized options:" << endl;
for (auto i = to_pass_further.cbegin(); i != to_pass_further.cend(); ++i)
{
cout << std::left << std::setw(2) << ' ' << *i;
return -1;
}
}
}
但是既然我认为人们会一直这样做,而且图书馆应该处理我们一直在做的所有事情,我是否会错过一些清洁工具呢?
答案 0 :(得分:2)
try
{
po::options_description desc("Allowed options");
desc.add_options()
("help,h", "produce help message")
("compression", po::value<int>()->required(), "set compression level")
;
po::variables_map vm;
po::store(po::parse_command_line(ac, av, desc), vm);
po::notify(vm);
}
catch(std::exception& e)
{
std::cerr << "Error: " << e.what() << "\n";
return -1;
}
catch(...)
{
std::cerr << "Unknown error!" << "\n";
return -1;
}