函數getopt(),及其參數optind

來源:互聯網
上載者:User

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:%d\n",optind,opterr);
printf("--------------------------\n");
    while ((ch = getopt(argc, argv, "ab:c:de::")) != -1)
    {
        printf("optind: %d,argc:%d,argv[%d]:%s\n", optind,argc,optind,argv[optind]);
        switch (ch) {
        case 'a':
            printf("HAVE option: -a\n\n");
    
            break;
        case 'b':
            printf("HAVE option: -b\n");
         
            printf("The argument of -b is %s\n\n", optarg);
            break;
        case 'c':
            printf("HAVE option: -c\n");
            printf("The argument of -c is %s\n\n", optarg);

            break;
    case 'd':
        printf("HAVE option: -d\n");
        break;
    case 'e':
        printf("HAVE option: -e\n");
        printf("The argument of -e is %s\n\n", optarg);
        break;

        case '?':
            printf("Unknown option: %c\n",(char)optopt);
            break;
        }
    }
    printf("----------------------------\n");
    printf("optind=%d,argv[%d]=%s\n",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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.