boost 分析命令行参数
- #include <boost/program_options.hpp>
- #include <iostream>
- #include <vector>
- using namespace std;
- using namespace boost::program_options;
- int main(int argc, char* argv[])
- {
- string one ; // 外部变量 存储 参数one的值
- vector<string> mult;
- boost::program_options::options_description opts("test options");
- opts.add_options()
- ("help,h","help info")
- ("test1,t",value<string>(),"test aaa ")
- ("one,o",value<string>(&one)->default_value("one"),"test one default") // 默认值
- ("mult,m",value<vector<string> >(&mult)->multitoken(),"mult test"); //多个参数
- variables_map vm;
- try
- {
- store(parse_command_line(argc,argv,opts),vm); // 分析参数
- }
- catch(boost::program_options::error_with_no_option_name &ex)
- {
- cout<<ex.what()<<endl;
- }
- notify(vm); // 将解析的结果存储到外部变量
- if (vm.count("help"))
- {
- cout<<opts<<endl;
- return -1;
- }
- if(vm.count("test1"))
- {
- cout<<vm["test1"].as<string>()<<endl;
- }
- cout<<one<<endl;
- cout<<mult.size()<<endl;
- getchar();
- return 0;
- }
[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 分析命令行参数的更多相关文章
- getopt函数的使用——分析命令行参数
getopt(分析命令行参数) getopt(分析命令行参数) 短参数的定义 返回值 范例 getopt_long 相关函数表头文件#include<unistd.h> 函数声明int g ...
- 【转】getopt分析命令行参数
(一) 在Linux中,用命令行执行可执行文件时可能会涉及到给其加入不同的参数的问题,例如: ./a.out -a1234 -b432 -c -d 程序会根据读取的参数执行相应的操作,在C语言中,这个 ...
- getopt 分析命令行参数 -n -t 1
在Linux中,我们常常用到 ls -l 等等之类带有选项项的命令,下面,让我们用C++来实现该类似的命令. 在实现之前,首先,我们来介绍一下一个重要函数:getopt() 表头文件 #include ...
- getopt(分析命令行参数)
ref:http://vopit.blog.51cto.com/2400931/440453 相关函数表头文件 #include<unistd.h>定义函数 ...
- 解析main函数的命令行参数
原创文章,转载请正确注明本文原始URL及作者. 介绍 写C/C++程序,我们常常需要把main函数的参数作为选项来传递.在linux中,解析选项有专门的函数可以用. int getopt(int ar ...
- Shell 参数(2) --解析命令行参数工具:getopts/getopt
getopt 与 getopts 都是 Bash 中用来获取与分析命令行参数的工具,常用在 Shell 脚本中被用来分析脚本参数. 两者的比较 (1)getopts 是 Shell 内建命令,geto ...
- 转载:linux编程,命令行参数输入getopt
下面资料来自百度百科: getopt(分析命令行参数) 相关函数 表头文件 #include<unistd.h> 定义函数 int getopt(int argc,char * const ...
- boost之program_options库,解析命令行参数、读取配置文件
一.命令行解析 tprogram_options解析命令行参数示例代码: #include <iostream> using namespace std; #include <boo ...
- python处理命令行参数
直接从命令行执行py文件的时候如果带有参数,如何获取这些参数,如何解析? http://blog.chinaunix.net/uid-20786165-id-3182268.html sys.argv ...
随机推荐
- photoshop使用注意事项
CMYK 与 RGB 任何网络图片都会以RGB模式显示图片: 数码图片以RGB模式被捕捉,因此应在RGB模式下编辑: 大部分工具和滤镜只能在RGB模式下使用: RGB模式和CMYK模式之间不能实现无损 ...
- FreeCodeCamp:Truncate a string
要求: 用瑞兹来截断对面的退路! 截断一个字符串! 如果字符串的长度比指定的参数num长,则把多余的部分用...来表示. 切记,插入到字符串尾部的三个点号也会计入字符串的长度. 但是,如果指定的参数n ...
- URAL 1203 Scientific Conference dp?贪心
题目:click here 分明就是贪心怎么会在dp的专题 #include <bits/stdc++.h> using namespace std; typedef unsigned l ...
- vim 多文件编辑【超实用】
vim 多文件编辑(偶的linux笔记) http://blog.csdn.net/lcj_cjfykx/article/details/18805721 通过vim打开的每个文件都对应着一个buff ...
- Qt Installer Framework的学习
Qt Installer Framework是Qt默认包的发布框架.它很方便,使用静态编译Qt制作而成.并且使用了压缩率很高的7z对组件进行压缩.之所以有这些好处,我才觉得值得花一点儿精力研究一下这个 ...
- web.py入门
官网介绍: web.py is a web framework for Python that is as simple as it is powerful. web.py is in the pub ...
- Python开源异步并发框架
Python开源异步并发框架的未来 2014年3月30日,由全球最大的中文IT社区CSDN主办的“开源技术大会·” (Open Source Technology Conference ,简称OSTC ...
- Maven--几个需要补充的问题(三)
<Maven--搭建开发环境(一)> <Maven--构建企业级仓库(二)> <Maven—几个需要补充的问题(三)> 前两篇由于篇幅太长,为了给读者理解方便,这篇 ...
- zzuli生化危机(dfs)
生化危机 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 73 Solved: 21SubmitStatusWeb Board Description ...
- hdu4405概率dp入门
Aeroplane chess Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...