码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2014-08-12 00:32:53      阅读:358      评论:0      收藏:0      [点我收藏+]

标签:des   blog   http   os   io   strong   文件   for   

一、命令行解析

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

 

[cpp] view plaincopybubuko.com,布布扣bubuko.com,布布扣
 
  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

bubuko.com,布布扣

 

输入参数:--compression 10

bubuko.com,布布扣

 

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

2.1 代码

 

[cpp] view plaincopybubuko.com,布布扣bubuko.com,布布扣
 
  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:

bubuko.com,布布扣

config2.conf:
bubuko.com,布布扣

2.3 输出结果

bubuko.com,布布扣

 

boost之program_options库,解析命令行参数、读取配置文件,布布扣,bubuko.com

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

标签:des   blog   http   os   io   strong   文件   for   

原文地址:http://www.cnblogs.com/lidabo/p/3906053.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!