linux getopt函数详细分析

标准库函数:getopt  

头文件 #include <unistd.h>

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

解释:take the argc and argv as passed to main function (argc和argv参数与int main(int argc,char * argv[])相同)

and an options specifier string that tells

getopt函数 what options are defined for the program and whether they have associated values.

the optstring is simply a list of characters,each representing a single character option.

if a character is followed by a colon(冒号),it indicatess that the option has an associated value that

will be taken as the next argument.

举例:getopt(argc,argv,"if:lr");

it allows for simple options -i,-l,-r    and -f(后紧跟一个filename参数)

假设执行程序名为test

./test -lrf:i

则argc=2  argv[0]="./test"  argv[1]="-lrf:i"  (已验证)

 

#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <fcntl.h>
int main(int argc,char*argv[])

{
int opt;
while((opt=getopt(argc,argv,"if:lr")) != -1)
{

switch(opt)
{
case 'i':
              printf("i: %d\n",optind);
              printf("option: %c\n",opt);
              break;
case 'l':
               printf("l: %d\n",optind);
               printf("option: %c\n",opt);
               break;
case 'r':
               printf("r: %d\n",optind);
               printf("option: %c\n",opt);
               break;
case 'f':
                printf("f: %d\n",optind);
                printf("filename: %s\n",optarg);
                break;

}

}

printf("final optind: %d",optind);

for(;optind<argc;++optind)
 printf("argument: %s\n",argv[optind]);

exit(0);

}

假设执行程序名为aaa

./aaa -i -lr 'hello world' -f fred.c
输出如下:

i: 2
option: i
l: 2
option: l
r: 3
option: r
f: 6
filename: fred.c

5

argument: hello world
分析:

man 3 getopt 获取帮助

The variable optind is the index of the next element to be processed in
       argv.  The system initializes this value to 1.  The caller can reset it
       to 1 to restart scanning of the same argv, or when scanning a new argu‐
       ment vector.

If  getopt() finds another option character, it returns that character,
       updating the external variable optind and a static variable nextchar so
       that  the  next call to getopt() can resume the scan with the following
       option character or argv-element.

 If there are no more option  characters,  getopt()  returns  -1.   Then
       optind  is  the  index in argv of the first argv-element that is not an
       option.

意思是:变量optind(初始值为1)是argv的索引,代表下个元素(待处理)。

如果getopt找到可选项字符,则getopt返回这个字符并更新(注:不一定add)

optind。

如果不再有可选项字符,则getopt返回-1。

optind就是第一个argv元素(不是可选项)的索引(这句话很重要 说明最后optind会

被修改)。

重要:By default, getopt() permutes(排列) the contents of argv as it scans, so that
       eventually all the nonoptions are at the end.

分析上面的例子:

argv指向{"./aaa","-i","-lr","hello world","-f","fred.c"}

i=2 意思是下一个待处理的元素在argv中的索引是2

l=2意思同i,因为l的下一个待处理元素是r,而r的索引是2

r=3 即r之后的元素在argv的索引是3

f=6 因为f后跟的fred.c的索引是5,fred.c是f的参数,所以f=6

由于f之后不再有可选项,所以getopt返回-1,而optind则是hello world在argv中的索引(注:

非可选项已被调整到可选项后面 所以其索引为5 即 ./aaa -i -lr -f fred.c 'hello world')


 

 

 

 

你可能感兴趣的:(linux getopt函数详细分析)