函數getopt(),及其參數optind


getopt被用來解析命令行選項參數。

#include <unistd.h>
       extern char *optarg;  //選項的參數指針
       extern int optind,   //下一次調用getopt的時,從optind存儲的位置處重新開始檢查選項。 
       extern int opterr,  //當opterr=0時,getopt不向stderr輸出錯誤信息。
       extern int optopt;  //當命令行選項字符不包括在optstring中或者選項缺少必要的參數時,該選項存儲在optopt 中,getopt返回'?’、

       int getopt(int argc, char * const argv[], const char *optstring);
 調用一次,返回一個選項。 在命令行選項參數再也檢查不到optstring中包含的選項時,返回-1,同時optind儲存第一個不包含選項的命令行參數。

首先說一下什么是選項,什么是參數。

1.單個字符,表示選項,

2.單個字符后接一個冒號:表示該選項后必須跟一個參數。參數緊跟在選項后或者以空格隔開。該參數的指針賦給optarg。
3 單個字符后跟兩個冒號,表示該選項后必須跟一個參數。參數必須緊跟在選項后不能以空格隔開。該參數的指針賦給optarg。(這個特性是GNU的擴張)。

例如gcc -g -o test test.c ,其中g和o表示選項,test為選項o的參數。

上面是getopt()函數的基本含義,大家懂得了這些之后,我們一個例子加深一下理解。

例如我們這樣調用getopt(argc, argv, "ab:c:de::");
從上面我們可以知道,選項a,d沒有參數,選項b,c有一個參數,選項e有有一個參數且必須緊跟在選項后不能以空格隔開。getopt首先掃描argv[1]到argv[argc-1],並將選項及參數依次放到argv數組的最左邊,非選項參數依次放到argv的最后邊。

執行程序為:
      0      1    2    3  4   5      6   7    8   9 
$ ./test file1 -a  -b -c code -d file2 -e file3
  掃描過程中,optind是下一個選項的索引, 非選項參數將跳過,同時optind增1。optind初始值為1。當掃描argv[1]時,為非選項參數,跳過,optind=2;掃描到-a選項時, 下一個將要掃描的選項是-b,則optind更改為3;掃描到-b選項時,后面有參數(會認為-c為選項b的參數),optind=5,掃描到code非選項跳過optind=6;掃描到-d選項,后面沒有參數,optind=7;掃描到file2非選項跳過optind=8;掃描到-e后面本來應該有參數,optind=9但是有空格所以e的參數為空。
 
掃描結束后,getopt會將argv數組修改成下面的形式
       0     1   2   3   4    5   6        7      8      9
$ ./test  -a  -b  -c  -d  -e  file1  code  file2  file3
 
同時,optind會指向非選項的第一個參數,如上面,optind將指向file1
代碼如下:
#include <unistd.h>
#include <stdio.h>
int main(int argc, char * argv[])
{
    int aflag=0, bflag=0, cflag=0;
    int ch;
printf("optind:%d,opterr:%dn",optind,opterr);
printf("--------------------------n");
    while ((ch = getopt(argc, argv, "ab:c:de::")) != -1)
    {
        printf("optind: %d,argc:%d,argv[%d]:%sn", optind,argc,optind,argv[optind]);
        switch (ch) {
        case 'a':
            printf("HAVE option: -ann");
    
            break;
        case 'b':
            printf("HAVE option: -bn");
         
            printf("The argument of -b is %snn", optarg);
            break;
        case 'c':
            printf("HAVE option: -cn");
            printf("The argument of -c is %snn", optarg);

            break;
    case 'd':
        printf("HAVE option: -dn");
        break;
    case 'e':
        printf("HAVE option: -en");
        printf("The argument of -e is %snn", optarg);
        break;

        case '?':
            printf("Unknown option: %cn",(char)optopt);
            break;
        }
    }
    printf("----------------------------n");
    printf("optind=%d,argv[%d]=%sn",optind,optind,argv[optind]);
}
執行結果:
shiqi@wjl-desktop:~/code$ vim getopt.c
shiqi@wjl-desktop:~/code$ gcc getopt.c -o g
shiqi@wjl-desktop:~/code$ ./g file1 -a  -b -c code -d file2 -e file3
optind:1,opterr:1
--------------------------
optind: 3,argc:10,argv[3]:-b
HAVE option: -a

optind: 5,argc:10,argv[5]:code
HAVE option: -b
The argument of -b is -c

optind: 7,argc:10,argv[7]:file2
HAVE option: -d

optind: 9,argc:10,argv[9]:file3
HAVE option: -e
The argument of -e is (null)   

----------------------------
optind=6,argv[6]=file1         //while循環執行完后,optind=6


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM