日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

[转载][总结]函数getopt(),getopt_long及其参数optind

發(fā)布時(shí)間:2024/8/26 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [转载][总结]函数getopt(),getopt_long及其参数optind 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

?

看webbench源碼的時(shí)候碰到命令行解析工具getopt的使用,雖然之前也看過(guò)一點(diǎn),但都不是很全面,只是了解個(gè)大概,下面稍微總結(jié)一下:

?

getopt和optind:

getopt被用來(lái)解析命令行選項(xiàng)參數(shù)。

#include <unistd.h>
???????extern char *optarg;??//選項(xiàng)的參數(shù)指針
???????extern int optind, ??//下一次調(diào)用getopt的時(shí),從optind存儲(chǔ)的位置處重新開(kāi)始檢查選項(xiàng)。?
???????extern int opterr,??//當(dāng)opterr=0時(shí),getopt不向stderr輸出錯(cuò)誤信息。
???????extern int optopt;??//當(dāng)命令行選項(xiàng)字符不包括在optstring中或者選項(xiàng)缺少必要的參數(shù)時(shí),該選項(xiàng)存儲(chǔ)在optopt 中,getopt返回'?’、
???????int getopt(int argc, char * const argv[], const char *optstring);
?調(diào)用一次,返回一個(gè)選項(xiàng)。 在命令行選項(xiàng)參數(shù)再也檢查不到optstring中包含的選項(xiàng)時(shí),返回-1,同時(shí)optind儲(chǔ)存第一個(gè)不包含選項(xiàng)的命令行參數(shù)。

首先說(shuō)一下什么是選項(xiàng),什么是參數(shù)。

1.單個(gè)字符,表示選項(xiàng),

2.單個(gè)字符后接一個(gè)冒號(hào):表示該選項(xiàng)后必須跟一個(gè)參數(shù)。參數(shù)緊跟在選項(xiàng)后或者以空格隔開(kāi)。該參數(shù)的指針賦給optarg。
3 單個(gè)字符后跟兩個(gè)冒號(hào),表示該選項(xiàng)后必須跟一個(gè)參數(shù)。參數(shù)必須緊跟在選項(xiàng)后不能以空格隔開(kāi)。該參數(shù)的指針賦給optarg。(這個(gè)特性是GNU的擴(kuò)張)。

例如gcc -g -o test test.c ,其中g(shù)和o表示選項(xiàng),test為選項(xiàng)o的參數(shù)。

上面是getopt()函數(shù)的基本含義,大家懂得了這些之后,我們一個(gè)例子加深一下理解。

例如我們這樣調(diào)用getopt(argc, argv, "ab:c:de::");
從上面我們可以知道,選項(xiàng)a,d沒(méi)有參數(shù),選項(xiàng)b,c有一個(gè)參數(shù),選項(xiàng)e有有一個(gè)參數(shù)且必須緊跟在選項(xiàng)后不能以空格隔開(kāi)。getopt首先掃描argv[1]到argv[argc-1],并將選項(xiàng)及參數(shù)依次放到argv數(shù)組的最左邊,非選項(xiàng)參數(shù)依次放到argv的最后邊。

執(zhí)行程序?yàn)? 0??????1????2????3??4???5 ?????6???7????8???9? $ ./test file1 -a??-b -c code -d file2 -e file3 掃描過(guò)程中,optind是下一個(gè)選項(xiàng)的索引, 非選項(xiàng)參數(shù)將跳過(guò),同時(shí)optind增1。optind初始值為1。當(dāng)掃描argv[1]時(shí),為非選項(xiàng)參數(shù),跳過(guò),optind=2;掃描到-a選項(xiàng)時(shí), 下一個(gè)將要掃描的選項(xiàng)是-b,則optind更改為3;掃描到-b選項(xiàng)時(shí),后面有參數(shù)(會(huì)認(rèn)為-c為選項(xiàng)b的參數(shù)),optind=5,掃描到code非選項(xiàng)跳過(guò)optind=6;掃描到-d選項(xiàng),后面沒(méi)有參數(shù),optind=7;掃描到file2非選項(xiàng)跳過(guò)optind=8;掃描到-e后面本來(lái)應(yīng)該有參數(shù),optind=9但是有空格所以e的參數(shù)為空。 掃描結(jié)束后,getopt會(huì)將argv數(shù)組修改成下面的形式? ? ? ? //其實(shí)要理解這個(gè)函數(shù)最重要的就是這個(gè)重排和非選項(xiàng)參數(shù)的理解。經(jīng)過(guò)getopt函數(shù)的遍歷,選項(xiàng)及其參數(shù)應(yīng)該會(huì)被準(zhǔn)確處理,之后還需要處理非選項(xiàng)參數(shù),所以這個(gè)函數(shù) 0?????1???2 ??3???4????5???6????????7??????8??????9? ? ? ? ? ? ? ? ? ? ? //把非選項(xiàng)參數(shù)都放置到argv參數(shù)列表的最后面,然后把optind指向第一個(gè)非選項(xiàng)參數(shù),便于后面的處理!!!
$ ./test??-a??-b??-c??-d??-e??file1??code??file2??file3 同時(shí),optind會(huì)指向非選項(xiàng)的第一個(gè)參數(shù),如上面,optind將指向file1
代碼如下: 1 #include <unistd.h> 2 #include <stdio.h> 3 int main(int argc, char * argv[]) 4 { 5 int aflag=0, bflag=0, cflag=0; 6 int ch; 7 printf("optind:%d,opterr:%d\n",optind,opterr); 8 printf("--------------------------\n"); 9 while ((ch = getopt(argc, argv, "ab:c:de::")) != -1) 10 { 11 printf("optind: %d,argc:%d,argv[%d]:%s\n", optind,argc,optind,argv[optind]); 12 switch (ch) { 13 case 'a': 14 printf("HAVE option: -a\n\n"); 15 16 break; 17 case 'b': 18 printf("HAVE option: -b\n"); 19 20 printf("The argument of -b is %s\n\n", optarg); 21 break; 22 case 'c': 23 printf("HAVE option: -c\n"); 24 printf("The argument of -c is %s\n\n", optarg); 25 26 break; 27 case 'd': 28 printf("HAVE option: -d\n"); 29 break; 30 case 'e': 31 printf("HAVE option: -e\n"); 32 printf("The argument of -e is %s\n\n", optarg); 33 break; 34 35 case '?': 36 printf("Unknown option: %c\n",(char)optopt); 37 break; 38 } 39 } 40 printf("----------------------------\n"); 41 printf("optind=%d,argv[%d]=%s\n",optind,optind,argv[optind]); 42 }

執(zhí)行結(jié)果:
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循環(huán)執(zhí)行完后,optind=6

?

getopt_long:

getopt_long支持長(zhǎng)選項(xiàng)的命令行解析, 所為長(zhǎng)選項(xiàng)就是諸如--help的形式, 使用該函數(shù), 需要引入<getopt.h>下面是函數(shù)原型:

?

#include <getopt.h> 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);

?

其中?argc?,?argv?,?optstring?和getopt中的含義一樣, 下面解釋一下longopts?和longindex

longopts

longopts 指向一個(gè)struct option 的數(shù)組, 下面是option的定義:

struct option { const char *name; int has_arg; int *flag; int val; };

?

下面是各字段的含義?

  • name?- 長(zhǎng)選項(xiàng)的名稱(chēng), 例如?help
  • has_arg?- 是否帶參數(shù), 0 不帶參數(shù), 1 必須帶參數(shù), 2 參數(shù)可選
  • flag?- 指定長(zhǎng)選項(xiàng)如何返回結(jié)果, 如果flag為NULL,?getopt_long() 會(huì)返回val. 如果flag不為NULL,?getopt_long會(huì)返回0, 并且將val的值存儲(chǔ)到flag中
  • val?- 將要被getopt_long返回或者存儲(chǔ)到flag指向的變量中的值

下面是longopts的一個(gè)示例

struct option opts[] = { {"version", 0, NULL, 'v'}, {"name", 1, NULL, 'n'}, {"help", 0, NULL, 'h'} };

我們來(lái)看{"version", 0, NULL, 'v'},?version?即為長(zhǎng)選項(xiàng)的名稱(chēng), 即按如下形式--version,?0?表示該選項(xiàng)后面不帶參數(shù),?NULL?表示直接將v返回(字符v在ascii碼中對(duì)應(yīng)的數(shù)值), 即在使用getopt_long遍歷到該條選項(xiàng)時(shí),?getopt_long?返回值為字符v對(duì)應(yīng)的ascii碼值.?

longindex

longindex表示長(zhǎng)選項(xiàng)在longopts中的位置, 例如在上面的示例中,?version?對(duì)應(yīng)的?longindex?為0,?name?對(duì)應(yīng)的?longindex?為1,?help對(duì)應(yīng)的?longindex?為2, 該項(xiàng)主要用于調(diào)試, 一般設(shè)為 NULL 即可.

下面是一個(gè)使用示例:

void use_getpot_long(int argc, char *argv[]) { const char *optstring = "vn:h"; int c; struct option opts[] = { {"version", 0, NULL, 'v'}, {"name", 1, NULL, 'n'}, {"help", 0, NULL, 'h'} };while((c = getopt_long(argc, argv, optstring, opts, NULL)) != -1) { switch(c) { case 'n': printf("username is %s\n", optarg); break; case 'v': printf("version is 0.0.1\n"); break; case 'h': printf("this is help\n"); break; case '?': printf("unknown option\n"); break; case 0 : printf("the return val is 0\n"); break; default: printf("------\n"); } } }

?


然后我們運(yùn)行程序?./test --name zhangjikai --version --help --haha, 下面是運(yùn)行結(jié)果:?

username is zhangjikai version is 0.0.1 this is help ./test: unrecognized option '--haha' unknown option

當(dāng)然我們也可以使用短選項(xiàng)?./test -n zhangjikai -v -h?

下面我們對(duì)程序做一下修改, 這一次將 struct option 中的?flag?和?longindex?設(shè)為具體的值

void use_getpot_long2(int argc, char *argv[]) { const char *optstring = "vn:h"; int c;int f_v = -1, f_n = -1, f_h = -1, opt_index = -1; struct option opts[] = { {"version", 0, &f_v, 'v'}, {"name", 1, &f_n, 'n'}, {"help", 0, &f_h, 'h'} };while((c = getopt_long(argc, argv, optstring, opts, &opt_index)) != -1) { switch(c) { case 'n': printf("username is %s\n", optarg); break; case 'v': printf("version is 0.0.1\n"); break; case 'h': printf("this is help\n"); break; case '?': printf("unknown option\n"); break; case 0 : printf("f_v is %d \n", f_v); printf("f_n is %d \n", f_n); printf("f_h is %d \n", f_h); break; default: printf("------\n"); } printf("opt_index is %d\n\n", opt_index); } }

?


運(yùn)行程序:?./test --name zhangjikai --version --help?, 下面是運(yùn)行結(jié)果:?

f_v is -1 f_n is 110 f_h is -1 opt_index is 1f_v is 118 f_n is 110 f_h is -1 opt_index is 0f_v is 118 f_n is 110 f_h is 104 opt_index is 2


我們可以看到當(dāng)給?flag?指定具體的指針之后,?getopt_long?會(huì)返回0, 因此會(huì)去執(zhí)行case 0, 并且?val?的值賦給了?flag?指向的變量. 下面我們用短選項(xiàng)執(zhí)行一下程序?./test -n zhangjikai -v -h, 下面是運(yùn)行結(jié)果?

username is zhangjikai opt_index is -1version is 0.0.1 opt_index is -1this is help opt_index is -1

我們看到使用短選項(xiàng)的時(shí)候?getopt_long?就相當(dāng)于?getopt?,?flag?和?longindex都不起作用了.?

getopt_long 和 getopt_long_only

下面解釋一下?getopt_long?和?getopt_long_only的區(qū)別, 首先用下列選項(xiàng)運(yùn)行一下?use_getopt_long?./test -name zhangjkai -version -help?, 下面是輸出結(jié)果:

username is ame version is 0.0.1 ./test: invalid option -- 'e' unknown option ./test: invalid option -- 'r' unknown option ./test: invalid option -- 's' unknown option ./test: invalid option -- 'i' unknown option ./test: invalid option -- 'o' unknown option username is -help

我們看到使用短選項(xiàng)標(biāo)識(shí)符?-?指向長(zhǎng)選項(xiàng)時(shí), 程序還是會(huì)按短選項(xiàng)來(lái)處理, 即一個(gè)字符一個(gè)字符的解析. 下面我們將?use_getopt_long?做一下更改, 即將?getopt_long?改為?getopt_long_only?, 如下所示:?

void use_getpot_long3(int argc, char *argv[]) { const char *optstring = "vn:h"; int c; struct option opts[] = { {"version", 0, NULL, 'v'}, {"name", 1, NULL, 'n'}, {"help", 0, NULL, 'h'} };while((c = getopt_long_only(argc, argv, optstring, opts, NULL)) != -1) { switch(c) { case 'n': printf("username is %s\n", optarg); break; case 'v': printf("version is 0.0.1\n"); break; case 'h': printf("this is help\n"); break; case '?': printf("unknown option\n"); break; case 0 : printf("the return val is 0\n"); break; default: printf("------\n");} } }

username is zhangjikai下面再運(yùn)行程序?./test -name zhangjikai -version -help?, 下面是運(yùn)行結(jié)果:

version is 0.0.1 this is help

即使用?getopt_long_only?時(shí),?-?和?--都可以作用于長(zhǎng)選項(xiàng), 而使用?getopt_only?時(shí), 只有?--可以作用于長(zhǎng)選項(xiàng).?

?

轉(zhuǎn)載自:http://blog.sina.com.cn/s/blog_64ba2b750100vz5b.html

    http://blog.zhangjikai.com/2016/03/05/%E3%80%90C%E3%80%91%E8%A7%A3%E6%9E%90%E5%91%BD%E4%BB%A4%E8%A1%8C%E5%8F%82%E6%95%B0--getopt%E5%92%8Cgetopt_long/

?

?

轉(zhuǎn)載于:https://www.cnblogs.com/J1ac/p/9063148.html

總結(jié)

以上是生活随笔為你收集整理的[转载][总结]函数getopt(),getopt_long及其参数optind的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。