linux C语言getopt()函数的使用
getopt被用来解析命令行选项参数。
#include <unistd.h>
函数及参数介绍
extern char *optarg; //选项的参数指针,如果选项字符串里的字母后接着冒号“:”,则表示还有相关的参数,全域变量optarg 即会指向此额外参数。如果getopt()找不到符合的参数则会印出错信息,并将全域变量optopt设为“?”字符,如果不希望getopt()印出错信息,则只要将全域变量opterr设为0即可。
extern int optind, //下一次调用getopt的时,从optind存储的位置处重新开始检查选项。
extern int opterr, //当opterr=0时,getopt不向stderr输出错误信息。
extern int optopt; //当命令行选项字符不包括在optstring中或者选项缺少必要的参数时,该选项存储在optopt 中,getopt返回'?’
int getopt(int argc, char * const argv[], const char *optstring); 调用一次,返回一个选项。在命令行选项参数再也检查不到optstring中包含的选项时,返回-1,同时optind储存第一个不包含选项的命令行参数。
什么是选项,什么是参数
1.单个字符,表示选项,
2.单个字符后接一个冒号:表示该选项后必须跟一个参数。参数紧跟在选项后或者以空格隔开。该参数的指针赋给optarg。
3 单个字符后跟两个冒号,表示该选项后必须跟一个参数。参数必须紧跟在选项后不能以空格隔开。该参数的指针赋给optarg。
测试代码:
#include <stdio.h>
#include <unistd.h> int main(int argc, int *argv[])
{
int ch;
opterr = ;
while ((ch = getopt(argc,argv,"a:bcde"))!=-)
{
switch(ch)
{
case 'a':
printf("option a:'%s'\n",optarg);
break;
case 'b':
printf("option b :b\n");
break;
default:
printf("other option :c\n",ch);
}
}
printf("optopt +%c\n",optopt);
}
执行效果:
.$ ./getopt -a
.other option :?
.optopt +a
.$ ./getopt -b
.option b :b
.optopt +
.$ ./getopt -c
.other option :c
.optopt +
.$ ./getopt -d
.other option :d
.optopt +
.$ ./getopt -abcd
.option a:'bcd'
.optopt +
.$ ./getopt -bcd
.option b :b
.other option :c
.other option :d
.optopt +
.$ ./getopt -bcde
.option b :b
.other option :c
.other option :d
.other option :e
.optopt +
.$ ./getopt -bcdef
.option b :b
.other option :c
.other option :d
.other option :e
.other option :?
.optopt +f
linux C语言getopt()函数的使用的更多相关文章
- linux c语言 select函数用法
linux c语言 select函数用法 表头文件 #i nclude<sys/time.h> #i nclude<sys/types.h> #i nclude<unis ...
- linux c语言 select函数使用方法
linux c语言 select函数使用方法 表头文件 #i nclude<sys/time.h> #i nclude<sys/types.h> #i nclude<un ...
- C语言-getopt函数
#include<unistd.h> int getopt(int argc,char *const argv[],const char *optstring); extern char ...
- C语言getopt()函数的使用
getopt(分析命令行参数) 相关函数表头文件 #include<unistd.h>定义函数 int getopt(int argc,char * ...
- Linux下getopt()函数的简单使用
最近在弄Linux C编程,本科的时候没好好学啊,希望学弟学妹们引以为鉴. 好了,虽然啰嗦了点,但确实是忠告.步入正题: 我们的主角----getopt()函数. 英雄不问出处,getopt()函数的 ...
- [转载]Linux下getopt()函数的简单使用
转载源地址:https://www.cnblogs.com/qingergege/p/5914218.html 1.getopt()函数的出处就是unistd.h头文件(哈哈),写代码的时候千万不要忘 ...
- linux c语言 fork() 和 exec 函数的简介和用法
linux c语言 fork() 和 exec 函数的简介和用法 假如我们在编写1个c程序时想调用1个shell脚本或者执行1段 bash shell命令, 应该如何实现呢? 其实在<std ...
- Linux getopt()函数 getopt_long()函数---转
http://hi.baidu.com/scoundrelgg/item/d4083f8412eea05d26ebd97f Linux getopt()函数 getopt_long()函数 get_o ...
- Linux下getopt()函数
from https://www.cnblogs.com/qingergege/p/5914218.html 最近在弄Linux C编程,本科的时候没好好学啊,希望学弟学妹们引以为鉴. 好了,虽然啰嗦 ...
随机推荐
- UVA 539 The Settlers of Catan dfs找最长链
题意:画边求最长链,边不能重复数点可以. 很水,用暴力的dfs即可,因为数据不大. 本来以为可以用floyd进行dp的,后来想想好像不能在有回路上的图跑...于是没去做. #include <c ...
- PureCSS框架
http://www.tuicool.com/articles/iyiI32 Pure中的主要组件包括: 一个响应式网格,可根据需求定制. 一个基于 Normalize.css 的基础库,用于修复跨浏 ...
- LInux下socket编程学习笔记
1.socket套接字: socket起源于Unix,而Unix/Linux基本哲学之一就是“一切皆文件”,都可以用“打开open –> 读写write/read –> 关闭close”模 ...
- AI 对不起 我还爱着你
艾弗森,对不起,我还爱着你.有时候我自己都不知道自己我怎么了,直到最后才发现,我还爱着你. 那天起,我认识了你,便一发不可收拾.这些天,谢谢你,似乎因为你的影响让我改变了,坚持了许多.致以至今我才发现 ...
- poj 2446 二分图最大匹配
思路:由(i+j)为偶数的点向(i+j)为奇数的点建边.求一次最大匹配,若正好为空格数(不包含洞)的一半,即输出YES. #include<iostream> #include<cs ...
- merge into update
如果要DML实现真正意义上的并发,在开始执行需要并发语句前,需要执行开启session并发 ALTER SESSION ENABLE PARALLEL DML; 在执行完语句后,需要执行关闭ses ...
- Cisco中删除flash通过tftp服务器恢复
首先我们来看一下,Cisco路由器中的flash文件.通过show flash 命令来查看. 这时可以看到flash的版本是c1841-ipbasek9-mz.124-12.bin 下面我们删除这个f ...
- javascript 实现图片预览(未上传到服务器端)
1,图片预览 越来越多的浏览器为了安全,都不能获得文件的,全路径,实现图片预览实现起来有点麻烦.有人选择复制图片到服务器的某个路径下,然后从服务器端找到路径,实现预览.但这不是最佳实现方案,如果用户一 ...
- C#中Predicate<T>与Func<T, bool>泛型委托的用法实例
本文以实例形式分析了C#中Predicate<T>与Func<T, bool>泛型委托的用法,分享给大家供大家参考之用.具体如下: 先来看看下面的例子: 1 2 3 4 5 6 ...
- C#消息模拟
C#中消息的工作流程: C#中的消息被Application类从应用程序消息队列中取出,然后分发到消息对应的窗体,窗体对象的第一个响应函数是对象中的protected override void Wn ...