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. Centos6虚拟主机的实现

    centos6上虚拟主机的实现   实现虚拟主机有三种方式:基于IP的实现.基于端口的实现.基于FQDN的实现 一.基于IP的实现 1.先创建三个站点: mkdir /app/site1 mkdir ...

  2. 面试准备——java设计模式

    1 总体来说,设计模式分为三大类: 设计模式(design pattern)是对软件设计中普遍存在(反复出现)的各种问题,所提出的解决方案. 创建型模式(五种):工厂方法模式.抽象工厂模式.单例模式. ...

  3. Python第三方库之openpyxl(10)

    Python第三方库之openpyxl(10) 雷达图 在工作表上的列或行中排列的数据可以在雷达图中绘制.雷达图比较多个数据系列的总值.它实际上是一个圆形x轴上的面积图的投影.有两种类型的雷达图:st ...

  4. 请编写一个方法,返回某集合的所有非空子集。 给定一个int数组A和数组的大小int n,请返回A的所有非空子集。保证A的元素个数小于等于20,且元素互异。各子集内部从大到小排序,子集之间字典逆序排序,见样例。

    题解:观察测试样例,会发现每个子集的选择规律与二进制((2^n) - 1)到 1 的顺序生成的规律是一致的,样例中n=3,2^n-1=7,用二进制表示为111,其中每一位的1表示数组中的三个数都选择. ...

  5. C语言常见的函数调用

    C语言常见的函数调用 isatty,函数名,主要功能是检查设备类型,判断文件描述词是否为终端机. 函数名: isatty 用 法: int isatty(int desc); 返回值:如果参数desc ...

  6. 【边双连通】poj 3352 Road Construction

    http://poj.org/problem?id=3352 [题意] 给定一个连通的无向图,求最少加多少条边使得这个图变成边双连通图 [AC] //#include<bits/stdc++.h ...

  7. 【kmp+最小循环节】poj 2406 Power Strings

    http://poj.org/problem?id=2406 [题意] 给定字符串s,s=a^n,a是s的子串,求n最大是多少 [思路] kmp中的next数组求最小循环节的应用 例如 ababab ...

  8. leetcode 319 灯泡问题

    例子:1-9 1的因子1 2       1,2 3        1,,3 4        1,2,4 5        1,5 6        1,2,3,6 7        1,7 8   ...

  9. C#.net磁盘管理以及文件操作

    原文发布时间为:2008-08-08 -- 来源于本人的百度文章 [由搬家工具导入]    需要引入的命名空间: using System.IO;using System.Text; private ...

  10. R语言入门--画图(一)--ggplot2

    先写一些需要用到的知识点,比如包.函数 dplyr 很好用的包 经常与ggplot2连用 mutate:用于对数据框的列进行重新处理,或者用处理的结果添加新列 数据清洗: 1.na.omit()   ...