C++应用之boost解析命令行参数

C++应用之boost解析命令行参数

2023年8月2日发(作者:)

C++应⽤之boost解析命令⾏参数#include

#include #include int main(int argc, char*argv[]){ int level;

boost::program_options::options_description desc("Options"); _options() ("help,h", "produce help message") ("encode", boost::program_options::value(), "set encode level")

("decode", boost::program_options::value(&level)->default_value(1), "set decode level"); boost::program_options::variables_map vm; try { boost::program_options::store(boost::program_options::parse_command_line(argc, argv, desc), vm); boost::program_options::notify(vm); } catch (boost::exception& e) { std::cerr << boost::diagnostic_information(e) << std::endl; return false; } if (("help")) { std::cout << desc << std::endl; return 1; } if (("encode")) { std::cout << "encode level was set to " << vm["encode"].as() << "." << std::endl; } if (("decode")) { std::cout << "decode level was set to " << vm["decode"].as() << "." << std::endl; std::cout << "decode level: " << level << std::endl; } return 0;}/*Debug>high_resolution_ --help Options : -h[--help] produce help message --encode arg set encode level --decode arg(= 1) set decode levelDebug>high_resolution_ - h Options : -h[--help] produce help message --encode arg set encode level --decode arg(= 1) set decode levelDebug>high_resolution_ --encode 2 encode level was set to 2. decode level was set to 1. decode level was set to 1. decode level : 1Debug>high_resolution_ --decode 2 decode level was set to 2. decode level : 2*/#include #include #include

#include

#include std::pair at_option_parser(std::string const& s){ if ('@' == s[0]) { return make_pair(std::string("config"), (1)); } else { return std::pair(); }}int main(int argc, char*argv[]){ std::string host_ip; short host_port; std::string server_ip; short server_port; boost::program_options::options_description hostoptions("host options"); _options() ("host_ip,H", boost::program_options::value(&host_ip), "set db_host") ("host_port,P", boost::program_options::value(&host_port)->default_value(3306), "set db_port"); boost::program_options::options_description general("general options"); _options() ("help,h", "produce help message") ("server_ip,s", boost::program_options::value(&server_ip), "set the http_server's ip. e.g. '202.106.0.20'") ("server_port,p", boost::program_options::value(&server_port)->default_value(80), "set the http_server's port. default:80"); std::string config_file; boost::program_options::options_description config("config options"); _options() ("config", boost::program_options::value(&config_file)->default_value(""), "set config file, specified with '@name' too"); boost::program_options::options_description all("All options"); (hostoptions).add(general).add(config); boost::program_options::variables_map vm; boost::program_options::store(boost::program_options::command_line_parser(argc, argv).options(all).extra_parser(::at_option_parser).run(), vm);

if (("help")) { std::cout << hostoptions << std::endl; std::cout << general << std::endl; std::cout << general << std::endl; std::cout << config << std::endl; std::cout << std::endl; std::cout << all << std::endl; return false; } if (("config")) { std::string conf_name = vm["config"].as(); std::ifstream ifs_config(conf_name.c_str()); if (!ifs_config){ std::cerr << "could not open the configure file" << std::endl; return false; } else { std::cout << conf_name << std::endl; } //boost::program_options::store(boost::program_options::parse_config_file(ifs_config, hostoptions), vm); } boost::program_options::notify(vm);

std::cout << "host_ip: " << host_ip << std::endl; std::cout << "host_port: " << host_port << std::endl; std::cout << "server_ip: " << server_ip << std::endl; std::cout << "server_port: " << server_port << std::endl; return 0;}

发布者:admin,转转请注明出处:http://www.yc00.com/xiaochengxu/1690907663a460453.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信