liunx 系统调用 getopt() 函数
命令行参数解析函数 —— getopt()
| #include <unistd.h> int getopt(int argc, char * const argv[], const char *optstring); extern char *optarg; extern int optind, opterr, optopt; |
argc)、指向这些参数的数组 (argv) 和选项字串 (optstring) 后,getopt() 将返回第一个选项,并设置一些全局变量。使用相同的参数再次调用该函数时,它将返回下一个选项,并设置相应的全局变量。如果不再有可识别的选项,将返回 -1,此任务就完成了。getopt() 所设置的全局变量包括:char *optarg——当前选项参数字串(如果有)。int optind——argv的当前索引值。当getopt()在while循环中使用时,循环结束后,剩下的字串视为操作数,在argv[optind]至argv[argc-1]中可以找到。- int opterr——这个变量非零时,getopt()函数为“无效选项”和“缺少参数选项,并输出其错误信息。
int optopt——当发现无效选项字符之时,getopt()函数或返回'?'字符,或返回':'字符,并且optopt包含了所发现的无效选项字符。
选项:
- -n —— 显示“我的名字”。
- -g —— 显示“我女朋友的名字”。
- -l —— 带参数的选项.
清单2:
| #include <stdio.h> #include <unistd.h> int main (int argc, char **argv) { int oc; /*选项字符 */ char *b_opt_arg; /*选项参数字串 */ while((oc = getopt(argc, argv, "ngl:")) != -1) { switch(oc) { case 'n': printf("My name is Lyong.\n"); break; case 'g': printf("Her name is Xxiong.\n"); break; case 'l': b_opt_arg = optarg; printf("Our love is %s\n", optarg); break; } } return 0; } |
| $ ./opt_parse_demo -n My name is Lyong. $ ./opt_parse_demo -g Her name is Xxiong. $ ./opt_parse_demo -l forever Our love is forever $ ./opt_parse_demo -ngl forever My name is Lyong. Her name is Xxiong. Our love is forever |
6、改变getopt()对错误命令行参数信息的输出行为
清单3:
| #include <stdio.h> #include <unistd.h> int main (int argc, char **argv) { int oc; /*选项字符 */ char *b_opt_arg; /*选项参数字串 */ while((oc = getopt(argc, argv, "ngl:")) != -1) { switch(oc) { case 'n': printf("My name is Lyong.\n"); break; case 'g': printf("Her name is Xxiong.\n"); break; case 'l': b_opt_arg = optarg; printf("Our love is %s\n", optarg); break; case '?': printf("arguments error!\n"); break; } } return 0; } |
| $ ./opt_parse_demo -l ./opt_parse_demo: option requires an argument -- l arguments error! |
- 在调用getopt()之前,将opterr设置为0,这样就可以在getopt()函数发现错误的时候强制它不输出任何消息。
- 如果optstring参数的第一个字符是冒号,那么getopt()函数就会保持沉默,并根据错误情况返回不同字符,如下:
- “无效选项” —— getopt()返回'?',并且optopt包含了无效选项字符(这是正常的行为)。
- “缺少选项参数” —— getopt()返回':',如果optstring的第一个字符不是冒号,那么getopt()返回'?',这会使得这种情况不能与无效选项的情况区分开。
清单4:
| #include <stdio.h> #include <unistd.h> int main (int argc, char **argv) { int oc; /*选项字符 */ char ec; /*无效的选项字符*/ char *b_opt_arg; /*选项参数字串 */ while((oc = getopt(argc, argv, ":ngl:")) != -1) { switch(oc) { case 'n': printf("My name is Lyong.\n"); break; case 'g': printf("Her name is Xxiong.\n"); break; case 'l': b_opt_arg = optarg; printf("Our love is %s\n", optarg); break; case '?': ec = (char)optopt; printf("无效的选项字符 \' %c \'!\n", ec); break; case ':': printf("缺少选项参数!\n"); break; } } return 0; } |
$ ./opt_parse_demo -a
无效的选项字符 ' a '!
$ ./opt_parse_demo -l
缺少选项参数!
------ 转载 CSDN
liunx 系统调用 getopt() 函数的更多相关文章
- getopt函数的使用——分析命令行参数
getopt(分析命令行参数) getopt(分析命令行参数) 短参数的定义 返回值 范例 getopt_long 相关函数表头文件#include<unistd.h> 函数声明int g ...
- Linux c 下使用getopt()函数
命令行参数解析函数 —— getopt() getopt()函数声明如下: #include <unistd.h> int getopt(int argc, char * const ar ...
- Linux下getopt()函数的简单使用
最近在弄Linux C编程,本科的时候没好好学啊,希望学弟学妹们引以为鉴. 好了,虽然啰嗦了点,但确实是忠告.步入正题: 我们的主角----getopt()函数. 英雄不问出处,getopt()函数的 ...
- C语言-getopt函数
#include<unistd.h> int getopt(int argc,char *const argv[],const char *optstring); extern char ...
- 如何使用getopt()函数解析参数
最近在写程序的过程中,把一部分时间都花费在程序对参数的处理上.今天听了学长说到getopt函数,才发现原来c里面还有一个专门解决参数处理的函数,查询了相关资料,这里简单总结一下. 使用int main ...
- Python中getopt()函数的使用
在运行程序时,可能需要根据不同的条件,输入不同的命令行选项来实现不同的功能.目前有短选项和长选项两种格式.短选项格式为"-"加上单个字母选项:长选项为"--"加 ...
- 实验作业:使gdb跟踪分析一个系统调用内核函数
实验作业:使gdb跟踪分析一个系统调用内核函数(我使用的是getuid) 20135313吴子怡.北京电子科技学院 [第一部分] 根据视频演示的步骤,先做第一部分,步骤如下 ①更新menu代码到最新版 ...
- [转载]Linux下getopt()函数的简单使用
转载源地址:https://www.cnblogs.com/qingergege/p/5914218.html 1.getopt()函数的出处就是unistd.h头文件(哈哈),写代码的时候千万不要忘 ...
- Linux getopt()函数 getopt_long()函数---转
http://hi.baidu.com/scoundrelgg/item/d4083f8412eea05d26ebd97f Linux getopt()函数 getopt_long()函数 get_o ...
随机推荐
- 初学node.js有感三
WebStorm下的node.js 一.回顾与继续 在前面,我们知道了node.js的基本框架和思路,在这些原生环境下我们对node.js的设计思想有了比较深刻的认识,并且具有了编写大型程 ...
- UWP 改变Button样式
-----some words------ 1.Control:控制 (我们理解成控件) 2.Template:模板 3.Ellipse 椭圆 4.Content 内容 5.Presenter 节目主 ...
- input输入中文时,拼音在输入框内会触发input事件的问题。
问题描述: 监听文本输入框的input事件,在拼写汉字(输入法)但汉字并未实际填充到文本框中(选词)时会触发input事件,如图: 需要完成的需求就是在输入阶段不触发input中的事件,选词之后文字落 ...
- Vue 爬坑之路(六)—— 使用 Vuex + axios 发送请求
Vue 原本有一个官方推荐的 ajax 插件 vue-resource,但是自从 Vue 更新到 2.0 之后,官方就不再更新 vue-resource 目前主流的 Vue 项目,都选择 axios ...
- 用html +js+css 实现页面轮播图效果
html 页面 <html lang="en"> <head> <meta charset="UTF-8"> <met ...
- MVC 中获取Json数据
@{ ViewBag.Title = "json示例项目"; } @Scripts.Render("~/bundles/jquery") <h2>j ...
- C#-WinForm 串口通信
//C# 的串口通信,是采用serialPort控件,下面是对serialPort控件(也是串口通信必备信息)的配置如下代码: serialPort1.PortName = commcomboBox1 ...
- 1.ElasticSearch介绍及基本概念
一.ElasticSearch介绍 一个采用RESTful API标准的高扩展性的和高可用性的实时性分析的全文搜索工具 基于Lucene[开源的搜索引擎框架]构建 ElasticSearch是一个面向 ...
- iOS音频格式PCM转G711u(或G711a-law)
请尊重作者劳动成果,如需转载本博客文章请注明出处!谢谢合作! inputData是PCM的实时数据,可以通过转码,获取到最后导出的G711u数据(sendData) NSUInteger datal ...
- 使用Gradle构建Android项目
阅读目录 Gradle是什么? 环境需求 Gradle基本结构 任务task的执行 基本的构建定制 目录配置 签名配置 代码混淆设置 依赖配置 输出不同配置的应用 生成多个渠道包(以Umeng为例) ...