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

简介: 一、命令行解析 tprogram_options解析命令行参数示例代码:   [cpp] view plaincopy   #include    using namespace std;      #include    namespace po = boos...

一、命令行解析

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

 

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



 

运行结果:

 

输入参数:--help

 

输入参数:--compression 10

 

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

2.1 代码

 

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

2.2 配置文件

 

config.conf:

config2.conf:

2.3 输出结果

 

目录
相关文章
|
24天前
|
算法 Linux 开发者
CMake深入解析:打造高效动态链接库路径设置
CMake深入解析:打造高效动态链接库路径设置
44 0
|
24天前
|
算法 数据处理 开发者
FFmpeg库的使用与深度解析:解码音频流流程
FFmpeg库的使用与深度解析:解码音频流流程
35 0
|
24天前
|
缓存 算法 C语言
【C++ 标准查找算法 】C++标准库查找算法深入解析(In-depth Analysis of C++ Standard Library Search Algorithms)
【C++ 标准查找算法 】C++标准库查找算法深入解析(In-depth Analysis of C++ Standard Library Search Algorithms)
45 0
|
23天前
|
算法 IDE Linux
【CMake 小知识】CMake中的库目标命名和查找策略解析
【CMake 小知识】CMake中的库目标命名和查找策略解析
98 1
|
24天前
|
安全 网络性能优化 Android开发
深入解析:选择最佳C++ MQTT库的综合指南
深入解析:选择最佳C++ MQTT库的综合指南
81 0
|
25天前
|
存储 并行计算 算法
【C++ 函数式编程】深入解析 C++ 函数式编程<functional> 库
【C++ 函数式编程】深入解析 C++ 函数式编程<functional> 库
127 0
|
25天前
|
JSON JavaScript 数据格式
【深入探究C++ JSON库】解析JSON元素的层级管理与遍历手段
【深入探究C++ JSON库】解析JSON元素的层级管理与遍历手段
79 2

推荐镜像

更多