getopt和getopt long函式使用詳解

2021-07-05 06:59:18 字數 3201 閱讀 3404

**getopt和getopt_long函式使用詳解**

在我們操作命令行的時候,main函式中輸入引數乙個乙個分析不免麻煩,我們可以使用linux的引數分析函式解決此問題方便省力。

#include 

int getopt(int argc, char * const argv,

const

char *optstring);

extern

char *optarg;

extern

int optind, opterr, optopt;

#include

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);

optstring的格式舉例說明比較方便,例如:

char *optstring = "abcd:";
上面這個optstring在傳入之後,getopt函式將依次檢查命令列是否指定了 -a, -b, -c及 -d(這需要多次呼叫getopt函式,直到其返回-1),當檢查到上面某乙個引數被指定時,函式會返回被指定的引數名稱(即該字母)

最後乙個引數d後面帶有冒號,: 表示引數d是可以指定值的,如 -d 100 或 -d user。

optind表示的是下乙個將被處理到的引數在argv中的下標值。

如果指定opterr = 0的話,在getopt、getopt_long、getopt_long_only遇到錯誤將不會輸出錯誤資訊到標準輸出流。

其中四個變數會經常使用到:

optarg: 當前位置的引數,:後面的引數。

optind: optind表示的是下乙個將被處理到的引數在argv中的下標值

opterr: 錯誤資訊

optopt: 。

#include 

#include

#include

int main(int argc, char *argv)

return

0;}

輸出結果:

ubuntu:~/desktop/getopt$ ./test_getopt -a 100 -b 200 -c admin -d

opt = a

optarg = 100

optind = 3

argv[optind - 1] = 100

opt = b

optarg = 200

optind = 5

argv[optind - 1] = 200

opt = c

optarg = admin

optind = 7

argv[optind - 1] = admin

opt = d

optarg = (null)

optind = 8

argv[optind - 1] = -d

下面來講getopt_long函式,getopt_long函式包含了getopt函式的功能,並且還可以指定「長引數」(或者說長選項),與getopt函式對比,getopt_long比其多了兩個引數:

int getopt(int argc, char * const argv,

const char *optstring);

int getopt_long(int argc, char * const argv,

const char *optstring,

const struct option *longopts, int *longindex);

在這裡,longopts指向的是乙個由option結構體組成的陣列,那個陣列的每個元素,指明了乙個「長引數」(即形如–name的引數)名稱和性質:

struct option ;

name 是引數的名稱

has_arg 指明是否帶引數值,其數值可選:

no_argument (即 0) 表明這個長引數不帶引數(即不帶數值,如:--name)

required_argument (即 1) 表明這個長引數必須帶引數(即必須帶數值,如:--name bob)

optional_argument(即2)表明這個長引數後面帶的引數是可選的,(即--name和--name bob均可)

flag 當這個指標為空的時候,函式直接將val的數值從getopt_long的返回值返回出去,當它非空時,val的值會被賦到flag指向的整型數中,而函式返回值為0

val 用於指定函式找到該選項時的返回值,或者當flag非空時指定flag指向的資料的值。

另乙個引數longindex,如果longindex非空,它指向的變數將記錄當前找到引數符合longopts裡的第幾個元素的描述,即是longopts的下標值

static

const struct option long_options=, ,

, ,, ,

, ,, ,

, ,, , };

while((opt=getopt_long(argc,argv,"912vfrt:p:c:?h",long_options,&options_index))!=eof )

if(tmp==optarg)

if(tmp==optarg+strlen(optarg)-1)

*tmp='\0';

proxyport=atoi(tmp+1);break;

case

':':

case

'h':

case

'?': usage();return

2;break;

case

'c': clients=atoi(optarg);break;

}

從中可以學習著怎麼使用getopt_long函式的使用。

getopt函式和getopt long函式

預備知識 1.getopt getopt在unix下的命令列程式特別好用,特別是在你有許多引數要加入時。表頭檔案 i nclude 函式宣告 int getopt int argc,char const argv,const char optstring 函式說明 getopt 用來分析命令列引數。...

getopt函式和getopt long函式詳解

getopt函式 函式功能 用來解析命令列引數,引數argc和argv分別代表引數個數和內容,跟main 函式裡的命令列引數一樣 函式所在標頭檔案 include 函式原型定義 int getopt int argc,char const argv const char optstring 引數op...

整理 getopt和getopt long函式

函式宣告 include int getopt int argc,char const argv,const char optstring extern char optarg extern int optind,opterr,optopt include int getopt long int a...