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. Android中的一些基础知识(一)

    翻译自这里,并做了部分修改. 什么是Android? Android是为移动设备提供的软件,它包括操作系统.中间件.和一些关键的应用程序.应用程序执行它自己的进程和在Dalvik虚拟机中的实例. An ...

  2. Android各种效果集合

    QQ侧滑风格:http://www.cnblogs.com/lichenwei/p/4111252.html,通过继承HorizontalScrollView类来实现的.

  3. thinkphp小技巧

    if(IS_POST) _404("页面不存在",U("index"))               //如果不是POST提交,则跳转到index.debug模 ...

  4. S2S:分享出的营销机遇

    (速途网专栏 作者:高学争)经常在网上购物的你,有没有遇到过这样的问题:你知道自己想买什么,但是在淘宝上一搜,出来了数以万计的同类型,他们有着同样的价位(甚至可能是同样的图片),但是由不同的商家提供, ...

  5. JS常用方法函数(1)

    1.字符串长度截取 function cutstr(str, len) { var temp, icount = 0, patrn = /[^\x00-\xff]/, strre = "&q ...

  6. Mac平台编译mupdf-qt的开源项目

    How to compile mupdf-qt Compile on Linux Install tools and thirdparty libraries You should install s ...

  7. cocos2dx进阶学习之CCScene

    继承关系 CCScene -> CCNode -> CCObject CCScene抽象了一个场景的概念,类似舞台的一幕 函数 static CCScene *create(void); ...

  8. 关于JAVA 向上转型

    最近复习中比较绕的一个地方 通过试验总结一下 若A为父类 B为子类 声明方式为: A t= new B(); 将子类对象赋值给父类对象,它编译时为父类对象,但运行时却是子类对象: 1)被声明为父类对象 ...

  9. poj 2346 Lucky tickets(区间dp)

    题目链接:http://poj.org/problem?id=2346 思路分析:使用动态规划解法:设函数 d( n, x )代表长度为n且满足左边n/2位的和减去右边n/2位的和为x的数的数目. 将 ...

  10. 介绍一款开源的类Excel电子表格软件

     Excel一直以霸主的地位,占领了Windows桌面表格软件市场No 1,与此同一时候,Office套装产品差点儿成为了IT行业的标配办公技能.有无相似Excel的桌面程序,绿色版,实现主要的数 ...