getopt()函数用法

来源:互联网 发布:linux 时间比较 编辑:程序博客网 时间:2024/04/29 12:01

getopt() 函数原型:getopt() 函数,以及配合其使用的全局变量

#include <unistd.h>

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

extern char *optarg; //选项的参数指针

extern int optind;        //下一次调用getopt的时,从optind存储的位置处重新开始检查选项

extern int opterr;      //当 opterr= 0 时,getopt 不向 stderr 输出错误信息

extern int optopt;      //当命令行选项字符不包括在optstring 中或者选项缺少必要的参数时,该选项存储在optopt中,getopt返回'?’

getopt()函数的三个参数:argc,argv 即为 main()程序运行时的参数;optsring是需要匹配的选项。

该调用一次,返回一个选项,在命令行选项参数再也检查不到optstring 中包含的选项时,返回-1,同时 optind 储存第一个不包含选项的命令行参数的位置(注意这一点)。

字符串optstring可以下列元素:
1.单个字符,表示选项;
2.单个字符后接一个冒号 ‘ :’ ,表示该选项后面的是要接收的参数,参数紧跟在选项后,或者以空格隔开。该参数的指针赋给optarg。
3 单个字符后跟两个冒号 ‘ :: ’ ,表示该选项后可以接收参数,但参数必须紧跟在选项后面,如果是空格隔开的就不是它的参数。该参数的指针赋给optarg。

getopt()重新排列main()参数:
默认情况下getopt会重新排列main()参数的顺序,把所有不包含选项(没有‘ - ’)的参数都排到最后,以及没有作为选项参数的main()参数排到后面。

例子:main.c

#include <unistd.h>#include <stdio.h>int main(int argc,char **argv){    printf("optind = %d \n",optind);    printf("opterr = %d \n",opterr);//打印optind,opterr的默认值    opterr = 1;//控制getopt()是否向标准错误输出错误信息    printf("argc = %d \n",argc);//下面显示main()的参数    int i;    for(i = 0; i < argc; ++i)    {        printf("argv[%d] = %s \n",i,argv[i]);    }    printf("*********************************\n");    int ch;    while((ch = getopt(argc,argv,"a:b::cde")) != -1)    {printf("ch = %c \n",ch);        switch(ch)        {        case 'a':            printf("option a : '%s' \n",optarg);            break;        case 'b':            printf("option b : '%s' \n",optarg);            break;        case 'c':            printf("option c : '%s' \n",optarg);            break;        case 'd':            printf("option d : '%s' \n",optarg);            break;        case 'e':            printf("option e : '%s' \n",optarg);            break;        default:            printf("other option : optopt = %c \n",optopt);            break;        }printf("optind = %d \n\n",optind,ch);    }    printf("*********************************\n");    printf("optind = %d \n",optind);    for(i = 0; i < argc; ++i)    {        printf("argv[%d] = %s \n",i,argv[i]);    }    return 0;}
getopt()函数第三个参数optstring 是:“ a: b::cde ”,意思是main()的参数里面,选项 a 后面的是它的参数,可以是紧挨着a,也可以这个空格;选项 b后面可能有参数,有的话参数必须是紧挨着b的;cde后面没有参数;

假如输入为:~$ ./main -a 100 -b 200 -c -d300 -e linux

输出为:

optind= 1 , opterr= 1 , argc= 9

***************************

ch= a ,option a : '100' ,optind = 3

ch= b ,option b : '(null)' ,optind = 4

ch= c ,option c : '(null)' ,optind = 6

ch= d ,option d : '(null)' ,optind = 6

./main:invalid option -- '3'

ch= ? ,other option : optopt = 3 ,optind = 6

./main:invalid option -- '0'

ch= ? ,other option : optopt = 0 ,optind = 6

./main:invalid option -- '0'

ch= ? ,other option : optopt = 0 ,optind = 7

ch= e ,option e : '(null)' ,optind = 8

*********************************

optind= 7

argv[0]= ./main

argv[1]= -a

argv[2]= 100

argv[3]= -b

argv[4]= -c

argv[5]= -d300

argv[6]= -e

argv[7]= 200

argv[8]= linux


由该程序可以对getopt()函数有一个比较全面的了解。

原创粉丝点击