一、命令行解析

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

输入参数:--compression 10

二、读取配置文件(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:

config2.conf:

2.3 输出结果

boost之program_options库,解析命令行参数、读取配置文件的更多相关文章

  1. linux 中解析命令行参数(getopt_long用法)

    linux 中解析命令行参数(getopt_long用法) http://www.educity.cn/linux/518242.html 详细解析命令行的getopt_long()函数 http:/ ...

  2. optparse模块解析命令行参数的说明及优化

    一.关于解析命令行参数的方法 关于“解析命令行参数”的方法我们一般都会用到sys.argv跟optparse模块.关于sys.argv,网上有一篇非常优秀的博客已经介绍的很详细了,大家可以去这里参考: ...

  3. python解析命令行参数

    常常需要解析命令行参数,经常忘记,好烦,总结下来吧. 1.Python 中也可以所用 sys 的 sys.argv 来获取命令行参数: sys.argv 是命令行参数列表 参数个数:len(sys.a ...

  4. C语言中使用库函数解析命令行参数

    在编写需要命令行参数的C程序的时候,往往我们需要先解析命令行参数,然后根据这些参数来启动我们的程序. C的库函数中提供了两个函数可以用来帮助我们解析命令行参数:getopt.getopt_long. ...

  5. Windows下解析命令行参数

    linux通常使用GNU C提供的函数getopt.getopt_long.getopt_long_only函数来解析命令行参数. 移植到Windows下 getopt.h #ifndef _GETO ...

  6. 3.QT中QCommandLineParser和QCommandLineOption解析命令行参数

     1  新建项目 main.cpp #include <QCoreApplication> #include <QCommandLineParser> #include & ...

  7. 使用 Apache Commons CLI 解析命令行参数示例

    很好的输入参数解析方法 ,转载记录下 转载在: https://www.cnblogs.com/onmyway20xx/p/7346709.html Apache Commons CLI 简介 Apa ...

  8. Shell 参数(2) --解析命令行参数工具:getopts/getopt

    getopt 与 getopts 都是 Bash 中用来获取与分析命令行参数的工具,常用在 Shell 脚本中被用来分析脚本参数. 两者的比较 (1)getopts 是 Shell 内建命令,geto ...

  9. getopt_long函数解析命令行参数

    转载:http://blog.csdn.net/hcx25909/article/details/7388750 每一天你都在使用大量的命令行程序,是不是感觉那些命令行参数用起来比较方便,他们都是使用 ...

随机推荐

  1. slf4j 和 log4j合用的(Maven)配置(转)

    简述:添加logger的日志输出,下面是配置信息供备忘 步骤:1. 在Maven的porn.xml 文件中添加dependency如下 <dependency> <groupId&g ...

  2. IT第十九天 - 继承、接口、多态、面向对象的编程思想

    IT第十九天 上午 继承 1.一般情况下,子类在继承父类时,会调用父类中的无参构造方法,即默认的构造方法:如果在父类中只写了有参的构造方法,这时如果在子类中继承时,就会出现报错,原因是子类继承父类时无 ...

  3. 解题报告 HDU1789 Doing Homework again

    Doing Homework again Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64 ...

  4. DicomIoException: Requested 132 bytes past end of fixed length stream.

    今天在用DicomFile.Open(Stream s)这个接口时,遇到一个异常:      DicomIoException: Requested 132 bytes past end of fix ...

  5. 请问set JAVA_OPTS的各项參数是什么意思?

    http://topic.csdn.net/u/20090910/10/20c6ba01-28ac-482e-94b2-bfce0a952f77.html 请问set JAVA_OPTS的各项參数是什 ...

  6. 典型c库函数的实现

    StrToInt:字符串转int输出 enum Status { kValid = , kInvalid = , }; int StrToInt(const char* str) { g_nStatu ...

  7. javascript 生成页面轮播元素

    <html xmlns="http://www.w3.org/1999/xhtml"><head runat="server">    ...

  8. for循环语句之棋盘放粮食、百鸡百钱、纸张的折叠问题

    1.棋盘放粮食 ; ; i < ; i++) { ; ; j <= i; j++) { x = x * ; } lszl = lszl + x; } double zl = lszl * ...

  9. for(;;)和 while(1) 有什么区别吗?for()和while()的使用情景。

    1 for(;;)和 while(1) 有什么区别吗? void main(void) { ; // for(;;) ) { a++; } } arm-linux-gcc -c -o for.o fo ...

  10. BZOJ 1798: [Ahoi2009]Seq 维护序列seq( 线段树 )

    线段树.. 打个 mul , add 的标记就好了.. 这个速度好像还挺快的...( 相比我其他代码 = = ) 好像是#35.. ---------------------------------- ...