1. #include <boost/program_options.hpp>
  2. #include <iostream>
  3. #include <vector>
  4. using namespace std;
  5. using namespace  boost::program_options;
  6. int main(int argc, char* argv[])
  7. {
  8. string one ; // 外部变量 存储 参数one的值
  9. vector<string> mult;
  10. boost::program_options::options_description opts("test options");
  11. opts.add_options()
  12. ("help,h","help info")
  13. ("test1,t",value<string>(),"test aaa ")
  14. ("one,o",value<string>(&one)->default_value("one"),"test one default") // 默认值
  15. ("mult,m",value<vector<string> >(&mult)->multitoken(),"mult test"); //多个参数
  16. variables_map vm;
  17. try
  18. {
  19. store(parse_command_line(argc,argv,opts),vm); // 分析参数
  20. }
  21. catch(boost::program_options::error_with_no_option_name &ex)
  22. {
  23. cout<<ex.what()<<endl;
  24. }
  25. notify(vm); // 将解析的结果存储到外部变量
  26. if (vm.count("help"))
  27. {
  28. cout<<opts<<endl;
  29. return -1;
  30. }
  31. if(vm.count("test1"))
  32. {
  33. cout<<vm["test1"].as<string>()<<endl;
  34. }
  35. cout<<one<<endl;
  36. cout<<mult.size()<<endl;
  37. getchar();
  38. return 0;
  39. }

[root@localhost test4]# g++ main.cpp  -l boost_program_options
[root@localhost test4]# ./a.out  -h
test options:
  -h [ --help ]           help info
  -t [ --test1 ] arg      test aaa 
  -o [ --one ] arg (=one) test one default
  -m [ --mult ] arg       mult test

[root@localhost test4]# ./a.out  -m f2 f3 f4 --test1 testbbbb
testbbbb
one
3

boost 分析命令行参数的更多相关文章

  1. getopt函数的使用——分析命令行参数

    getopt(分析命令行参数) getopt(分析命令行参数) 短参数的定义 返回值 范例 getopt_long 相关函数表头文件#include<unistd.h> 函数声明int g ...

  2. 【转】getopt分析命令行参数

    (一) 在Linux中,用命令行执行可执行文件时可能会涉及到给其加入不同的参数的问题,例如: ./a.out -a1234 -b432 -c -d 程序会根据读取的参数执行相应的操作,在C语言中,这个 ...

  3. getopt 分析命令行参数 -n -t 1

    在Linux中,我们常常用到 ls -l 等等之类带有选项项的命令,下面,让我们用C++来实现该类似的命令. 在实现之前,首先,我们来介绍一下一个重要函数:getopt() 表头文件 #include ...

  4. getopt(分析命令行参数)

    ref:http://vopit.blog.51cto.com/2400931/440453   相关函数表头文件         #include<unistd.h>定义函数       ...

  5. 解析main函数的命令行参数

    原创文章,转载请正确注明本文原始URL及作者. 介绍 写C/C++程序,我们常常需要把main函数的参数作为选项来传递.在linux中,解析选项有专门的函数可以用. int getopt(int ar ...

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

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

  7. 转载:linux编程,命令行参数输入getopt

    下面资料来自百度百科: getopt(分析命令行参数) 相关函数 表头文件 #include<unistd.h> 定义函数 int getopt(int argc,char * const ...

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

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

  9. python处理命令行参数

    直接从命令行执行py文件的时候如果带有参数,如何获取这些参数,如何解析? http://blog.chinaunix.net/uid-20786165-id-3182268.html sys.argv ...

随机推荐

  1. Oracle创建表、索引、主键、字段描述

    -- 创建表 create table SX04_LBALANCE ( YEAR VARCHAR2(10) not null, PROGRAMNO VARCHAR2(40) not null, FDA ...

  2. myEclipse快捷键及其常用设置

    快捷键:    查找替换:ctrl + f    复制行: ctrl + alt + down    删除行: ctrl + d    插入行: shift + enter, ctrl + shift ...

  3. 2013 Changsha Regional 一样的木板一样的气球

    气球:      木板:   比赛现场: 场外: 曲线....  除了第一题出的太慢了,总体比成都好了很多.... 自此,今年两铜收场,再接再厉,明年要有更好的成绩.

  4. 转: seajs知识点与cmd规范

    在比较之前,我们得先来了解下什么是AMD规范?什么是CMD规范?当然先申明一下,我个人也是总结下而已,也是网上看到的资料,自己总结下或者可以说整理下而已,供大家更深入的了解!因为我们都知道 AMD规范 ...

  5. 数据结构——栈(Stacks)

    栈遵循LIFO ( last in first out) 即后入先出原则 栈结构类似于叠盘子 后叠上去的要先拿走 才能拿到下面的盘子 因此stack是一种访问受限的线性存储结构 用单向链表的结构来存储 ...

  6. 史上最全然oophper php文件上传之文件类型相应表,ie,火狐各一份。

    ie 火狐 id 后缀名 php识别出的文件类型 0 gif image/gif 1 jpg image/jpeg 2 png image/png 3 bmp image/bmp 4 psd appl ...

  7. IAR之工程配置

    参考 : IAR的Workspace顶部下拉菜单中Debug和Release http://blog.csdn.net/yanpingsz/article/details/5588525 ++++++ ...

  8. CMarkUp接口说明

    CMarkup是一个小型XML的分析器,实现语言是C++,英文版的接口说明地址为:http://www.firstobject.com/dn_markupmethods.htm 有厉害的网友已经翻译出 ...

  9. java实现随机验证码的图片

    链接地址:http://blog.sina.com.cn/s/blog_407a68fc010006qo.html 1.一共需要2个常用java文件(RandomCode.java和RandomCod ...

  10. PHP学习笔记1-常量,函数

    常量:使用const(php5)声明,只能被赋值一次,php5以下版本使用define: <?php const THE_VALUE = 100;//PHP5中才有const echo THE_ ...