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()函数的使用的更多相关文章

  1. linux c语言 select函数用法

    linux c语言 select函数用法 表头文件 #i nclude<sys/time.h> #i nclude<sys/types.h> #i nclude<unis ...

  2. linux c语言 select函数使用方法

    linux c语言 select函数使用方法 表头文件 #i nclude<sys/time.h> #i nclude<sys/types.h> #i nclude<un ...

  3. C语言-getopt函数

    #include<unistd.h> int getopt(int argc,char *const argv[],const char *optstring); extern char ...

  4. C语言getopt()函数的使用

    getopt(分析命令行参数)     相关函数表头文件         #include<unistd.h>定义函数         int getopt(int argc,char * ...

  5. Linux下getopt()函数的简单使用

    最近在弄Linux C编程,本科的时候没好好学啊,希望学弟学妹们引以为鉴. 好了,虽然啰嗦了点,但确实是忠告.步入正题: 我们的主角----getopt()函数. 英雄不问出处,getopt()函数的 ...

  6. [转载]Linux下getopt()函数的简单使用

    转载源地址:https://www.cnblogs.com/qingergege/p/5914218.html 1.getopt()函数的出处就是unistd.h头文件(哈哈),写代码的时候千万不要忘 ...

  7. linux c语言 fork() 和 exec 函数的简介和用法

    linux c语言 fork() 和 exec 函数的简介和用法   假如我们在编写1个c程序时想调用1个shell脚本或者执行1段 bash shell命令, 应该如何实现呢? 其实在<std ...

  8. Linux getopt()函数 getopt_long()函数---转

    http://hi.baidu.com/scoundrelgg/item/d4083f8412eea05d26ebd97f Linux getopt()函数 getopt_long()函数 get_o ...

  9. Linux下getopt()函数

    from https://www.cnblogs.com/qingergege/p/5914218.html 最近在弄Linux C编程,本科的时候没好好学啊,希望学弟学妹们引以为鉴. 好了,虽然啰嗦 ...

随机推荐

  1. linux 文件夹权限及umask

    先创建一个目录,看看权限: $ ll 总用量 drwxrwxr-x huangxm huangxm 2月 : ./ drwxr-xr-x huangxm huangxm 2月 : ../ drwxrw ...

  2. 【VMware虚拟化解决方案】设计和配置VMware vCenter 5.5

    在这之前,我们已经对VMware ESXi 5.5进行了整个环境的设计和规划,虽然安装VMware ESXi 5.5在CPU的选型.网络的设计.共享存储的方式.虚拟化资源的需求和安装ESXI的模式等一 ...

  3. [设计模式]<<设计模式之禅>>关于迪米特法则

    迪米特法则(Law of Demeter,LoD)也称为最少知识原则(Least KnowledgePrinciple,LKP),虽然名字不同,但描述的是同一个规则:一个对象应该对其他对象有最少的了解 ...

  4. Linux下进程的创建

    这篇文章主要是讲解到Linux进程的控制,包括程序和进程.守护进程.守护进程的出错处理. 1.程序和进程 程序(program)是存放在磁盘文件中的可执行文件,程序的执行实例被称为进程(process ...

  5. nineOldAnimation 应用

    Android动画学习笔记-Android Animation   3.0以前,android支持两种动画模式,tween animation,frame animation,在android3.0中 ...

  6. Oracle导入SQL脚本执行 scott 用户下的表删除了

    执行 .sql 文件时,应在 sqlplus  或 cmd 中执行,速度比plsql 中的command window 中书许多, scott 用户下的表删除了 可以执行如下 @D:\app\Admi ...

  7. asp.net上传大文件

    Asp.net默认允许上传文件的最大值为4M. 如果想要上传更大的文件,需要修改web.config文件,方法是: 在<system.web>节点中添加代码 <httpRuntime ...

  8. (浅谈).Net控件GridView绑定数据

    前台GridView属性设置 <td> <asp:GridView ID="GridView" runat="server" AutoGene ...

  9. java-MySQL存储过程

    import java.sql.CallableStatement; import java.sql.Connection; import java.sql.DriverManager; import ...

  10. 【转】创业C2C(Copy To China):停车位共享APP,用户、市政能够买账?

    如果周六中午想开车到旧金山的Mission吃顿早午餐,笔者劝您还是省省吧.因为不光是到了吃饭的地儿排队得耗上一个小时,就是满大街的兜圈子找停车位都能折腾死人.那个时候您或许就明白了,其实最苦的并不是买 ...