boost之program_options库,解析命令行参数、读取配置文件

时间:2023-03-08 21:57:38

一、命令行解析

tprogram_options解析命令行参数示例代码:

  1. #include <iostream>
  2. using namespace std;
  3. #include <boost/program_options.hpp>
  4. namespace po = boost::program_options;
  5. int main(int argc, char*argv[])
  6. {
  7. //int level;
  8. po::options_description desc("Allowed options");
  9. desc.add_options()
  10. ("help", "produce help message")
  11. //("help,h", "produce help message")
  12. ("compression", po::value<int>(), "set compression level");
  13. //("compression", po::value<int>(&level)->default_value(1), "set compression level");
  14. po::variables_map vm;
  15. po::store(po::parse_command_line(argc, argv, desc), vm);
  16. po::notify(vm);
  17. if(vm.count("help"))
  18. {
  19. cout<<desc<<endl;
  20. return 1;
  21. }
  22. if(vm.count("compression"))
  23. {
  24. cout<<"compression level was set to "<<vm["compression"].as<int>()<<"."<<endl;
  25. //cout<<"compression level: "<<level<<endl;
  26. }
  27. else
  28. {
  29. cout<<"compression level was not set."<<endl;
  30. }
  31. return 0;
  32. }

运行结果:

输入参数:--help

boost之program_options库,解析命令行参数、读取配置文件

输入参数:--compression 10

boost之program_options库,解析命令行参数、读取配置文件

二、读取配置文件(Linux、Windows均可)

2.1 代码

  1. #include <fstream>
  2. #include <map>
  3. using namespace std;
  4. #include <boost/program_options.hpp>
  5. using namespace boost;
  6. namespace po = boost::program_options;
  7. #ifdef WIN32
  8. #include "C:\Users\gwy8868\Desktop\fast_dr302\fast_dr302\global\xtokens.h"
  9. #else
  10. #include "/opt/guowenyan/fast_dr302/global/xtokens.h"
  11. #endif
  12. std::pair<std::string, std::string> at_option_parser(std::string const& s)
  13. {
  14. if ('@' == s[0])
  15. {
  16. return make_pair(std::string("config"), s.substr(1));
  17. }
  18. else
  19. {
  20. return std::pair<std::string, std::string>();
  21. }
  22. }
  23. int main(int argc, char*argv[])
  24. {
  25. //
  26. string host_ip;
  27. short  host_port;
  28. string server_ip;
  29. short  server_port;
  30. //
  31. po::options_description hostoptions("host options");
  32. hostoptions.add_options()
  33. ("host_ip,H", po::value<string>(&host_ip), "set db_host")
  34. ("host_port,P", po::value<short>(&host_port)->default_value(3306), "set db_port");
  35. po::options_description general("general options");
  36. general.add_options()
  37. ("help,h", "produce help message")
  38. ("server_ip,s", po::value<string>(&server_ip), "set the http_server's ip. e.g. '202.106.0.20'")
  39. ("server_port,p", po::value<short>(&server_port)->default_value(80), "set the http_server's port. default:80");
  40. string config_file;
  41. po::options_description config("config options");
  42. config.add_options()
  43. ("config", po::value<string>(&config_file)->default_value("config.conf"),
  44. "set config file, specified with '@name' too");
  45. po::options_description all("All options");
  46. all.add(hostoptions).add(general).add(config);
  47. po::variables_map vm;
  48. po::store(po::command_line_parser(argc, argv).options(all).extra_parser(::at_option_parser).run(), vm);
  49. if (vm.count("help"))
  50. {
  51. cout << hostoptions <<endl;
  52. cout << general << endl;
  53. cout << config << endl;
  54. return false;
  55. }
  56. if (vm.count("config"))
  57. {
  58. string conf_name = vm["config"].as<string>();
  59. ifstream ifs_config(conf_name.c_str());
  60. if (! ifs_config)
  61. {
  62. cerr << "could not open the configure file" << endl;
  63. return false;
  64. }
  65. stringstream ss_config;
  66. ss_config << ifs_config.rdbuf();
  67. global::strings_t args;
  68. global::separate_tokens(ss_config.str(), args, " \r\n");
  69. po::store(po::command_line_parser(args).options(all).run(), vm);
  70. }
  71. po::notify(vm);
  72. //
  73. cout<<"host_ip: "<<host_ip<<endl;
  74. cout<<"host_port: "<<host_port<<endl;
  75. cout<<"server_ip: "<<server_ip<<endl;
  76. cout<<"server_port: "<<server_port<<endl;
  77. return 0;
  78. }

2.2 配置文件

config.conf:

boost之program_options库,解析命令行参数、读取配置文件

config2.conf:
boost之program_options库,解析命令行参数、读取配置文件

2.3 输出结果

boost之program_options库,解析命令行参数、读取配置文件