1. 程式人生 > >getopt函式學習

getopt函式學習

getopt()用來分析命令列引數。

標頭檔案 #include<unistd.h>
定義函式 int getopt(int argc,char * const argv[ ],const char * optstring);
extern char *optarg;
extern int optind, opterr, optopt;
optind是下一次進行選項搜尋的開始索引,下一次從argv[optind]開始索引。
opterr

先分析一段程式碼:

1 #include <stdio.h>
  2 #include "apue.h"
  3 
  4 int main(int argc,char **argv)
  5 {
  6     int oc;
  7     while((oc = getopt(argc,argv,":n::gl:"))!=-1)
  8     {
  9         switch(oc)
 10         {
 11             case 'n':
 12                 printf("option n\n");
 13                 if(optarg)
 14                 {
 15                     printf("%s\n",optarg);
 16                 }
 17                 else
 18                 {                                                                                                                                 
 19                     printf("no argument\n");
 20                 }
 21                 break;
 22             case 'g':
 23                 printf("option g\n");
 24                 break;
 25             case 'l':
 26                 printf("option l\n");
 27                 printf("%s\n",optarg);
 28                 break;
 29             case '?':
 30                 putchar(oc);
 31                 printf("無效選項\n");
 32                 break;
 33             case ':':
 34                 putchar(oc);
 35                 printf("缺少選項的引數\n");
 36                 break;
 37         }
 38     }
 39     return 0;
 40 }

執行結果:

./a.out -g
option g


./a.out -h
?無效選項


./a.out -l
:缺少選項的引數


./a.out -l hello
option l
hello


./a.out -lhello
option l
hello


./a.out -n
option n
no argument


./a.out -n hello
option n
no argument

./a.out -nhello
option n
hello


分析:

ngl是選項,如果是無效選項,返回‘?’,如果缺少選項的引數,返回‘:’(如果ngl前面的那個冒號去掉的話,缺少選項引數也返回‘?’)。

::表示引數可有可無,從執行結果看,-nhello,引數和選項之間沒有空格才能解析出來。

  1 #include <stdio.h>                                                                                                                                
  2 #include "apue.h"
  3 
  4 int main(int argc,char **argv)
  5 {
  6     int oc;
  7     printf("optind:%d\n",optind);
  8     while((oc = getopt(argc,argv,":n::gl:"))!=-1)
  9     {   
 10         printf("optind:%d\n",optind);
 11         switch(oc)
 12         {   
 13             case 'n':
 14                 printf("option n\n");
 15                 if(optarg)
 16                 {   
 17                     printf("%s\n",optarg);
 18                 }
 19                 else
 20                 {   
 21                     printf("no argument\n");
 22                 }
 23                 break;
 24             case 'g':
 25                 printf("option g\n");
 26                 break;
 27             case 'l':
 28                 printf("option l\n");
 29                 printf("%s\n",optarg);
 30                 break;
 31             case '?':
 32                 putchar(oc);
 33                 printf("無效選項\n");
 34                 break;
 35             case ':':
 36                 putchar(oc);
 37                 printf("缺少選項的引數\n");
 38                 break;
 39         }
 41     printf("optind:%d\n",optind);
 42     return 0;
 43 }   
 44  

執行:

./a.out -l 1 2 3 4
optind:1
optind:3
option l
1
optind:3

可以看出:optind會指向下一個開始掃描的位置,開始的時候的等於1,也就是從-l開始掃描,-l是有引數的,所以下一次從3開始掃描,因為沒有掃描到有效選項,返回-1,跳出迴圈之後optind仍為3。