由于系统库getopt和getopt_long用起来不够直观,仔细看了下boost发现Boost.Program_options可以满足我的需求,它和getopt系列函数一样,可以抓起命令行参数,这里写下我对Boost.Program_options的理解。

一、getopt的不足

使用过了getopt函数后,我感觉它不足的地方(可能是我没研究明白的问题),我觉得getopt系列函数的不足有下面几点:

1、使用switch-case语句,导致程序看起来不是太美观,不怎么好看

2、没法使用一个参数接受N个值,如-a 1 2 3 4..n -b..这样

3、程序里变量的控制,只是一些自己定义的字符(如v,h等等),只有程序设计的人才知道这些字符代表的什么意思,做不到一目了然,与现代程序设计不符合

当然getopt也有自己优势,这里就不再罗嗦,直接开始boost命令行参数。

二、使用前准备

当然我们在使用boost库时,需要安装boost库,这里也不再罗嗦,自己上网搜索,很简单。在使用的时候,我们要首先引入头文件:#include <boost/program_options.hpp>,同时不要忘记了要使用该boost库的命名空间:boost::program_options

三、实战

由于boost库的写法比较怪异,跟我们平常的写法有所区别,最好先把程序运行下,然后再看程序,能够很快理解程序的意思,下面的例子由易到难。

例子1

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

编译:g++ -L/usr/local/lib -lboost_program_options

结果:./a.out --help

Allow options:

     --help                       produce help message

      --compresion arg      set compression level

./a.out --compression 10

Compression level was set to 10


上面的例子我们只是演示的只有int类型,还可以有其他的类型,同时上面的例子,也是无法做到一个参数后跟N个值,下面就说怎么解决这个问题

例子2

  1. #include<iostream>
  2. #include <boost/program_options.hpp>
  3. using namespace std;
  4. using namespace boost::program_options;
  5. int main(int argc, char **argv)
  6. {
  7. int opt;
  8. options_description desc("Allowed options");
  9. desc.add_options()
  10. ("help", "produce help message")
  11. ("compression", value<int>(), "set compression level")
  12. ("optimizaiton", value<int>(&opt)->default_value(), "optimization level")
  13. ("input-file,i", value<vector<string>>(), "input file")//这里注意input-file和i之间不能有空格
  14. ("include-path", value<vector<int>>(), "include path")
  15. ;
  16.  
  17. //note here
  18. positional_options_description p;
  19. p.add("include-path“, -1)
  20. //这里只会有一个生效,就是后面的那个,他会把命令行中哪些没有参数值默认为
  21. //你这里设置的值
  22. //p.add("include-path", -1)
  23.  
  24. variables_map vm;
  25. store(command_line_parser(argc, argv).options(desc).positional(p).run(), vm);
  26. notify(vm);
  27.  
  28. if (vm.count("help")) {
  29. cout << desc << "\n";
  30. return ;
  31. }
  32.  
  33. if (vm.count("compression")) {
  34. cout << "Compression level was set to " << vm["compression"].as<int>() << ".\n";
  35. }
  36.  
  37. if (vm.count("include-path")) {
  38. for (auto& v: vm["include-path"].as<vector<int>>()) {
  39. cout << "include-path: " << v << endl;
  40. }
  41. }
  42.  
  43. if (vm.count("input-file")) {
  44. for (auto &v : vm["input-file"].as<vector<string>>()) {
  45. cout << "input-file: " << v << endl;
  46. }
  47. }
  48. }

测试结果:

./a.out --compression 10 -i a.cpp --include-path 10 20 40

  1. Compression level was set 10.
    input-file: a.cpp
    include-path: 10
    include-path: 20
    include-path: 40

  1. 下面介绍多个options_description之间的配合使用,这样让你的程序更加专业化
  1. #include<iostream>
  2. #include <boost/program_options.hpp>
  3. #include <string>
  4. #include <vector>
  5. using namespace std;
  6. using namespace boost::program_options;
  7. int main(int argc, char **argv)
  8. {
  9. options_description generic("Generic options");
  10. generic.add_options()
  11. ("version,v", "print version string")
  12. ("help,h", "produce help message")
  13. ;
  14.  
  15. int opt = ;
  16. options_description config("Configuration");
  17. config.add_options()
  18. ("optimization", value<int>(&opt)->default_value(), "optimization level")
  19. ("include-path,I", value<string>(),"include path")
  20. ("compression", value<int>(), "compression")
  21. ;
  22.  
  23. options_description hidden("Hidden options");
  24. hidden.add_options()
  25. ("input-file", value<vector<string>>(), "input file")
  26. ;
  27.  
  28. options_description cmdline_options("Allow options");
  29. cmdline_options.add(generic).add(config).add(hidden);
  30.  
  31. positional_options_description p;
  32. p.add("input-file", -);
  33.  
  34. variables_map vm;
  35. store(parse_command_line(argc, argv).
  36. options(cmdline_options).positional(p).run(), vm);
  37. notify(vm);
  38.  
  39. if (vm.count("help")) {
  40. cout << desc << "\n";
  41. return ;
  42. }
  43.  
  44. if (vm.count("compression")) {
  45. cout << "Compression level was set to " << vm["compression"].as<int>() << ".\n";
  46. }
  47.  
  48. if (vm.count("input-file")) {
  49. for (auto &v : vm["input-file"].as<vector<string>>()) {
  50. cout << "input-file: " << v << endl;
  51. }
  52.  
  53. }
  54. }
  1. 结果:
  2.  
  3. Allow options:
  4.  
  5. Generic options:
    -v [--version] print version string
    -h [--help] produce help message
  6.  
  7. Configuration:
    --optimization arg(=10) optimization level
    -I [ --include-path ] arg include path
    --compression arg compression
  8.  
  9. Hidden options:
    --input-file args input file
  10.  

  1. 下面尽可能解释一下,程序中的难点,解释不对的地方欢迎指正~
    1

  1. 欢迎转载,但是请指明出处以及作者,谢谢~

boost:program_options的更多相关文章

  1. 使用Boost program_options控制程序输入

    简介 很多人使用界面来输入数据,本文的程序介绍如何使用Boost的program_options控制输入. 程序中使用了: 1. 短选项 2. 可以指定多个参数的选项 程序 #include < ...

  2. boost::program_options 解析命令行参数

    源码: #include <boost/program_options.hpp> namespace po = boost::program_options; int main(int a ...

  3. Boost命令行解释器的简单使用:Boost.Program_options

    简介 如果使用比较多的命令行程序的话,对于命令行参数的输入肯定不会陌生,大部分的程序都是通过类似下面的形式进行输入的,比如熟悉的ls ls --all -l --color=auto 这里面包含了三种 ...

  4. [C++Boost]程序参数项解析库Program_options使用指南

    介绍 程序参数项(program options)是一系列name=value对,program_options 允许程序开发者获得通过命令行(command line)和配置文件(config fi ...

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

    一.命令行解析 tprogram_options解析命令行参数示例代码: #include <iostream> using namespace std; #include <boo ...

  6. boost库中的 program_options

    1.阅读rviz中的源码时在rviz/visualizer_app.cpp中遇到如下代码: po::options_description options; options.add_options() ...

  7. boost::xml——基本操作以及中文乱码解决方案

    下面是本人使用boost库的xml部分的基础操作,并且解决对于大家使用boost库读写中文xml内容出现的乱码问题. 1.实现boost库xml基本操作2.解决boost对xml中中文乱码问题3.实现 ...

  8. boost::xml——基本操作以及中文乱码解决方案 (续)

    本博文主要想说明以下两点: 1.对于上一篇的<boost::xml——基本操作以及中文乱码解决方案>解释,这篇博文基本解决了正确输入输出中英文问题,但是好像还没有解决修改中文出现乱码的问题 ...

  9. program_options禁止命令行短参数

    典型的 boost program_options的用法如下: #include <boost/program_options.hpp> using namespace boost::pr ...

随机推荐

  1. 用shell获取文件大小

    ls -l filename | awk '{ print $5,$9 }' ls -l filename | awk '{ print $5 }'

  2. Android系统中设置TextView等的行间距

    1.android:lineSpacingExtra 设置行间距,如”2dp”. 2.android:lineSpacingMultiplier 设置行间距的倍数,如”2″.

  3. uiview 的setAnimationTransition : forView 方法实现翻页效果

    [UIView beginAnimations:nil context:nil]; [UIView setAnimationTransition:UIViewAnimationTransitionCu ...

  4. 通过layer的contents属性来实现uiimageview的淡入切换

    #import "ViewController.h" @interface ViewController () @property(nonatomic,strong)CALayer ...

  5. [SEO] 网站标题分隔符

    标题用什么分隔符对SEO最有利 我们在看同行的朋友对网站标题优化时,关键词分按照主次的顺序进行分布,在网站标题或者是关键词之间都会有一个符号,俗话来讲就称为关键词分隔符,网站标 题分隔符分为“-”(横 ...

  6. 深入理解HTTPS通讯原理

    一.HTTPS简介 HTTPS(Hyper Text Transfer Protocol over Secure Socket Layer),简单来讲就是加了安全的HTTP,即HTTP+SSL:我们知 ...

  7. [转]Web Services使用out参数

    本文转自:http://www.cnblogs.com/zhaozhan/archive/2010/10/25/1860837.html Web Services使用out参数,在SOAP协议中会跟返 ...

  8. angular-ui-router state.go not passing data to $stateParams

    app.js中定义了一个state如下,url接收一个id参数 $stateProvider.state("page.details", { url: "/details ...

  9. 四舍五入PK银行四舍五入

    描述 在实际开发中decimal.Round(1.23525,4)!=1.2353实际是1.2352,而decimal.Round(1.23535,4)==1.2354 说明 四舍五入:当舍去位的数值 ...

  10. OpenShare:前所未有的开放性

    客户总是面临一个选择:开放的企业门户产品 vs 封闭的企业门户产品 市场上大多数企业门户产品是自成一体的其实也就是封闭的,他们不能和企业目录集成,不能和Exchange集成,不能和SAP集成,不能和L ...