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是由main()传递的参数个数和内容。参数 optstring为选项字符串, 告知 getopt()可以处理哪个选项以及哪个选项需要参数,如果选项字符串里的字母后接着冒号“:”,则表示还有相关的参数,全域变量optarg 即会指向此额外参数。如果在处理期间遇到了不符合optstring指定的其他选项getopt()将显示一个错误消息,并将全域变量optarg设为“?”字符,如果不希望getopt()打印出错信息,则只要将全域变量opterr设为0即可。
getopt() 所设置的全局变量包括:
optarg——指向当前选项参数(如果有)的指针。 optind——再次调用 getopt() 时的下一个 argv 指针的索引。 optopt——最后一个已知选项。
 
optstring中的指定的内容的意义(例如getopt(argc, argv, "ab:c:de::");
1.单个字符,表示选项,(如上例中的abcde各为一个选项)
2.单个字符后接一个冒号:表示该选项后必须跟一个参数。参数紧跟在选项后或者以空格隔开。该参数的指针赋给optarg。(如上例中的b:c:)
3 单个字符后跟两个冒号,表示该选项后可以跟一个参数,也可以不跟。如果跟一个参数,参数必须紧跟在选项后不能以空格隔开。该参数的指针赋给optarg。(如上例中的e::)
 
元以下为代码示例:
 #include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char **argv)
{
char ch;
//opterr = 0; //getopt出错不向stderr输出错误信息 while((ch = getopt(argc, argv, "ab:c::")) != -)
{
switch(ch)
{
case 'a':
printf("option = a, optopt = %c, optind = %d, optarg = %s\n",
optopt, optind, optarg);
break;
case 'b':
printf("option = b, optopt = %c, optind = %d, optarg = %s\n",
optopt, optind, optarg);
break;
case 'c':
printf("option = c, optopt = %c, optind = %d, optarg = %s\n",
optopt, optind, optarg);
break;
case 'd':
printf("option = d, optopt = %c, optind = %d, optarg = %s\n",
optopt, optind, optarg);
break;
default:
printf("option = %c\n", ch);
}
printf("argv[%d] = %s\n", optind, argv[optind]);
} printf("ch == -1, optind = %d", optind); return ;
}

linux getopt函数详解的更多相关文章

  1. linux select函数详解

    linux select函数详解 在Linux中,我们可以使用select函数实现I/O端口的复用,传递给 select函数的参数会告诉内核: •我们所关心的文件描述符 •对每个描述符,我们所关心的状 ...

  2. linux select函数详解【转】

    转自:http://www.cnblogs.com/ccsccs/articles/4224253.html 在Linux中,我们可以使用select函数实现I/O端口的复用,传递给 select函数 ...

  3. Linux fcntl函数详解

    功能描述:根据文件描述词来操作文件的特性. 文件控制函数          fcntl -- file control 头文件: #include <unistd.h> #include ...

  4. Linux system函数详解

    system 功能:system()函数调用"/bin/sh -c command"执行特定的命令,阻塞当前进程直到command命令执行完毕 原型 int system(cons ...

  5. Linux wait函数详解

    wait和waitpid出现的原因 SIGCHLD --当子进程退出的时候,内核会向父进程SIGCHLD信号,子进程的退出是个异步事件(子进程可以在父进程运行的任何时刻终止) --子进程退出时,内核将 ...

  6. linux system()函数详解

    system(3) - Linux man page Name system - execute a shell command Synopsis #include <stdlib.h> ...

  7. Linux C popen()函数详解

    表头文件 #include<stdio.h> 定义函数 FILE * popen( const char * command,const char * type); 函数说明 popen( ...

  8. linux内核中send与recv函数详解

    Linux send与recv函数详解 1.简介 #include <sys/socket.h> ssize_t recv(int sockfd, void *buff, size_t n ...

  9. Linux环境fork()函数详解

    Linux环境fork()函数详解 引言 先来看一段代码吧, 1 #include <sys/types.h> 2 #include <unistd.h> 3 #include ...

随机推荐

  1. 自定义iOS上双击Home键图切换

    如果双击Home,会来到iOS App的switcher页面,在这儿列出了当前系统挂起的App, 上面有每个App的切屏,相信大家都熟悉这个东东了.它其实是每个App在挂起前,对App后个载屏. 那么 ...

  2. Appium+python自动化-appium元素定位

    前言 appium定位app上的元素,可以通过id,name.class这些属性定位到 一.id定位 1.appium的id属性也就是通过UI Automator工具查看的resource-id属性

  3. POJ-3352 Road Construction,tarjan缩点求边双连通!

    Road Construction 本来不想做这个题,下午总结的时候发现自己花了一周的时间学连通图却连什么是边双连通不清楚,于是百度了一下相关内容,原来就是一个点到另一个至少有两条不同的路. 题意:给 ...

  4. Android刷新页面

    代码改变世界 Android刷新页面 继承 extends Activity /*** 调用onCreate(), 目的是刷新数据,  从另一activity界面返回到该activity界面时, 此方 ...

  5. JMS API学习总结(一)

    三.JMS API简析 顶级接口 P2P Pub/sub 备注 ConnectionFactory QueueConnectionFactory TopicConnectionFactory 基于工厂 ...

  6. SSD ECC中的LDPC编解码原理

    转自:http://blog.csdn.net/zhuzongpeng/article/details/78899198 目前SSD中ECC纠错代码主要两种BCH和LDPC.不过,随着SSD对ECC纠 ...

  7. LightOJ1125 Divisible Group Sums

    Divisible Group Sums Given a list of N numbers you will be allowed to choose any M of them. So you c ...

  8. redmine与SVN的Https方式整合问题

    尼玛啊!这个SVN的整合搞了一晚上,今天早上终于搞定了,FUCK!!! 进入话题: 可以先在bitnami redmine stack的命令行环境下手工运行svn,看是否能取到数据, svn list ...

  9. 玩转css样式选择器----当父元素有多个子元素时选中第一个

  10. MongoDB_语法命令

    可以通过MongoDB shell 来连接MongoDB服务: ./mongo   进入交互 数据库-->集合-->文档 几个文档就组成了集合,可以设置固定大小的集合,集合就会有过期机制, ...