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

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

生活随笔

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

编程问答

getopt和getopt_long函数

發(fā)布時(shí)間:2023/12/9 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 getopt和getopt_long函数 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
平時(shí)在寫(xiě)程序時(shí)常常需要對(duì)命令行參數(shù)進(jìn)行處理,當(dāng)命令行參數(shù)個(gè)數(shù)較多時(shí),如果按照順序一個(gè)一個(gè)定義參數(shù)含義很容易造成混亂,而且如果程序只按順序處理參數(shù)的話(huà),一些“可選參數(shù)”的功能將很難實(shí)現(xiàn)。

在Linux中,我們可以使用getopt、getopt_long、getopt_long_only來(lái)對(duì)這個(gè)問(wèn)題進(jìn)行處理。

  • #include <unistd.h>
  • int getopt(int argc, char * const argv[],
  • const char *optstring);
  • extern char *optarg;
  • extern int optind, opterr, optopt;
  • #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);

  • 從最簡(jiǎn)單的getopt講起,getopt函數(shù)的前兩個(gè)參數(shù),就是main函數(shù)的argc和argv,這兩者直接傳入即可,要考慮的就只剩下第三個(gè)參數(shù)。

    optstring的格式舉例說(shuō)明比較方便,例如:

    ? ? char *optstring = "abcd:";

    上面這個(gè)optstring在傳入之后,getopt函數(shù)將依次檢查命令行是否指定了 -a, -b, -c及 -d(這需要多次調(diào)用getopt函數(shù),直到其返回-1),當(dāng)檢查到上面某一個(gè)參數(shù)被指定時(shí),函數(shù)會(huì)返回被指定的參數(shù)名稱(chēng)(即該字母)

    最后一個(gè)參數(shù)d后面帶有冒號(hào),: 表示參數(shù)d是可以指定值的,如 -d 100 或 -d user。

    optind表示的是下一個(gè)將被處理到的參數(shù)在argv中的下標(biāo)值。

    如果指定opterr = 0的話(huà),在getopt、getopt_long、getopt_long_only遇到錯(cuò)誤將不會(huì)輸出錯(cuò)誤信息到標(biāo)準(zhǔn)輸出流。

  • #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;
  • }
  • 編譯上述程序并運(yùn)行,有如下結(jié)果:

  • cashey@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

  • 下面來(lái)講getopt_long函數(shù),getopt_long函數(shù)包含了getopt函數(shù)的功能,并且還可以指定“長(zhǎng)參數(shù)”(或者說(shuō)長(zhǎng)選項(xiàng)),與getopt函數(shù)對(duì)比,getopt_long比其多了兩個(gè)參數(shù):

    ? ? ? ?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指向的是一個(gè)由option結(jié)構(gòu)體組成的數(shù)組,那個(gè)數(shù)組的每個(gè)元素,指明了一個(gè)“長(zhǎng)參數(shù)”(即形如--name的參數(shù))名稱(chēng)和性質(zhì):

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

    ? ? ? ?name ?是參數(shù)的名稱(chēng)

    ? ? ? ?has_arg 指明是否帶參數(shù)值,其數(shù)值可選:
    ? ? ? ? ? ? ? no_argument (即 0) 表明這個(gè)長(zhǎng)參數(shù)不帶參數(shù)(即不帶數(shù)值,如:--name)
    ? ? ? ? ? ? ? required_argument (即 1) 表明這個(gè)長(zhǎng)參數(shù)必須帶參數(shù)(即必須帶數(shù)值,如:--name Bob)
    ? ? ? ? ? ? optional_argument(即2)表明這個(gè)長(zhǎng)參數(shù)后面帶的參數(shù)是可選的,(即--name和--name Bob均可)

    ? ? ? ?flag ? 當(dāng)這個(gè)指針為空的時(shí)候,函數(shù)直接將val的數(shù)值從getopt_long的返回值返回出去,當(dāng)它非空時(shí),val的值會(huì)被賦到flag指向的整型數(shù)中,而函數(shù)返回值為0

    ? ? ? ?val ? ?用于指定函數(shù)找到該選項(xiàng)時(shí)的返回值,或者當(dāng)flag非空時(shí)指定flag指向的數(shù)據(jù)的值。

    另一個(gè)參數(shù)longindex,如果longindex非空,它指向的變量將記錄當(dāng)前找到參數(shù)符合longopts里的第幾個(gè)元素的描述,即是longopts的下標(biāo)值。

  • #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;
  • }
  • 編譯運(yùn)行以上程序并運(yùn)行,可以得到以下結(jié)果:

  • cashey@ubuntu:~/Desktop/getopt$ ./test_getopt_long -a 100 --reqarg 100 --nonarg
  • opt = a
  • optarg = 100
  • optind = 3
  • argv[optind - 1] = 100
  • option_index = 0
  • opt = r
  • 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

  • 當(dāng)所給的參數(shù)存在問(wèn)題時(shí),opt(即函數(shù)返回值是'?'),如:

  • cashey@ubuntu:~/Desktop/getopt$ ./test_getopt_long -a
  • ./test_getopt_long: option requires an argument -- 'a'
  • opt = ?
  • optarg = (null)
  • optind = 2
  • argv[optind - 1] = -a
  • option_index = 0
  • cashey@ubuntu:~/Desktop/getopt$ ./test_getopt_long --reqarg
  • ./test_getopt_long: option '--reqarg' requires an argument
  • opt = ?
  • optarg = (null)
  • optind = 2
  • argv[optind - 1] = --reqarg

  • 最后說(shuō)說(shuō)getopt_long_only函數(shù),它與getopt_long函數(shù)使用相同的參數(shù)表,在功能上基本一致,只是getopt_long只將--name當(dāng)作長(zhǎng)參數(shù),但getopt_long_only會(huì)將--name和-name兩種選項(xiàng)都當(dāng)作長(zhǎng)參數(shù)來(lái)匹配。在getopt_long在遇到-name時(shí),會(huì)拆解成-n -a -m -e到optstring中進(jìn)行匹配,而getopt_long_only只在-name不能在longopts中匹配時(shí)才將其拆解成-n -a -m -e這樣的參數(shù)到optstring中進(jìn)行匹配。

    總結(jié)

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

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