[轉] getopt getopt_long函數


轉自http://blog.csdn.net/cashey1991/article/details/7942809

0 getopt

#include <unistd.h> 

int getopt(int argc, char * const argv[], 

           const char *optstring); 

extern char *optarg; 

extern int optind, opterr, optopt;

       函數的前面2個參數就是main函數的argc和argv,直接傳入即可。Optstring則表示接受參數的格式,1.單個字符,表示選項,(如上例中的abcde各爲一個選項);2.單個字符後接一個冒號:表示該選項後必須跟一個參數。參數緊跟在選項後或者以空格隔開。該參數的指針賦給optarg。(如上例中的b:c:);3 單個字符後跟兩個冒號,表示該選項後可以跟一個參數,也可以不跟。如果跟一個參數,參數必須緊跟在選項後不能以空格隔開。optind表示下一個將被處理的參數argv中的下標。getopt返回值即爲採樣到的參數的ASCII碼。

#include <unistd.h> 

#include <stdlib.h> 

#include <stdio.h> 

int main(int argc, char *argv[]) 

{ 

    int opt; 

    char *optstring = "a:b:c:d"; 

 

    while ((opt = getopt(argc, argv, optstring)) != -1) { 

        printf("opt = %c\n", opt); 

        printf("optarg = %s\n", optarg); 

        printf("optind = %d\n", optind); 

        printf("argv[optind - 1] = %s\n\n",  argv[optind - 1]); 

    } 

    return 0; 

} 

$ ./test_getopt -a 100 -b 200 -c admin -

opt =

optarg = 100 

optind = 3 

argv[optind - 1] = 100 

 

opt =

optarg = 200 

optind = 5 

argv[optind - 1] = 200 

 

opt =

optarg = admin 

optind = 7 

argv[optind - 1] = admin 

 

opt =

optarg = (null) 

optind = 8 

argv[optind - 1] = -

1 getopt_long

所有的GNU/Linux程序都遵循一些命令行參數定義的約定。程序希望出現的參數可以分成兩種:選項(options or flags)、其他類型的的參數。Options修飾了程序運行的方式,其他類型的參數則提供了輸入(例如,輸入文件的名稱)。

對於options類型參數可以有兩種方式:

    1)短選項(short options):顧名思義,就是短小參數。它們通常包含一個連字號和一個字母(大寫或小寫字母)。例如:-s,-h等。

    2)長選項(long options):長選項,包含了兩個連字號和一些大小寫字母組成的單詞。例如,--size,--help等。

    *注:一個程序通常會提供包括short options和long options兩種參數形式的參數。

       函數原型如下。

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

       longopts指向的是一個由option結構體組成的數組,那個數組的每個元素,指明瞭一個“長參數”(即形如--name的參數)名稱和性質:

struct option {

    const char *name;

    int         has_arg;

    int        *flag;

    int         val;

};

1) name  是參數的名稱

2has_arg 指明是否帶參數值,其數值可選:no_argument ( 0) 表明這個長參數不帶參數(即不帶數值,如:--namerequired_argument ( 1) 表明這個長參數必須帶參數(即必須帶數值,如:--name Boboptional_argument(即2)表明這個長參數後面帶的參數是可選的,(即--name--name Bob均可)

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

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

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

#include <unistd.h> 

#include <stdlib.h> 

#include <stdio.h> 

#include <getopt.h> 

 

int 

main(int argc, char **argv) 

{ 

   int opt; 

   int digit_optind = 0; 

   int option_index = 0; 

   char *optstring = "a:b:c:d"; 

   static struct option long_options[] = { 

       {"reqarg", required_argument, NULL, 'r'}, 

       {"noarg",  no_argument,       NULL, 'n'}, 

       {"optarg", optional_argument, NULL, 'o'}, 

       {0, 0, 0, 0} 

   }; 

 

   while ( (opt = getopt_long(argc, argv, optstring, long_options, &option_index)) != -1) 

   { 

        printf("opt = %c\n", opt); 

        printf("optarg = %s\n", optarg); 

        printf("optind = %d\n", optind); 

        printf("argv[optind - 1] = %s\n",  argv[optind - 1]); 

        printf("option_index = %d\n", option_index); 

   } 

 

   return 0; 

} 

./test_getopt_long -a 100 --reqarg 100 --nonarg 

opt =

optarg = 100 

optind = 3 

argv[optind - 1] = 100 

option_index = 0 

opt =

optarg = 100 

optind = 5 

argv[optind - 1] = 100 

option_index = 0 

./test_getopt_long: unrecognized option '--nonarg' 

opt = ? 

optarg = (null) 

optind = 6 

argv[optind - 1] = --nonarg 

option_index = 0 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章