一、查询linux命令手册:

  1. #include<unistd.h>
  2. #include<getopt.h> /*所在头文件 */
  3. int getopt(intargc, char * const argv[], const char *optstring);
  4. int getopt_long(int argc, char * const argv[], const char *optstring,
  5. const struct option *longopts, int*longindex);
  6. int getopt_long_only(int argc, char * const argv[],const char *optstring,
  7. const struct option *longopts, int*longindex);
  8. extern char *optarg; /*系统声明的全局变量 */
  9. extern int optind, opterr, optopt;

先拿最简单的 getopt 函数开刀,getopt_long 只是前者的增强版,功能多点而已。

二、getopt函数

1、定义

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

2、描述

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

3、参数

  1. argcmain()函数传递过来的参数的个数
  2. argvmain()函数传递过来的参数的字符串指针数组
  3. optstring:选项字符串,告知 getopt()可以处理哪个选项以及哪个选项需要参数

4、返回

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

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

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

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

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

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

6、实例:

  1. #include<stdio.h>
  2. #include<unistd.h>
  3. #include<getopt.h>
  4. int main(intargc, char *argv[])
  5. {
  6. int opt;
  7. char *string = "a::b:c:d";
  8. while ((opt = getopt(argc, argv, string))!= -1)
  9. {
  10. printf("opt = %c\t\t", opt);
  11. printf("optarg = %s\t\t",optarg);
  12. printf("optind = %d\t\t",optind);
  13. printf("argv[optind] = %s\n",argv[optind]);
  14. }
  15. }

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

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

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

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

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

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

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

输入选项参数错误的情况

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

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

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

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

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

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

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

到这里应该已经把getopt函数的功能讲解清楚了吧,下边来说说 getopt_long 函数,getopt_long 函数包含了 getopt 函数的功能,并且还可以指定"长参数"(或者说长选项),与 getopt 函数对比,getopt_long 比其多了两个参数:

三、getopt_long函数

1、定义

  1. int getopt_long(int argc, char * const argv[], const char *optstring,
  2.  
  3. const struct option *longopts,int *longindex);

2、描述

  1. 包含 getopt 功能,增加了解析长选项的功能如:--prefix --help

3、参数

  1. longopts 指明了长参数的名称和属性
  2. longindex 如果longindex非空,它指向的变量将记录当前找到参数符合longopts里的第几个元素的描述,即是longopts的下标值

4、返回

  1. 对于短选项,返回值同getopt函数;对于长选项,如果flagNULL,返回val,否则返回0;对于错误情况返回值同getopt函数
  1. 5struct option
  1. struct option {
  2. const char *name; /* 参数名称 */
  3. int has_arg; /* 指明是否带有参数 */
  4. int *flag; /* flag=NULL时,返回value;不为空时,*flag=val,返回0 */
  5. int val; /* 用于指定函数找到选项的返回值或flag非空时指定*flag的值 */
  6. }; 

6、参数说明:

  1. has_arg 指明是否带参数值,其数值可选:
  2. no_argument 表明长选项不带参数,如:--name, --help
  3. required_argument 表明长选项必须带参数,如:--prefix /root --prefix=/root
  4. optional_argument 表明长选项的参数是可选的,如:--help prefix=/root,其它都是错误

接着看一下实例操作会更加深刻地理解:

7、实例:

  1. int main(intargc, char *argv[])
  2. {
  3. int opt;
  4. int digit_optind = 0;
  5. int option_index = 0;
  6. char *string = "a::b:c:d";
  7. static struct option long_options[] =
  8. {
  9. {"reqarg", required_argument,NULL, 'r'},
  10. {"optarg", optional_argument,NULL, 'o'},
  11. {"noarg", no_argument, NULL,'n'},
  12. {NULL, 0, NULL, 0},
  13. };
  14. while((opt =getopt_long_only(argc,argv,string,long_options,&option_index))!= -1)
  15. {
  16. printf("opt = %c\t\t", opt);
  17. printf("optarg = %s\t\t",optarg);
  18. printf("optind = %d\t\t",optind);
  19. printf("argv[optind] =%s\t\t", argv[optind]);
  20. printf("option_index = %d\n",option_index);
  21. }
  22. }

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

正确输入长选项的情况

  1.  
  1. dzlab:~/test/test#./long --reqarg 100 --optarg=200 --noarg
  2. opt = r optarg =100 optind = 3 argv[optind] = --optarg=200 option_index = 0
  3. opt = o optarg =200 optind = 4 argv[optind] = --noarg option_index = 1
  4. opt = n optarg =(null) optind = 5 argv[optind] =(null) option_index = 2

或者这种方式:

  1. dzlab:~/test/test#./long –reqarg=100 --optarg=200 --noarg
  2. opt = r optarg =100 optind = 2 argv[optind] = --optarg=200 option_index = 0
  3. opt = o optarg =200 optind = 3 argv[optind] = --noarg option_index = 1
  4. opt = n optarg =(null) optind = 4 argv[optind] =(null) option_index = 2

可选选项可以不给参数

  1. dzlab:~/test/test#./long --reqarg 100 --optarg --noarg
  2. opt = r optarg =100 optind = 3 argv[optind] = --optarg option_index = 0
  3. opt = o optarg =(null) optind = 4 argv[optind] =--noarg option_index = 1
  4. opt = n optarg =(null) optind = 5 argv[optind] =(null) option_index = 2

输入长选项错误的情况

  1. dzlab:~/test/test#./long --reqarg 100 --optarg 200 --noarg
  2. opt = r optarg =100 optind = 3 argv[optind] = --optarg option_index= 0
  3. opt = o optarg =(null) optind = 4 argv[optind] =200 option_index = 1
  4. opt = n optarg =(null) optind = 6 argv[optind] =(null) option_index = 2

这时,虽然没有报错,但是第二项中 optarg 参数没有正确解析出来(格式应该是 —optarg=200)

必须指定参数的选项,如果不给参数,同样解析错误如下:

  1. dzlab:~/test/test#./long --reqarg --optarg=200 --noarg
  2. opt = r optarg =--optarg=200 optind = 3 argv[optind] =--noarg option_index = 0
  3. opt = n optarg =(null) optind = 4 argv[optind] =(null) option_index = 2

长选项的举例说明暂且就这么多吧,其它如选项错误、缺参数、格式不正确的情况自己再试验一下。

四、getopt_long_only函数

getopt_long_only 函数与 getopt_long 函数使用相同的参数表,在功能上基本一致,只是 getopt_long 只将 --name 当作长参数,但 getopt_long_only 会将 --name 和 -name 两种选项都当作长参数来匹配。getopt_long_only 如果选项 -name 不能在 longopts 中匹配,但能匹配一个短选项,它就会解析为短选项。

转自 https://www.cnblogs.com/chenliyang/p/6633739.html

getopt函数使用说明的更多相关文章

  1. getopt函数的使用——分析命令行参数

    getopt(分析命令行参数) getopt(分析命令行参数) 短参数的定义 返回值 范例 getopt_long 相关函数表头文件#include<unistd.h> 函数声明int g ...

  2. Linux c 下使用getopt()函数

    命令行参数解析函数 —— getopt() getopt()函数声明如下: #include <unistd.h> int getopt(int argc, char * const ar ...

  3. Linux下getopt()函数的简单使用

    最近在弄Linux C编程,本科的时候没好好学啊,希望学弟学妹们引以为鉴. 好了,虽然啰嗦了点,但确实是忠告.步入正题: 我们的主角----getopt()函数. 英雄不问出处,getopt()函数的 ...

  4. Firebird/InterBase内置函数使用说明

    Firebird/InterBase内置函数使用说明(转自:圣域天堂) 2008-10-12 20:56 加*号为FB2.0加入的函数 整理:剑雷(jianlei) 2006-10-13 1. COU ...

  5. C语言-getopt函数

    #include<unistd.h> int getopt(int argc,char *const argv[],const char *optstring); extern char ...

  6. 如何使用getopt()函数解析参数

    最近在写程序的过程中,把一部分时间都花费在程序对参数的处理上.今天听了学长说到getopt函数,才发现原来c里面还有一个专门解决参数处理的函数,查询了相关资料,这里简单总结一下. 使用int main ...

  7. liunx 系统调用 getopt() 函数

    命令行参数解析函数 -- getopt() getopt()函数声明如下: #include <unistd.h>int getopt(int argc, char * const arg ...

  8. Python中getopt()函数的使用

    在运行程序时,可能需要根据不同的条件,输入不同的命令行选项来实现不同的功能.目前有短选项和长选项两种格式.短选项格式为"-"加上单个字母选项:长选项为"--"加 ...

  9. [转载]Linux下getopt()函数的简单使用

    转载源地址:https://www.cnblogs.com/qingergege/p/5914218.html 1.getopt()函数的出处就是unistd.h头文件(哈哈),写代码的时候千万不要忘 ...

  10. Linux getopt()函数 getopt_long()函数---转

    http://hi.baidu.com/scoundrelgg/item/d4083f8412eea05d26ebd97f Linux getopt()函数 getopt_long()函数 get_o ...

随机推荐

  1. Linux出现Read-only file system错误解决方法

    执行命令时遇到如下错误 这个问题是文件系统受损导致得,fstab文件未正确配置 解决方法: df -hT #查看一下分区及挂载信息 fsck -a /dev/sda3 -a :检查文件系统,有异常便自 ...

  2. Centos 7.5 MySql的安装和配置

    一.安装 三个步骤: wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpmyum -y ins ...

  3. 理解函数调用_使用严格模式边使用arguments别名

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. VAE变分自编码器公式推导

    VAE变分推导依赖数学公式 (1)贝叶斯公式:\(p(z|x) = \frac{p(x|z)p(z)}{p(x)}\) (2)边缘概率公式:\(p(x) =\int{p(x,z)}dz\) (3)KL ...

  5. Java数组之什么是数组?+ 数组的声明和创建

    数组 数组是相同类型数据的有序集合. 数组描述的是相同类型的若干个数据,按照一定的先后次序排序组合而成. 其中,每一个数据称作一个数组元素,每个数组元素可以通过一个下标来访问它们. 数组声明创建 首先 ...

  6. Oracle 临时表空间暴满的原因与解决方法

    Oracle临时表空间主要用来做查询和存放一些缓冲区数据.临时表空间消耗的主要原因是需要对查询的中间结果进行排序. 重启数据库可以释放临时表空间,如果不能重启实例,而一直保持问题sql语句的执行,te ...

  7. NIO 缓冲区 ByteBuffer 之黏包和半包

    一.低效率方式 /** * 黏包.半包 */ private static void buffExample2() { /* 网络上传输多条数据给服务器,数据之间使用 \n 分隔. 但由于某种原因(多 ...

  8. C/C++:printf 函数格式化输出,用法详细记录

    printf ( print format )函数是接触C/C++之后接触的第一个函数,它的功能除了输出hello world外,更重要的是进行格式化输出,比如输出整数的%d,输出小数的%f,%lf ...

  9. ESP-Example ble-ancs解析

    苹果通知中心服务(ANCS)的目的是让蓝牙配件(通过蓝牙低能耗链接连接到iOS设备)以简单方便的方式访问iOS设备上产生的各种通知. Apple通知中心服务是主服务,其UUID为7905F431-B5 ...

  10. mysql剪贴板

    // mysql 8.0 连接数据源的配置文件 <!--数据源--><bean id="dataSource" class="com.alibaba.d ...