1、main函數(shù)參數(shù)形式
int main(int argc , char *argv[] , char *env[]);//第一個參數(shù)argc代表命令行的參數(shù)個數(shù)//第二個參數(shù)依次指向各個參數(shù),如"ls -l",argv[0]指向"ls",argv[1]指向"-l",argv[2]為NULL//第三個參數(shù)為當(dāng)前進(jìn)程環(huán)境變量列表
2、命令行參數(shù)識別
在命令行輸入命令時,選項(xiàng)的位置是隨意的,如ls -l -a 與ls -a -l的效果是一樣的
2.1、getopt(int argc , char *argv[] , char *optstring) 引入頭文件"unistd.h"
第一個參數(shù)、第二個參數(shù)同main函數(shù)的前兩個參數(shù),第三個參數(shù)optstring定義了選項(xiàng)和參數(shù)的形式
(1)單個字符表示該選項(xiàng)沒有參數(shù)
(2)單個字符后面接單個冒號,表示該選項(xiàng)后面必定有參數(shù),且以空格分隔選項(xiàng)和參數(shù)
(3)單個字符后面接雙冒號,表示該選項(xiàng)后可以有參數(shù)也可以沒有參數(shù);若有參數(shù),參數(shù)必須緊跟在選項(xiàng)后
如:
ab:c::
選項(xiàng)a沒有參數(shù);選項(xiàng)b有一個參數(shù)且用空格分隔;選項(xiàng)c可以有參數(shù)也可以沒有參數(shù),若有參數(shù),參數(shù)必須緊跟在選項(xiàng)后
此外還有4個與getopt相關(guān)的全局變量
int optopt:存儲未滿足optstring規(guī)定的選項(xiàng)
char *optarg:指向當(dāng)前選項(xiàng)的參數(shù)指針
int optind:確定選項(xiàng)和對應(yīng)參數(shù)后,指向下一個argv參數(shù)
int opterr:若設(shè)為0,則getopt不想stderr輸出錯誤信息
調(diào)用原理:
每次調(diào)用完getopt,都會返回對應(yīng)的選項(xiàng),使得optarg指向該選項(xiàng)對應(yīng)的參數(shù)。若選項(xiàng)不滿足optstring的規(guī)則,則返回‘?’,并將該選項(xiàng)存入optopt;若解析完畢則返回-1。
解析完畢后,getopt會重新排列argv中的參數(shù),所有不符合要求的選項(xiàng)和參數(shù)被放到argv的最后(其中選項(xiàng)在前、參數(shù)在后),并使optind指向這些不符合要求的選項(xiàng)參數(shù)在argv中的起始下標(biāo)。
1 #include <stdio.h> 2 #include <unistd.h> 3 4 int main(int argc ,char ** argv ,char ** env){ 5 int res; 6 opterr=0; //不顯示錯誤信息 7 while((res = getopt(argc , argv , "ab:c::")) != -1){ 8 switch(res){ 9 case 'a': 10 PRintf("option=a/toptarg=%s/toptopt=%c/toptind=%d/targv[optind]->%s/n",optarg,optopt,optind,argv[optind]); 11 break; 12 case 'b': 13 printf("option=b/toptarg=%s/toptopt=%c/toptind=%d/targv[optind]->%s/n",optarg,optopt,optind,argv[optind]); 14 break; 15 case 'c': 16 printf("option=c/toptarg=%s/toptopt=%c/toptind=%d/targv[optind]->%s/n",optarg,optopt,optind,argv[optind]); 17 break; 18 case '?': 19 printf("option=?/toptarg=%s/toptopt=%c/toptind=%d/targv[optind]->%s/n",optarg,optopt,optind,argv[optind]); 20 break; 21 } 22 23 } 24 printf("OVER/noptarg=%s/toptopt=%c/toptind=%d/targv[optind]->%s/n",optarg,optopt,optind,argv[optind]); //打印三個全局變量最終值 25 int i = 1 ; 26 printf("******************************************/n"); 27 for(; i < argc ; i++){ //打印重新排列后的命令行參數(shù) 28 printf("%s/n",argv[i]); 29 } 30 }sudo ./main -a -b bbb -c ccc -d dddoption=a optarg=(null) optopt= optind=2 argv[optind]->-boption=b optarg=bbb optopt= optind=4 argv[optind]->-coption=c optarg=(null) optopt= optind=5 argv[optind]->cccoption=? optarg=(null) optopt=d optind=7 argv[optind]->dddOVERoptarg=(null) optopt=d optind=6 argv[optind]->ccc******************************************-a-bbbb-c-dcccddd
2.2、getopt_long
它不僅能識別短選項(xiàng)(命令 -短選項(xiàng)),而且還支持長選項(xiàng)(命令 --長選項(xiàng))。
int getopt_long(int argc , char **argv , char * shortOpt ,struct option * longOpt, int *index) 注意:struct option 要引入頭文件”getopt.h“
前三個參數(shù)同getopt的參數(shù),shortOpt定義的短選項(xiàng)及參數(shù)的形式。
第四個參數(shù)定義的是結(jié)構(gòu)體option數(shù)組,
第五個參數(shù)記錄長選項(xiàng)位于longOpt數(shù)組的下標(biāo),一般情況下設(shè)為NULL
struct option{ const char *name; //長選項(xiàng)名稱 int has_arg; //0表示該選項(xiàng)沒有參數(shù),1表示必須有參數(shù),2參數(shù)為可選 int *flag; //一般為NULL,則getopt_long返回val;否則,將val的值賦給flag所指向的整形變量,函數(shù)返回值為0 int val; //一般設(shè)為長選項(xiàng)對應(yīng)的短選項(xiàng),若flag為NULL,則val作為函數(shù)的返回值。否則將val賦給flag所指向的變量};
若不想支持短選項(xiàng),則讓shortOpt為”“空字符串即可,但不能設(shè)為NULL。
1 #include <stdio.h> 2 #include <unistd.h> 3 #include <getopt.h> 4 int main(int argc , char **argv , char **env){ 5 int res; //保存函數(shù)返回值 6 struct option longOption[4]={ //定義長選項(xiàng)數(shù)組 7 {"help",0,NULL,'h'}, 8 {"version",0,NULL,'v'}, 9 {"output",1,NULL,'o'}, 10 {NULL,0,NULL,0} 11 }; 12 opterr=0; //不輸出錯誤信息 13 int index; //長選項(xiàng)位于longOption的下標(biāo) 14 while( -1 != (res = getopt_long(argc , argv , "hvo:",longOption ,&index))){ 15 switch(res){ 16 case 'h': 17 printf("option=help/n"); 18 break; 19 case 'v': 20 printf("option=version/n"); 21 break; 22 case 'o': 23 printf("option=output,output path=%s/n",optarg); 24 break; 25 case '?': 26 printf("error/n"); 27 break; 28 } 29 printf("index in longOption is %d/n",index); 30 } 31 32 return 0; 33 }輸出:
sudo ./main -hoption=helpindex in longOption is -1218614280 //對于短選項(xiàng),函數(shù)的第五個參數(shù)無效
sudo ./main -o /home/outoption=output,output path=/home/outindex in longOption is -1219220488
sudo ./main --versionoption=versionindex in longOption is 1
sudo ./main --output /home/outoption=output,output path=/home/outindex in longOption is 2
新聞熱點(diǎn)
疑難解答
圖片精選