getopt被用來解析命令行選項參數。html
#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儲存第一個不包含選項的命令行參數。shell
首先說一下什麼是選項,什麼是參數。數組
字符串optstring能夠下列元素,函數
單個字符,表示選項,工具
單個字符後接一個冒號:表示該選項後必須跟一個參數。參數緊跟在選項後或者以空格隔開。該參數的指針賦給optarg。測試
單個字符後跟兩個冒號,表示該選項後必須跟一個參數。參數必須緊跟在選項後不能以空格隔開。該參數的指針賦給optarg。(這個特性是GNU的擴張)。ui
getopt處理以'-’開頭的命令行參數,如spa
optstring="ab:c::d::",命令行爲getopt.exe -a -b host -ckeke -d haha
在這個命令行參數中,-a和-h就是選項元素,去掉'-',a,b,c就是選項。host是b的參數,keke是c的參數。但haha並非d的參數,由於它們中間有空格隔開。命令行
還要注意的是默認狀況下getopt會從新排列命令行參數的順序,因此到最後全部不包含選項的命令行參數都排到最後。設計
如 getopt.exe -a ima -b host -ckeke -d haha, 都最後命令行參數的順序是: -a -b host -ckeke -d ima haha
若是optstring中的字符串以'+'加號開頭或者環境變量POSIXLY_CORRE被設置。那麼一遇到不包含選項的命令行參數,getopt就會中止,返回-1。
unistd.h中的全局變量示例以下
#include <stdio.h> #include <stdlib.h> #include <unistd.h> int main(int argc, char **argv) { int result; opterr = 0; //使getopt不行stderr輸出錯誤信息 while( (result = getopt(argc, argv, "ab:c::")) != -1 ) { switch(result) { case 'a': printf("option=a, optopt=%c, optarg=%s\n", optopt, optarg); break; case 'b': printf("option=b, optopt=%c, optarg=%s\n", optopt, optarg); break; case 'c': printf("option=c, optopt=%c, optarg=%s\n", optopt, optarg); break; case '?': printf("result=?, optopt=%c, optarg=%s\n", optopt, optarg); break; default: printf("default, result=%c\n",result); break; } printf("argv[%d]=%s\n", optind, argv[optind]); } printf("result=-1, optind=%d\n", optind); //看看最後optind的位置 for(result = optind; result < argc; result++) printf("-----argv[%d]=%s\n", result, argv[result]); //看看最後的命令行參數,看順序是否改變了哈。 for(result = 1; result < argc; result++) printf("\nat the end-----argv[%d]=%s\n", result, argv[result]); return 0; } unistd裏有個 optind 變量,每次getopt後,這個索引指向argv裏當前分析的字符串的下一個索引,所以 argv[optind]就能獲得下個字符串,經過判斷是否以 '-'開頭就可。下面是個測試程序 #include <stdio.h> #include <unistd.h> int main(int argc, char* argv[]) { int tmp = 4; while( (tmp = getopt(argc, argv, "abck")) != -1 ) { printf("-%c\t", tmp); int opt = optind ; while( opt < argc ) { if ( argv[opt][0] != '-' ) { printf("%s\t", argv[opt]); opt ++; } else break; } printf("\n"); } getchar(); } 函數說明 getopt()用來分析命令行參數。參數argc和argv是由main()傳遞的參數個數和內容。參數optstring 則表明欲處理的選項字符串。此函數會返回在argv 中下一個的選項字母,此字母會對應參數optstring 中的字母。若是選項字符串裏的字母后接着冒號「:」,則表示還有相關的參數,全域變量optarg 即會指向此額外參數。若是getopt()找不到符合的參數則會印出錯信息,並將全域變量getopt設爲「?」字符,若是不但願getopt()印出錯信息,則只要將全域變量opterr設爲0便可。 返回值 若是找到符合的參數則返回此參數字母,若是參數不包含在參數optstring 的選項字母則返回「?」字符,分析結束則返回-1。
#include<stdio.h> #include<unistd.h> int main(int argc,char **argv) { int ch; opterr = 0; while((ch = getopt(argc,argv,」a:bcde」))!= -1) switch(ch) { case ‘a’: printf(「option a:’%s’\n」,optarg); break; case ‘b’: printf(「option b :b\n」); break; default: printf(「other option :%c\n」,ch); } printf(「optopt +%c\n」,optopt); }
執行 $./getopt –b option b:b $./getopt –c other option:c $./getopt –a other option :? $./getopt –a12345 option a:’12345’
在 20 世紀 90 年代(若是沒有記錯的話),UNIX 應用程序開始支持長選項,即一對短橫線(而不是普通短 選項所使用的單個短橫線)、一個描述性選項名稱還能夠包含一個使用等號鏈接到選項的參數。幸運的是,能夠經過使用 getopt_long() 向程序添加長選項支持。您可能已經猜到了,getopt_long() 是同時支持長選項和短選項的getopt() 版本。getopt_long() 函數還接受其餘參數,其中一個是指向 struct option 對象數組的指針。此結構至關直接,代碼以下。
struct option { char *name; //name表示的是長參數名 int has_arg; //has_arg有3個值,no_argument(或者是0),表示該參數後面不跟參數值 // required_argument(或者是1),表示該參數後面必定要跟個參數值 // optional_argument(或者是2),表示該參數後面能夠跟,也能夠不跟參數值 int *flag; //用來決定,getopt_long()的返回值究竟是什麼。若是flag是null,則函數會返回與該項option匹配的val值 int val; //和flag聯合決定返回值 };
定義option的示例
struct option long_options[] = { {"a123", required_argument, 0, 'a'}, {"c123", no_argument, 0, 'c'}, }
函數定義以下
int getopt_long(int argc, char * const argv[], const char *optstring, const struct option *longopts, int *longindex); int getopt_long_only(int argc, char * const argv[], const char *optstring, const struct option *longopts, int *longindex);
#include #include int main( int argc, char **argv ) { struct option long_options[] = { {"a123", required_argument, 0, 'a'}, {"c123", no_argument, 0, 'c'}, } int opt; printf("starting... "); while((opt = getopt_long(argc, argv, "a:c", long_options, NULL)) != -1) { switch (opt) { case 'a': printf("It's a! "); printf("string of a:%s ",optarg); break; case 'c': printf("It's c! "); break; default: printf("You should look for help! "); exit(1); break; } } printf("end... "); return 0; } 編譯後,假設生成a.out,能夠試驗一下。 ./a.out -a hello -c 輸出: starting... It's a! string of a:hello It's c! end...
UNIX 用戶始終依賴於命令行參數來修改程序的行爲,特別是那些設計做爲小工具集合 (UNIX 外殼環境)的一部分使用的實用工具更是如此。程序須要可以快速處理各個選項和參數,且要求不會浪費開發人員的太多時間。畢竟,幾乎沒有程序設計爲僅處理命令行參數,開發人員更應該將精力放在程序所實際進行的工做上。getopt() 函數是一個標準庫調用,可容許您使用直接的 while/switch 語句方便地逐個處理命令行參數和檢測選項(帶或不帶附加的參數)。與其相似的 getopt_long() 容許在幾乎不進行額外工做的狀況下處理更具描述性的長選項,這很是受開發人員的歡迎。既然已經知道了如何方便地處理命令行選項,如今就能夠集中精力改進您的程序的命令行,能夠添加長選項支持,或添加以前因爲不想向程序添加額外的命令行選項處理而擱置的任何其餘選項。不要忘記在某處記錄您全部的選項和參數,並提供某種類型的內置幫助函數來爲健忘的用戶提供幫助
http://www.cnitblog.com/zouzheng/archive/2007/04/02/25034.aspx
http://www.ibm.com/developerworks/cn/aix/library/au-unix-getopt.html