C getopt.h
发布日期:2021-05-06 19:47:43 浏览次数:6 分类:技术文章

本文共 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

參考連結

上一篇:C++ struct初始化
下一篇:C++ struct的繼承

发表评论

最新留言

关注你微信了!
[***.104.42.241]2025年03月27日 21时37分04秒