好的,它因未处理的异常而崩溃:
Unhandled exception at 0x76dbc41f in test.exe: Microsoft C++ exception: boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::program_options::unknown_option> > at memory location 0x0018efd8..
它基本上不认识你-p
,所以阅读我发现它期望参数是长参数的文档。这就是为什么它需要并与--p
.
这是修改后的源(可编译)
#include <boost/program_options.hpp>
#include <iostream>
#include <string>
int main(unsigned int argc, const char** argv)
{
boost::program_options::options_description desc("XXX");
desc.add_options()
("p-arg,p", boost::program_options::value<std::string>(), "pi")
("s", boost::program_options::value<std::string>(), "es");
std::cout << desc << std::endl;
boost::program_options::variables_map vm;
boost::program_options::store(
boost::program_options::parse_command_line(argc, argv, desc), vm);
boost::program_options::notify(vm);
if (vm.count("p-arg"))
std::cout << "p is: " << vm["p-arg"].as<std::string>() << std::endl;
if (vm.count("s"))
std::cout << "s is: " << vm["s"].as<std::string>() << std::endl;
}
您需要进行异常处理try-catch
以处理无法识别的程序选项,否则您可能会进一步崩溃。