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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

命令行选项解析函数:getopt()

發布時間:2025/4/5 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 命令行选项解析函数:getopt() 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1、定義

int getopt(int argc, char * const argv[], const char *optstring);

2、描述

getopt是用來解析命令行選項參數的,但是只能解析短選項: -d 100,不能解析長選項:--prefix

3、參數

argc:main()函數傳遞過來的參數的個數 argv:main()函數傳遞過來的參數的字符串指針數組 optstring:選項字符串,告知 getopt()可以處理哪個選項以及哪個選項需要參數

4、返回

如果選項成功找到,返回選項字母;如果所有命令行選項都解析完畢,返回 -1;如果遇到選項字符不在 optstring 中,返回字符 '?';如果遇到丟失參數,那么返回值依賴于 optstring 中第一個字符,如果第一個字符是 ':' 則返回':',否則返回'?'并提示出錯誤信息。

5、下邊重點舉例說明optstring的格式意義:

char*optstring = “ab:c::”; 單個字符a 表示選項a沒有參數 格式:-a即可,不加參數 單字符加冒號b: 表示選項b有且必須加參數 格式:-b 100或-b100,但-b=100錯 單字符加2冒號c:: 表示選項c可以有,也可以無 格式:-c200,其它格式錯誤

上面這個 optstring 在傳入之后,getopt 函數將依次檢查命令行是否指定了 -a, -b, -c(這需要多次調用 getopt 函數,直到其返回-1),當檢查到上面某一個參數被指定時,函數會返回被指定的參數名稱(即該字母)

optarg —— 指向當前選項參數(如果有)的指針。 optind —— 再次調用 getopt() 時的下一個 argv指針的索引。 optopt —— 最后一個未知選項。 opterr -—— 如果不希望getopt()打印出錯信息,則只要將全域變量opterr設為0即可。

以上描述的并不生動,下邊結合實例來理解:

6、實例:

?

?

#include<stdio.h> #include<unistd.h> #include<getopt.h> int main(intargc, char *argv[]) {int opt;char *string = "a::b:c:d";while ((opt = getopt(argc, argv, string))!= -1){ printf("opt = %c\t\t", opt);printf("optarg = %s\t\t",optarg);printf("optind = %d\t\t",optind);printf("argv[optind] = %s\n",argv[optind]);} }

?

編譯上述程序并執行結果:

輸入選項及參數正確的情況

dzlab:~/test/test#./opt -a100 -b 200 -c 300 -d opt = a optarg = 100 optind = 2 argv[optind] = -b opt = b optarg = 200 optind = 4 argv[optind] = -c opt = c optarg = 300 optind = 6 argv[optind] = -d opt = d optarg = (null) optind = 7 argv[optind] = (null)

或者這樣的選項格式(注意區別):

dzlab:~/test/test#./opt -a100 -b200 -c300 -d opt = a optarg = 100 optind = 2 argv[optind] = -b200 opt = b optarg = 200 optind = 3 argv[optind] = -c300 opt = c optarg = 300 optind = 4 argv[optind] = -d opt = d optarg = (null) optind = 5 argv[optind] = (null)

選項a是可選參數,這里不帶參數也是正確的

dzlab:~/test/test#./opt -a -b 200 -c 300 -d opt = a optarg = (null) optind = 2 argv[optind] = -b opt = b optarg = 200 optind = 4 argv[optind] = -c opt = c optarg = 300 optind = 6 argv[optind] = -d opt = d optarg = (null) optind = 7 argv[optind] = (null)

輸入選項參數錯誤的情況

dzlab:~/test/test#./opt -a 100 -b 200 -c 300 -d opt = a optarg = (null) optind = 2 argv[optind] = 100 opt = b optarg = 200 optind = 5 argv[optind] = -c opt = c optarg = 300 optind = 7 argv[optind] = -d opt = d optarg = (null) optind = 8 argv[optind] = (null)

導致解析錯誤,第一個 optarg = null,實際輸入參數 100,由于格式不正確造成的(可選參數格式固定)

參數丟失,也會導致錯誤,c選項是必須有參數的,不加參數提示錯誤如下:

dzlab:~/test/test#./opt -a -b 200 -c opt = a optarg = (null) optind = 2 argv[optind] = -b opt = b optarg = 200 optind = 4 argv[optind] = -c ./opt: optionrequires an argument -- 'c' opt = ? optarg = (null) optind = 5 argv[optind] = (null)

這種情況,optstring 中第一個字母不是':',如果在 optstring 中第一個字母加':',則最后丟失參數的那個選項 opt 返回的是':',不是'?',并且沒有提示錯誤信息,這里不再列出。

命令行選項未定義,-e選項未在optstring中定義,會報錯:

dzlab:~/test/test#./opt -a -b 200 -e opt = a optarg = (null) optind = 2 argv[optind] = -b opt = b optarg = 200 optind = 4 argv[optind] = -e ./opt: invalidoption -- 'e' opt = ? optarg = (null) optind = 5 argv[optind] = (null)

總結

以上是生活随笔為你收集整理的命令行选项解析函数:getopt()的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。