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. photoshop使用注意事项

    CMYK 与 RGB 任何网络图片都会以RGB模式显示图片: 数码图片以RGB模式被捕捉,因此应在RGB模式下编辑: 大部分工具和滤镜只能在RGB模式下使用: RGB模式和CMYK模式之间不能实现无损 ...

  2. FreeCodeCamp:Truncate a string

    要求: 用瑞兹来截断对面的退路! 截断一个字符串! 如果字符串的长度比指定的参数num长,则把多余的部分用...来表示. 切记,插入到字符串尾部的三个点号也会计入字符串的长度. 但是,如果指定的参数n ...

  3. URAL 1203 Scientific Conference dp?贪心

    题目:click here 分明就是贪心怎么会在dp的专题 #include <bits/stdc++.h> using namespace std; typedef unsigned l ...

  4. vim 多文件编辑【超实用】

    vim 多文件编辑(偶的linux笔记) http://blog.csdn.net/lcj_cjfykx/article/details/18805721 通过vim打开的每个文件都对应着一个buff ...

  5. Qt Installer Framework的学习

    Qt Installer Framework是Qt默认包的发布框架.它很方便,使用静态编译Qt制作而成.并且使用了压缩率很高的7z对组件进行压缩.之所以有这些好处,我才觉得值得花一点儿精力研究一下这个 ...

  6. web.py入门

    官网介绍: web.py is a web framework for Python that is as simple as it is powerful. web.py is in the pub ...

  7. Python开源异步并发框架

    Python开源异步并发框架的未来 2014年3月30日,由全球最大的中文IT社区CSDN主办的“开源技术大会·” (Open Source Technology Conference ,简称OSTC ...

  8. Maven--几个需要补充的问题(三)

    <Maven--搭建开发环境(一)> <Maven--构建企业级仓库(二)> <Maven—几个需要补充的问题(三)> 前两篇由于篇幅太长,为了给读者理解方便,这篇 ...

  9. zzuli生化危机(dfs)

    生化危机 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 73  Solved: 21SubmitStatusWeb Board Description ...

  10. hdu4405概率dp入门

    Aeroplane chess Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...