getopt

#include<stdio.h>

#include<unistd.h>



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

{ 

  int ch; 

  opterr=0; 



  while((ch=getopt(argc,argv,"a:b:c:d:e:"))!=-1) 

  { 

    printf("\n\n\n"); 

    printf("optind:%d\n",optind); 

    printf("optarg:%s\n",optarg); 

    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:%c\n",ch); 

    } 

    printf("optopt+%c\n",optopt); 

  } 

}



// ./a.out -b 2222 -a 1111 -a 3333 -c 4444 -d 5555 -e 6666







/* optarg和optind是两个最重要的external变量。optarg是指向参数的指针(当然这只针对有参数的选项);

optind是argv[]数组的索引,众所周知,argv[0]是函数名称,所有参数从argv[1]开始,所以optind被初始化设置指为1。

每调用一次getopt()函数,返回一个选项,如果该选项有参数,则optarg指向该参数。                  

在命令行选项参数再也检查不到optstring中包含的选项时,返回-1。



optstring是getopt()中第3个参数,作为选项的字符串的列表。

函数getopt()认为optstring中,以'-’开头的字符(注意!不是字符串!!)就是命令行参数选项,有的参数选项后面可以跟参数值。

optstring中的格式规范如下: 

1) 单个字符,表示选项, 

2) 单个字符后接一个冒号”:”,表示该选项后必须跟一个参数值。参数紧跟在选项后或者以空格隔开。该参数的指针赋给optarg。 

3) 单个字符后跟两个冒号”:”,表示该选项后必须跟一个参数。                                                                        

参数必须紧跟在选项后不能以空格隔开。该参数的指针赋给optarg。

*/



int main1(int argc, char **argv)

{

    int result;

    opterr = 0;  //使getopt不行stderr输出错误信息

    while( (result = getopt(argc, argv, "ab:c::")) != -1 )

    {

           switch(result)

          {

               case 'a':

                   printf("option=a, optopt=%c, optarg=%s\n", optopt, optarg);

                   break;

              case 'b':   

                   printf("option=b, optopt=%c, optarg=%s\n", optopt, optarg);

                   break;

              case 'c':

                   printf("option=c, optopt=%c, optarg=%s\n", optopt, optarg);

                   break;

              case '?':

                    printf("result=?, optopt=%c, optarg=%s\n", optopt, optarg);

                    break;

              default:

                   printf("default, result=%c\n",result);

                   break;

           }

        printf("argv[%d]=%s\n", optind, argv[optind]);

    }

    printf("result=-1, optind=%d\n", optind);   //看看最后optind的位置

    for(result = optind; result < argc; result++)

         printf("-----argv[%d]=%s\n", result, argv[result]);

 //看看最后的命令行参数,看顺序是否改变了哈。

    for(result = 1; result < argc; result++)

          printf("\nat the end-----argv[%d]=%s\n", result, argv[result]);

    return 0;

}


#include<stdio.h>
#include<unistd.h>
int main(int argc,char **argv)
{
int ch;
opterr = 0;
while((ch = getopt(argc,argv,"a:b::cde"))!= -1)
switch(ch)
{
case 'a': printf("option a:'%s'\n",optarg); break;
case 'b': printf("option b:'%s'\n",optarg); break;
default: printf("other option :%c\n",ch);
}
printf("optopt +%c\n",optopt);
}

 
   

 

 

 

你可能感兴趣的:(get)