我正在使用Boost :: Program_options来解析我的命令行,并修改了教程中的一些代码,如下所示:
try { po::options_description desc("Allowed options"); desc.add_options() ("help,h","output help message") ("width,w",po::value<int>()->required()," width") ; po::positional_options_description p; p.add("width",1); po::variables_map vm; po::store(po::command_line_parser(argc,argv). options(desc).positional(p).run(),vm); if (vm.count("help")) { std::cout << "USAGE: " << av[0] << &p << std::endl; return 0; } po::notify(vm); if (vm.count("width")) { std::cout << "width: " << vm["width"].as<int>() << "\n"; } } catch (std::exception& e) { std::cout << e.what() << std::endl; return 1; } catch (...) { std::cout << "Exception of unknown type!" << std::endl; }
解决方法
argc是这里的方法,program_options不会暴露设置了多少选项.不要过度工程.