1、定义

int getopt(int argc, char * const argv[], const char *optstring);

2、描述

getopt是用来解析命令行选项参数的,但是只能解析短选项: -d 100,不能解析长选项:--prefix

3、参数

argc:main()函数传递过来的参数的个数
argv:main()函数传递过来的参数的字符串指针数组
optstring:选项字符串,告知 getopt()可以处理哪个选项以及哪个选项需要参数

4、返回

如果选项成功找到,返回选项字母;如果所有命令行选项都解析完毕,返回 -1;如果遇到选项字符不在 optstring 中,返回字符 '?';如果遇到丢失参数,那么返回值依赖于 optstring 中第一个字符,如果第一个字符是 ':' 则返回':',否则返回'?'并提示出错误信息。

5、下边重点举例说明optstring的格式意义:

char*optstring = “ab:c::”;
单个字符a         表示选项a没有参数            格式:-a即可,不加参数
单字符加冒号b:     表示选项b有且必须加参数      格式:-b 100或-b100,但-b=100错
单字符加2冒号c::   表示选项c可以有,也可以无     格式:-c200,其它格式错误

上面这个 optstring 在传入之后,getopt 函数将依次检查命令行是否指定了 -a, -b, -c(这需要多次调用 getopt 函数,直到其返回-1),当检查到上面某一个参数被指定时,函数会返回被指定的参数名称(即该字母)

optarg —— 指向当前选项参数(如果有)的指针。
optind —— 再次调用 getopt() 时的下一个 argv指针的索引。
optopt —— 最后一个未知选项。
opterr ­—— 如果不希望getopt()打印出错信息,则只要将全域变量opterr设为0即可。

以上描述的并不生动,下边结合实例来理解:

6、实例:

#include<stdio.h>
#include<unistd.h>
#include<getopt.h>
int main(intargc, char *argv[])
{int opt;char *string = "a::b:c:d";while ((opt = getopt(argc, argv, string))!= -1){  printf("opt = %c\t\t", opt);printf("optarg = %s\t\t",optarg);printf("optind = %d\t\t",optind);printf("argv[optind] = %s\n",argv[optind]);}
}

编译上述程序并执行结果:

输入选项及参数正确的情况

dzlab:~/test/test#./opt -a100 -b 200 -c 300 -d
opt = a         optarg = 100            optind = 2              argv[optind] = -b
opt = b         optarg = 200            optind = 4              argv[optind] = -c
opt = c         optarg = 300            optind = 6              argv[optind] = -d
opt = d         optarg = (null)         optind = 7              argv[optind] = (null)

或者这样的选项格式(注意区别):

dzlab:~/test/test#./opt -a100 -b200 -c300 -d
opt = a         optarg = 100            optind = 2              argv[optind] = -b200
opt = b         optarg = 200            optind = 3              argv[optind] = -c300
opt = c         optarg = 300            optind = 4              argv[optind] = -d
opt = d         optarg = (null)         optind = 5              argv[optind] = (null)

选项a是可选参数,这里不带参数也是正确的

dzlab:~/test/test#./opt -a -b 200 -c 300 -d
opt = a         optarg = (null)         optind = 2              argv[optind] = -b
opt = b         optarg = 200            optind = 4              argv[optind] = -c
opt = c         optarg = 300            optind = 6              argv[optind] = -d
opt = d         optarg = (null)         optind = 7              argv[optind] = (null)

输入选项参数错误的情况

dzlab:~/test/test#./opt -a 100 -b 200 -c 300 -d
opt = a         optarg = (null)         optind = 2              argv[optind] = 100
opt = b         optarg = 200            optind = 5              argv[optind] = -c
opt = c         optarg = 300            optind = 7              argv[optind] = -d
opt = d         optarg = (null)         optind = 8              argv[optind] = (null)

导致解析错误,第一个 optarg = null,实际输入参数 100,由于格式不正确造成的(可选参数格式固定)

参数丢失,也会导致错误,c选项是必须有参数的,不加参数提示错误如下:

dzlab:~/test/test#./opt -a -b 200 -c
opt = a         optarg = (null)         optind = 2              argv[optind] = -b
opt = b         optarg = 200            optind = 4              argv[optind] = -c
./opt: optionrequires an argument -- 'c'
opt = ?         optarg = (null)         optind = 5              argv[optind] = (null)

这种情况,optstring 中第一个字母不是':',如果在 optstring 中第一个字母加':',则最后丢失参数的那个选项 opt 返回的是':',不是'?',并且没有提示错误信息,这里不再列出。

命令行选项未定义,-e选项未在optstring中定义,会报错:

dzlab:~/test/test#./opt -a -b 200 -e
opt = a         optarg = (null)         optind = 2              argv[optind] = -b
opt = b         optarg = 200             optind = 4              argv[optind] = -e
./opt: invalidoption -- 'e'
opt = ?         optarg = (null)         optind = 5              argv[optind] = (null)

命令行选项解析函数:getopt()相关推荐

  1. webbench源码学习--命令行选项解析函数getopt和getopt_long函数

    对于webbench这个网站压力测试工具网上介绍的很多,有深度详解剖析的,对于背景就不在提了, 听说最多可以模拟3万个并发连接去测试网站的负载能力,这里主要是学习了一下它的源码,做点 笔记. 官方介绍 ...

  2. 命令行选项解析函数(C语言):getopt()和getopt_long()

    上午在看源码项目 webbench 时,刚开始就被一个似乎挺陌生函数 getopt_long() 给卡住了,说实话这函数没怎么见过,自然不知道这哥们是干什么的.于是乎百度了一番,原来是处理命令行选项参 ...

  3. 命令行选项解析函数(C语言):getopt()和getopt_long()【转】

    (转自:https://www.cnblogs.com/chenliyang/p/6633739.html) 上午在看源码项目 webbench 时,刚开始就被一个似乎挺陌生函数 getopt_lon ...

  4. getopt:命令行选项、参数处理

    getopt:命令行选项.参数处理 2014-01-11 Posted by yeho 在写shell脚本时经常会用到命令行选项.参数处理方式,如: ./test.sh -f config.conf ...

  5. 《Python 黑帽子》学习笔记 - 命令行选项和参数处理 - Day 4

    在学习书中 netcat 代码的时候,发现其命令行选项和参数的处理存在一些小问题,由于调用 getopt 模块的 getopt() 函数时参数设置不当,会引起代码执行时获取不到参数值或引发异常.该问题 ...

  6. python 基础命令-详解python常用命令行选项与环境变量

    一.命令行选项 1.解释器选项 python的解释器非常像unix的shell,在我们使用python的过程中,我们可以指定很多的选项. 比如,最常用的选项如下: python script.py 在 ...

  7. python add argument list_python模块介绍- argparse:命令行选项及参数解析

    #承接软件自动化实施与培训等gtalk:ouyangchongwu#gmail.com qq 37391319 博客: http://blog.csdn.net/oychw #版权所有,转载刊登请来函 ...

  8. ccf命令行选项只能用c实现_CCF-201403-3-命令行选项

    问题描述 试题编号: 201403-3 试题名称: 命令行选项 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 请你写一个命令行分析程序,用以分析给定的命令行里包含哪些选项.每 ...

  9. 【Python】Python3.7.3 - sys.flag 命令行选项标志结构序列

    文章目录 Python命令行完整选项 sys.flag - Python启动命令行选项标志 Python命令行完整选项 https://blog.csdn.net/qq_43401808/articl ...

最新文章

  1. 二、Asp.Net Core WebAPI——OcelotDemo
  2. Java中的volatile关键字
  3. 梁建章的多米诺,混合办公的未知数
  4. 华为堡垒机_安恒信息成为“华为云优秀严选合作伙伴”,携手保障“云上”资产安全访问...
  5. android 如何动态设置margin,Android 动态设置margin
  6. 2.5D屏幕有什么好处?
  7. 拉格朗日(lagrange)插值及其MATLAB程序
  8. jqgrid 点击列头的超链接或按钮时,不触发列排序事件
  9. Poj(2312),坦克大战,BFS的变形
  10. 计算机装机配置单5000元,5000以内的diy装机配置清单推荐
  11. 武汉申报|2022年洪山区科技企业梯次培育专项资金申报指南
  12. 福建省厦门市谷歌卫星地图下载
  13. ambari mysql 开机自动启动_ambari的服务启动顺序如何设置
  14. 《老路用得上的商学课》56-60学习笔记
  15. win7 uefi 无法对计算机,U盘UEFI模式无法启动WIN7安装解决教程
  16. 并行是什么意思?与并发的区别是什么?
  17. flv转mp4选项设置
  18. MFC版 黄金矿工 游戏开发记录
  19. 设计基于HTML5的APP登录功能及安全调用接口的方式(原理篇)
  20. jenkins k8s 动态增减 jenkins-salve (1) 制作部署jenkins-master 镜像

热门文章

  1. [Java] grails 安装手记
  2. 为无LIB的DLL制作LIB函数符号输入库zz
  3. 矩阵微积分的一些实用结论与推导
  4. 中断描述符表IDT以及Linux内核IDT表的初始化的基本情况
  5. CASE软件Enterprise Architect简介和使用入门图解
  6. UEStudio使用入门
  7. (三)Redis for StackExchange.Redis
  8. ubuntu网卡问题
  9. BZOJ1003: [ZJOI2006]物流运输
  10. C# Excel 导入