程序會根據讀取的參數執行相應的操作,在C語言中,這個功能一般是靠getopt()這個函數,結合switch語句來完成的,首先來看下面的代碼:
#include <unistd.h>
int main(int argc,char *argv[])
{
int ch;
opterr=0;
while((ch=getopt(argc,argv,"a:b::cde"))!=-1)
{
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\n");
break;
case 'd':
printf("option d\n");
break;
case 'e':
printf("option e\n");
break;
default:
printf("other option:%c\n",ch);
}
printf("optopt+%c\n",optopt);
}
}
用gcc編譯後,在終端行執行以上的命令:
則會有如下的輸出:
optarg:1234
ch:a
option a:'1234'
optopt+
optind:3
optarg:432
ch:b
option b:'432'
optopt+
optind:4
optarg:(null)
ch:c
option c
optopt+
optind:5
optarg:(null)
ch:d
option d
optopt+
main(int argc,char *argv[])中的argc是一個整型,argv是一個指針數組,argc記錄argv的大小。上面的例子中。
argc=5;
argv[0]=./a.out
argv[1]=-a1234
argv[2]=-b432
argv[3]=-c
argv[4]=-d
getopt()函數的原型爲getopt(int argc,char *const argv[],const char *optstring)。
其中argc和argv一般就將main函數的那兩個參數原樣傳入。
optstring是一段自己規定的選項串,例如本例中的"a:b::cde",表示可以有,-a,-b,-c,-d,-e這幾個參數。
“:”表示必須該選項帶有額外的參數,全域變量optarg會指向此額外參數,“::”標識該額外的參數可選(有些Uinx可能不支持“::”)。
全域變量optind指示下一個要讀取的參數在argv中的位置。
如果getopt()找不到符合的參數則會印出錯信息,並將全域變量optopt設爲“?”字符。
如果不希望getopt()印出錯信息,則只要將全域變量opterr設爲0即可。
變量函數原型:
extern char *optarg;
extern int optind, // 初始化值爲1,下一次調用getopt時,從optind存儲的位置重新開始檢查選項。
extern int opterr, // 初始化值爲1,當opterr=0時,getopt不向stderr輸出錯誤信息。
extern int optopt; // 當命令行選項字符不包括在optstring中或者選項缺少必要的參數時,
// 該選項存儲在optopt中, getopt返回'?’。
int getopt(int argc, char * const argv[], const char *optstring);
optarg和optind是兩個最重要的external
變量。optarg是指向參數的指針(當然這隻針對有參數的選項);optind是argv[]數組的索引,衆所周知,argv[0]是函數名稱,所有參數從argv[1]開始,所以optind被初始化設置指爲1。 每調用一次getopt()函數,返回一個選項,如果該選項有參數,則optarg指向該參數。 在命令行選項參數再也檢查不到optstring中包含的選項時,返回-1
1) 單個字符,表示選項,
2) 單個字符後接一個冒號”:”,表示該選項後必須跟一個參數值。參數緊跟在選項後或者以空格隔開。該參數的指針賦給optarg。
3) 單個字符後跟兩個冒號”::”,表示該選項後必須跟一個參數。參數必須緊跟在選項後不能以空格隔開。該參數的指針賦給optarg。(這個特性是GNU的擴張)。
在這個命令行參數中,-a和-h就是選項元素,去掉'-',a,b,c就是選項。host是b的參數,keke是c的參數。但haha並不是d的參數,因爲它們中間有空格隔開。所以上面的命令行調用會出錯。
如果optstring中的字符串以'+'加號開頭或者環境變量POSIXLY_CORRE被設置。那麼一遇到不包含選項的命令行參數,getopt就會停止,返回-1。