博客
关于我
C getopt.h
阅读量:287 次
发布时间:2019-03-03

本文共 3876 字,大约阅读时间需要 12 分钟。

C getopt.h

getopt.h

Gnulib是GNU開源的庫,廣泛用於各種軟體、套件中。getopt.h則是這個開源庫裡的一個頭文件。

來自,關於getopt.h的介紹:

Defines the type struct option and declares the variables optarg, optind, opterr, optopt and the functions getopt, getopt_long, getopt_long_only.

getopt.h定義了strcut option,並宣告optargoptindopterroptopt等變數及getoptgetopt_longgetopt_long_only等函數。

getopt函數

#include 
int getopt(int argc, char * const argv[], const char *optstring);extern char *optarg;extern int optind, opterr, optopt;

getopt的參數包括argcargv,它的功能是解析可選參數(以-開頭的參數)。

getopt的第三個參數:optstring字串是所有合法option character的集合。如果option character後跟了一個:,代表該可選參數是有接參數的。該參數會被放入optarg這個變數內。

如果我們連續地呼叫getopt,它會連續地回傳option character(即跟在-後的字串)。如果argv中已經沒有可選參數了,則getopt會回傳-1。

getopt_long函數

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

getopt_longgetopt比起來,多了處理long option的功能,所謂的long option,即以--開頭的參數。

getopt相比,getopt_long多了兩個參數,分別是longoptslongindex

  • longopts是為struct option

    struct option {         const char *name;    int         has_arg;    int        *flag;    int         val;};
    • name代表該long otpion的名字
    • has_arg有三種值可選:no_argument (or 0) required_argument (or 1) optional_argument (or 2)
    • flag:如果為NULL,則getopt_long回傳val
    • valgetopt_long的回傳值
  • longindex:如果非NULL,則它代表當前解析的是longopts裡的第幾個參數

TensorRT代碼片段

TensorRT/samples/common/argsParser.h中:

namespace samplesCommon{   //...inline bool parseArgs(Args& args, int argc, char* argv[]){       //無窮迴圈。等到參數解析完了,arg會自動變成-1,接著跳出迴圈    while (1)    {           int arg;        static struct option long_options[] = {   {   "help", no_argument, 0, 'h'}, {   "datadir", required_argument, 0, 'd'},            {   "int8", no_argument, 0, 'i'}, {   "fp16", no_argument, 0, 'f'}, {   "useILoop", no_argument, 0, 'l'},            {   "useDLACore", required_argument, 0, 'u'}, {   "batch", required_argument, 0, 'b'}, {   nullptr, 0, nullptr, 0}};        int option_index = 0;        // getopt_long用於解析傳入的參數,定義於Linux系統自帶的getopt.h,        // 亦或是samples/common/windows/getopt.c        // 在getopt_long被呼叫的過程中,option_index會隨之變化,代表該次解析的是long_options裡的第幾個參數        //getopt_long的回傳值是struct option裡最後一個元素,即'h','d','i','f','l','u','b'或0        //注意到d後面跟著一個冒號,這代表-d後面還跟著一個參數        //為何這裡optstring只有'h','d','i','u',少了'f','l','b'及0?        arg = getopt_long(argc, argv, "hd:iu", long_options, &option_index);        // 如果所有可選參數都解析完了,則getopt_long回傳-1        if (arg == -1)        {               break;        }        switch (arg)        {           //parseArgs的參數args傳入時是空的,在這個block裡依不同情況來為args設值        case 'h': args.help = true; return true;        case 'd':            //optarg定義於samples/common/windows/getopt.c,由getopt_long間接地被賦值,表示可選參數            //-d後跟著的參數會被解析到optarg內            if (optarg)            {                   args.dataDirs.push_back(optarg);            }            else            {                   std::cerr << "ERROR: --datadir requires option argument" << std::endl;                return false;            }            break;        case 'i': args.runInInt8 = true; break;        //'f'不在optstring內,能被解析出來?        case 'f': args.runInFp16 = true; break;        //?        //'l'不在optstring內,能被解析出來?        case 'l': args.useILoop = true; break;        case 'u':            //在optstring中,u後面沒加冒號,為何這裡可以有參數?            if (optarg)            {                   args.useDLACore = std::stoi(optarg);            }            break;        case 'b':            //'b'不在optstring內,能被解析出來?            if (optarg)            {                   args.batch = std::stoi(optarg);            }            break;        //如果傳入的參數不是上面的任一個,則程序執行失敗        default: return false;        }    }    return true;}} // namespace samplesCommon

參考連結

转载地址:http://xatm.baihongyu.com/

你可能感兴趣的文章
2020N1叉车司机模拟考试题库及N1叉车司机复审模拟考试
查看>>
2020熔化焊接与热切割考试及熔化焊接与热切割考试题库
查看>>
2020年G3锅炉水处理报名考试及G3锅炉水处理考试申请表
查看>>
2020年制冷与空调设备运行操作答案解析及制冷与空调设备运行操作考试总结
查看>>
2020年保育员(初级)考试资料及保育员(初级)新版试题
查看>>
2020年茶艺师(高级)考试内容及茶艺师(高级)考试申请表
查看>>
2021年烟花爆竹经营单位安全管理人员考试及烟花爆竹经营单位安全管理人员考试试卷
查看>>
2021年过氧化工艺试题及答案及过氧化工艺考试平台
查看>>
2021年重氮化工艺考试题库及重氮化工艺考试报名
查看>>
2021年车工(高级)考试总结及车工(高级)试题及答案
查看>>
2021年压力焊证考试及压力焊实操考试视频
查看>>
2021年低压电工考试及低压电工考试申请表
查看>>
2021年低压电工考试及低压电工考试申请表
查看>>
2021年A特种设备相关管理(电梯)考试APP及A特种设备相关管理(电梯)复审考试
查看>>
2021年美容师(初级)考试报名及美容师(初级)新版试题
查看>>
2021年N1叉车司机考试题及N1叉车司机复审模拟考试
查看>>
2021年危险化学品经营单位主要负责人考试APP及危险化学品经营单位主要负责人多少钱
查看>>
2021年T电梯修理考试技巧及T电梯修理模拟考试软件
查看>>
2021年R2移动式压力容器充装考试题及R2移动式压力容器充装找答案
查看>>
2021年电工(初级)考试及电工(初级)证考试
查看>>