zoukankan      html  css  js  c++  java
  • boost之program_options库,解析命令行参数、读取配置文件

    一、命令行解析

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

    [cpp] view plaincopy在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 plaincopy在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:Usersgwy8868Desktopfast_dr302fast_dr302globalxtokens.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, "  ");  
    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 输出结果

  • 相关阅读:
    C语言提高代码效率的几种方法
    如何写出高效率稳定的单片机代码
    位运算之 C 与或非异或
    C语言中位运算符异或“∧”的作用
    位运算之——按位与(&)操作——(快速取模算法)
    案例分析
    2018年春季个人阅读计划
    《我们应当怎样做需求分析》读书笔记
    寒假——练车、脑力风暴和辅导初中生
    需求工程:软件建模与分析 读书笔记三
  • 原文地址:https://www.cnblogs.com/lidabo/p/3906053.html
Copyright © 2011-2022 走看看