歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux技術 >> 函數getopt(),及其參數optind

函數getopt(),及其參數optind

日期:2017/3/3 11:21:49   编辑:Linux技術

http://blog.sina.com.cn/s/blog_64ba2b750100vz5b.html

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

Copyright © Linux教程網 All Rights Reserved