C getopt.h
C getopt.h
- getopt.h
- getopt函數
- getopt_long函數
- TensorRT代碼片段
- 參考連結
getopt.h
Gnulib
是GNU開源的庫,廣泛用於各種軟體、套件中。getopt.h
則是這個開源庫裡的一個頭文件。
來自11.17 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函數
#include <unistd.h>int 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函數
#include <getopt.h>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_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回傳-1if (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
參考連結
getopt_long(3) - Linux man page
11.17 getopt.h
GitHub - gcc/include/getopt.h
C getopt.h相关推荐
- getopt.h和getopt(),getopt_long()等函数
下载了一个牛人的代码,里面包括了一个getopt.h的头文件,在vs2008下无法通过编译,没有这个头文件,上网搜索了一些信息,记录下来,以方便以后查阅. getopt.h和对应的链接库不是每个编译器 ...
- getopt.h及相应的函数
getopt.h和对应的链接库不是每个编译器都有的,gcc编译器好像是有的,但是在vs2008是没有的,所以就要去网上下载跨平台的代码,不管怎么样,最终要把getopt.h和相应的lib文件和工程链接 ...
- C语言解析命令行函数:getopt系列
头文件:/usr/include/getopt.h 函数传入较长参数 函数getopt_long_only和getopt_long两者用法差不多,都可以用来解析命令行选项 函数出处 #include ...
- linux c 命令行解析 getopt getopt_long optarg optind opterr optopt 简介
目录 getopt 函数 测试代码 getopt_long函数 代码示例 getopt 函数 头文件 #include<unistd.h> 定义函数 int getopt(int argc ...
- linux c 命令行解析函数 getopt getopt_long
平时在写程序时常常需要对命令行参数进行处理,因为参数少,自己解析就可以搞定:如果命令行个数比较多时,如果按照顺序一个一个定义参数含义很容易造成混乱,而且如果程序只按顺序处理参数的话,一些"可 ...
- linux c 命令行参数处理函数 getopt()和getopt_long()
在实际编程当中,自己编写代码处理命令行参数是比较麻烦且易出错的.一般我们会直接使用getopt()和getopt_long()函数,下文将介绍具体的使用方法. getopt() getopt()用于处 ...
- Python 安装zbar-py时出现 无法打开包括文件: “unistd.h” no such file or directory
问题 途中使用的命令是cl.exe,在执行命令的时候找不到对应的unistd.h文件. unistd.h是Unix系统的文件,因此,十有八九,使用的是Windows系统.下面的代码可以修复,但是如果修 ...
- getopt和getopt_long的使用
getopt的函数使用 1.需要头文件#include <unistd.h>,其函数原型为int getopt(int argc, char *const argv[], const ch ...
- 函数 —— 分析命令行参数 getopt() getopt_long() getopt_long_only()
为什么需要命令行解析函数? 只按顺序处理参数的话,一些"可选参数"的功能将很难实现. 在Linux中,我们可以使用getopt.getopt_long.getopt_long_on ...
最新文章
- max7219c语言,51单片机+MAX7219数码管显示C程序
- mugen4g补丁如何使用_如何搜索下载游戏
- 从数据库表中随机获取N条记录的SQL语句
- Android官方开发文档Training系列课程中文版:使用Fragment构建动态UI之与其它Fragment通信
- 三观碎一地:轮子天天见,车轮悖论却2000年无解?
- Python sys模块的使用
- [转载] RGB与16进制色互转
- 《高翔视觉slam十四讲》学习笔记 第三讲 三维空间刚体运动
- COCOS CREATOR(TS)之按钮事件
- Freescale mx27 DDR 初始化代码分析
- A311D项目开发总结
- dataGridView单元格引用
- RK平台ME3630模块GPS移植调试
- 正雅齿科运用新数字技术为正畸行业开辟新空间
- 配置OSPF实现pc机互通小实验
- 【收藏】C#面试题整理笔试篇(最全1000+道带答案)300道填空 + 300道选择 + 300道判断 + 70道读程序写结果和看程序填空 + 100道简答题
- 是什么的简称_什么是“KDJ”?通俗易懂,让你了解股市上的那些英文简称
- Excel表格VLOOKUP函数的应用(如何把A表中的一列数据匹配到B表中)
- 【IOS】iphone型号和Model Identifier对应关系
- OpenStack该不该“隐形”?
热门文章
- WiFi广告强推原理
- 《代码整洁之道》—第1章1.4节思想流派
- Python制作小游戏(一)
- 【215】第K个大的数,K相关题目-分治、堆应用
- 2020年中国德化陶瓷博览会暨茶具文化节隆重举行—五洲御瓷分会场精品荟萃
- win10无法安装完成若要在此计算机上,windows10无法完成安装怎么解决_win10提示windows无法完成安装的解决教程...
- 百度SEO教程-利于百度推送工具实现百度快速收录
- 解决vscode打开中文乱码,用记事本打开却无乱码
- STM32-(08):USART通信基础
- 解决SecureCRT报错keyboard-interactive authentication with the ssh2 server failed