博客
关于我
C getopt.h
阅读量:306 次
发布时间: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/

你可能感兴趣的文章
mysql InnoDB数据存储引擎 的B+树索引原理
查看>>
mysql innodb通过使用mvcc来实现可重复读
查看>>
mysql insert update 同时执行_MySQL进阶三板斧(三)看清“触发器 (Trigger)”的真实面目...
查看>>
mysql interval显示条件值_MySQL INTERVAL关键字可以使用哪些不同的单位值?
查看>>
Mysql join原理
查看>>
MySQL Join算法与调优白皮书(二)
查看>>
Mysql order by与limit混用陷阱
查看>>
Mysql order by与limit混用陷阱
查看>>
mysql order by多个字段排序
查看>>
MySQL Order By实现原理分析和Filesort优化
查看>>
mysql problems
查看>>
mysql replace first,MySQL中处理各种重复的一些方法
查看>>
MySQL replace函数替换字符串语句的用法(mysql字符串替换)
查看>>
mysql replace用法
查看>>
Mysql Row_Format 参数讲解
查看>>
mysql select, from ,join ,on ,where groupby,having ,order by limit的执行顺序和书写顺序
查看>>
MySQL Server 5.5安装记录
查看>>
mysql server has gone away
查看>>
mysql slave 停了_slave 停止。求解决方法
查看>>
MySQL SQL 优化指南:主键、ORDER BY、GROUP BY 和 UPDATE 优化详解
查看>>