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. C#中文件管理的运用(Twelfth Day)

    又到了总结的时间了,今天在云和学院学习了文件管理的一些运用及复习昨天学的里氏转换.今天我就总结下昨天遗留下的问题以及今天所学的知识. 昨天遗留的问题 里氏转换(父类转子类) 例:在这里定义父类Peop ...

  2. 获得view所在的控制器

    - (UIViewController*)getViewController{ for (UIView* next = [self superview]; next; next = next.supe ...

  3. URAL 1225 Flags

    题目:click here #include <bits/stdc++.h> using namespace std; typedef long long ll; ; int n; ll ...

  4. 深夜,用canvas画一个时钟

    深夜,用canvas画一个时钟 查看demo 这几天准备阿里巴巴的笔试,可以说已经是心力交瘁,自从阿里和蘑菇街的内推被刷掉之后,开始越来越怀疑起自己的能力来,虽然这点打击应该是微不足道的.毕竟校招在刚 ...

  5. (IOS)N duplicate symbols for architecture i386

    Xcode编译时的error,出现此情况的汇总引用如下: 1.可能存在两个main入口: 2.导入的.h文件可能误写为.m: 3.两文件间可能存在重复定义的全局变量名: 4.使用#include导入文 ...

  6. javascript 关闭页面提示

    window.onbeforeunload = function (e) { e = e || window.event; // For IE and Firefox prior to version ...

  7. perl学习(4) 子程序

    子程序,类比c语言中的函数,在形式上个人认为最大的区别:没有形参 1.1.定义子程序 1.2.调用 #! /usr/bin/perl sub marine { $n += 1 ; print &quo ...

  8. JQuery实现表格的相同单元格合并的三种方法

    代码: <!DOCTYPE html> <html> <head> <title>merge.html</title> <meta h ...

  9. svn恢复到之前某个版本号

    一直在找svn回滚的方法,这个还是非常有用的,屡试不爽阿 常常因为坑爹的需求,功能要切回到之前的某一个版本号.有两种方法能够实现: 方法1: 用svn merge  1) 先 svn up,保证更新到 ...

  10. ThinkPHP - URL - 伪静态 - 路由 - 重写

    URL: 一.URL规则 1.默认是区分大小写,可以修改(配置文件)为不区分大小写. 2. //修改URL大小写问题 'URL_CASE_INSENSITIVE' =>true, 如果模块名为 ...