
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
,並宣告optarg
,optind
,opterr
,optopt
等變數及getopt
,getopt_long
,getopt_long_only
等函數。
getopt函數
#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。
getopt_long函數
#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代碼片段
在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
參考連結
发表评论
最新留言
关注你微信了!
[***.104.42.241]2025年03月27日 21时37分04秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
PHP7.0--如何使用函数的引用
2019-03-03
Java基础--01--数据类型/方法/数组
2019-03-03
【JokerのZYNQ7020】LINUX_EMIO_LED。
2019-03-03
【JokerのZYNQ7020】LINUX_EMIO_BUTTON。
2019-03-03
将代码从windows移动linux上出现^M错误的解决方法
2019-03-03
git查看相对于最新的push改动内容
2019-03-03
vim匹配特定的行并删除
2019-03-03
读取excel文件错误
2019-03-03
傅里叶变换的初级理解三
2019-03-03
伟大的欧拉公式
2019-03-03
F1 score的意义
2019-03-03
画多个图及相关细节总结
2019-03-03
python36+centos7离线安装tensorflow与talib的方法
2019-03-03
金融数据信噪比的影响力又一力证
2019-03-03
hdf5与hdfs的区别
2019-03-03
scala运行的方式
2019-03-03
启动spark集群的方法
2019-03-03
vim 有用命令-20190217
2019-03-03
tf.Session().as_default的作用
2019-03-03
isnull与isna的区别
2019-03-03