本文共 3876 字,大约阅读时间需要 12 分钟。
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
,並宣告optarg
,optind
,opterr
,optopt
等變數及getopt
,getopt_long
,getopt_long_only
等函數。
#includeint getopt(int argc, char * const argv[], const char *optstring);extern char *optarg;extern int optind, opterr, optopt;
getopt
的參數包括argc
及argv
,它的功能是解析可選參數(以-
開頭的參數)。
getopt
的第三個參數:optstring
字串是所有合法option character的集合。如果option character後跟了一個:
,代表該可選參數是有接參數的。該參數會被放入optarg
這個變數內。 如果我們連續地呼叫getopt
,它會連續地回傳option character(即跟在-
後的字串)。如果argv
中已經沒有可選參數了,則getopt
會回傳-1。
#includeint 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_long
與getopt
比起來,多了處理long option的功能,所謂的long option,即以--
開頭的參數。
與getopt
相比,getopt_long
多了兩個參數,分別是longopts
及longindex
:
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
val
:getopt_long
的回傳值longindex:如果非NULL,則它代表當前解析的是longopts
裡的第幾個參數
在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/