最近做cache lab 用到了getopt函数, 用man 3 getopt查看了下用法, 做个总结.

描述:getopt函数是用来解析命令行参数的, 以‘-’或‘--’开头的参数为选项元素,除去‘-’或‘--’的选项元素

为选项字符。如果getopt函数被重复调用,则它将会依次返回每个选项元素中的选项字符。

使用getopt函数需要包含以下头文件:

#include

#include

有几个全局变量与getopt函数解析参数有关:

optind: int型, 指示下一个要解析的参数位置,初始时为1.

optarg: char *, 必须接参数的选项元素的参数, 如上面的-nxzz, optarg 就指向"xzz"字符串.

opterr: int 型, 设为0将不打印错误信息.

函数原型为: int getopt(int argc, char * const argv[], const char *optstring);

参数说明: 前两个参数与main函数参数相同, argc保存参数个数,argv[]保存参数数组,第三个参数optstring是你定义

的选项字符组成的字符串, 如"abc",表示该命令有三个选项元素 -a, -b, -c, 选项字符后面如果有一个冒号说

明该选项元素一定有一个参数, 且该参数保存在optarg中, 如"n:t",

表示选项元素n后要接参数, 选项元素t后

不接参数,如 -n xzz -t 或 -nxzz t,有两个冒号说明该选项可接可选参数, 但可选参数不保存在optarg中.

返回值: 如果当前处理的参数为选项元素,且该选项字符在optstring字符串中, 即为你定义的选项, 则返回该

选项字符,如果该选项字符不是你定义的, 那么返回字符'?', 并更新全局变量optind, 指向argc数组中的下一

个参数. 如果当前处理的参数不是选项元素, 则optind偏移向下一个参数, 直到找到第一个选项元素为止, 然后

再按之前描述的操作,如果找不到选项元素, 说明解析结束, 则返回-1.

下面看例子:

#include

#include

int main(int argc, char * const argv[])

{

int opt;

while ((opt = getopt(argc, argv, "nb:o::t")) != -1) {

printf("opt = %c, optarg = %s, optind = %d, argv[%d] = %sn",

opt, optarg, optind, optind, argv[optind]);

}

return 0;

}

假设编译好的可执行文件名为test, test有3个有效参数-n, -b, -t, 其中-n, -t后不接参数, -b后一定要接参数, -o后接可选参数.

# ./test -x -y -z

输出:

./getopt: invalid option -- 'x'

opt = ?, optarg = (null), optind = 2, argv[2] = -y

./getopt: invalid option -- 'y'

opt = ?, optarg = (null), optind = 3, argv[3] = -z

./getopt: invalid option -- 'z'

opt = ?, optarg = (null), optind = 4, argv[4] = (null)

# ./test -n -b xzz -t

opt = n, optarg = (null), optind = 2, argv[2] = -b

opt = b, optarg = xzz, optind = 4, argv[4] = -t

opt = t, optarg = (null), optind = 5, argv[5] = (null)

# ./test -n -bxzz -t

opt = n, optarg = (null), optind = 2, argv[2] = -bxzz

opt = b, optarg = xzz, optind = 3, argv[3] = -t

opt = t, optarg = (null), optind = 4, argv[4] = (null)

# ./test -b -t

opt = b, optarg = -t, optind = 3, argv[3] = (null)

# ./test -t -b

opt = t, optarg = (null), optind = 2, argv[2] = -b

./getopt: option requires an argument -- 'b'

opt = ?, optarg = (null), optind = 3, argv[3] = (null)

# ./test -t a -b xzz

opt = t, optarg = (null), optind = 2, argv[2] = a

opt = b, optarg = xzz, optind = 5, argv[5] = (null)

# ./test -ntb xzz

opt = n, optarg = (null), optind = 1, argv[1] = -ntb

opt = t, optarg = (null), optind = 1, argv[1] = -ntb

opt = b, optarg = xzz, optind = 3, argv[3] = (null)

# ./test -t -o -b xzz

opt = t, optarg = (null), optind = 2, argv[2] = -o

opt = o, optarg = (null), optind = 3, argv[3] = -b

opt = b, optarg = xzz, optind = 5, argv[5] = (null)

# ./test -t -o 10 -b xzz

opt = t, optarg = (null), optind = 2, argv[2] = -o

opt = o, optarg = (null), optind = 3, argv[3] = 10

opt = b, optarg = xzz, optind = 6, argv[6] = (null)

常用用法:

一般是用while循环配合switch语句来使用getopt函数解析参数, 如下:

/*

* 得到参数信息

*/

int getinfo(int argc, char * const argv[], int *ps, int *pE, int *pb, char *trace_name, int *vflag)

{

int opt;

int count_arg = 0;

opterr = 0;

while ((opt = getopt(argc, argv, "vs:E:b:t:")) != -1) {

switch (opt) {

case 'v':

*vflag = 1;

break;

case 's':

++count_arg;

*ps = atoi(optarg);

break;

case 'E':

++count_arg;

*pE = atoi(optarg);

break;

case 'b':

++count_arg;

*pb = atoi(optarg);

break;

case 't':

++count_arg;

strcpy(trace_name, optarg);

break;

default: /* '?' */

Usage();

exit(-1);

break;

}

}

if (count_arg < 4) {

Usage();

exit(-1);

}

return 0;

}

linux getopt命令,Linux中getopt函数用法相关推荐

  1. linux图片裁剪工具,Linux_在Linux的命令行中实现裁剪图片的方法 ,当涉及到在Linux中转换或编辑 - phpStudy...

    在Linux的命令行中实现裁剪图片的方法 当涉及到在Linux中转换或编辑图像文件时,ImageMagick毫无疑问是最为熟知的一体化软件之一.它包含了一整套命令行工具,用以显示.转换,或复制超过20 ...

  2. linux上传文件命令ftp put,Linux ftp 命令行中下载文件get与上传文件put的命令应用详解...

    介绍:从本地以用户anok登录的机器192.168.0.16上通过ftp远程登录到192.168.0.6的ftp服务器上,登录用户名是peo.以下为使用该连接做的实验. 查看远程ftp服务器上用户pe ...

  3. linux get与put,科技常识:Linux ftp 命令行中下载文件get与上传文件put的命令应用详解...

    今天小编跟大家讲解下有关Linux ftp 命令行中下载文件get与上传文件put的命令应用详解 ,相信小伙伴们对这个话题应该也很关注吧,小编也收集到了有关Linux ftp 命令行中下载文件get与 ...

  4. linux上传文件put,详解Linux ftp 命令行中下载文件get与上传文件put的操作方法

    尽管现在有许多好的FTP应用程序,但服务器命令行ftp命令的应用程序仍然很多,下面就让电脑乐园小编带你一起来学习详解Linux ftp 命令行中下载文件get与上传文件put的操作方法. 介绍:从本地 ...

  5. length命令怎么用Matlab,matlab中length函数用法

    matlab中的length表示什么?应该如何使用? length:数组长度(即行数或列数中的较大值): 使用方法: n=length(A):如果A为非空数组,返回行数和列数两者之间数值较大的那一个值 ...

  6. python中max函数用法_Python中max函数用法实例分析

    Python中max函数用法实例分析 更新时间:2015年07月17日 15:45:09 作者:优雅先生 这篇文章主要介绍了Python中max函数用法,实例分析了Python中max函数的功能与使用 ...

  7. linux objdump命令,Linux objdump命令

    一.简介 objdump命令是用查看目标文件或者可执行的目标文件的构成的gcc工具. 二.选项 三.实例 1)显示文件头信息 objdump -f test 2)显示Section Header信息 ...

  8. python中sleep函数用法_sleep函数函数介绍与使用方法详解

    在一些竞猜的网站中,如果我们需要做一个定时执行的功能,比如有一道题,在十秒之内要完成,否则显示"您已超时",如果完成,则跳转到下一道题上面,而这中间有一个十秒的停顿,这样的功能是怎 ...

  9. linux unset命令,Linux unset命令

    Linux unset命令 Linux unset命令用于删除变量或函数. unset为shell内建指令,可删除变量或函数. 语法unset [-fv][变量或函数名称] 参数:-f 仅删除函数. ...

  10. matlab stem函数坐标轴_MATLAB中stem函数用法

    stem(Y) 将数据序列Y从x轴到数据值按照茎状形式画出,以圆圈终止.如果Y是一个矩阵,则将其每一列按照分隔方式画出. stem(X,Y)在X的指定点处画出数据序列Y.  stem(...,'fil ...

最新文章

  1. 如何在Unity中添加三维空间声音Spatial Sounds
  2. CTFshow php特性 web149
  3. table 多行 宽度不一致_layui table 中固定列的行高和table行高不一致
  4. class_create和class_device_create
  5. 无法打开多维数据集(使用Dundas的OLAP显示控件时的一个小的问题)
  6. SQL2005转2000
  7. layui 按钮点击一次后失效_electron-vue自定义边框后点击事件失效问题
  8. vs2012生成的项目,如何在只装有VS2010的电脑上打开
  9. 进击的雨燕----------基本运算符
  10. 学习SQLAlchemy Core
  11. java获取汉字首字母
  12. 换脸算法 X2Face 详解
  13. 数字万用表判断绝缘栅场效应管的好坏
  14. 一转眼,波士顿动力 Atlas 机器人又会过独木桥了!
  15. centos设置密码复杂度及最长使用时间
  16. 有哪些简洁的人生建议?
  17. Linux 下检查 VT-d / IOMMU 是否开启
  18. js实现操作成功之后自动跳转页面
  19. Chromedriver Mirror
  20. CNN与句子分类之动态池化方法DCNN--模型介绍篇

热门文章

  1. 代谢组学资讯,全球爆火的ChatGPT,是如何看待三阴性乳腺癌的?
  2. 操作系统学习(2) 进程管理
  3. xw总结4:jmmntsj
  4. AODV协议的运行方式
  5. 编写一个判别素数(质数)的函数 int isPrime(int x)。在 main 函数中,输入一个正整数 n,输出 1~n 之间的所有素数以及素数的个数
  6. matlab命令行窗口显示长度设置_MATLAB中如何设置坐标轴的显示长度?
  7. JVM - 双亲委派
  8. STM32F411 Discovery学习笔记(四)串口
  9. 明日方舟公式计算机,明日方舟公开招募公式汇总
  10. 【统计学】统计学基础