在讨论这个函数之前我们先理解两个概念:选项及其参数

  gcc -o program program.c

  在上述命令中 -o 就是其选项 program就是参数。

getopt();

  头文件: 

    #include<unistd.h>

  函数原型:  

    int getopt(int argc,char * const argv[ ],const char * optstring);

    argc为参数个数,argv是一个指针数组。

  外部变量

   extern char *optarg;   //选项的参数指针,指向下一个要扫描的参数

extern int optind,        //索引为下一个要处理的指针的下标

extern int opterr,        //当opterr=0时,getopt不向stderr输出错误信息;相反,不为0,就会向sterr打印一条错误信息

extern int optopt;       //用于储存错误或不可知的信息,当命令行选项字符不包括在optstring中或者选项缺少必要的参数时,该选项存储在optopt,getopt返回问号(?)。

  optstring参数所做的规定

    1.单个字符,表示选项,
    2.单个字符后接一个冒号(:)表示该选项后必须跟一个参数。参数紧跟在选项后或者以空格隔开。该参数的指针赋给optarg。
    3 单个字符后跟两个冒号(::),表示该选项后的参数可有可无。如果有,则参数必须紧跟在选项后不能以空格隔开。该参数的指针赋给optarg。

e.g.

  /*getopt.c*/
1 #include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
int main(int argc,char *argv[])
{
int opt;
while((opt=getopt(argc,argv,"ab:c::"))!=-){
switch(opt){
case 'a':
printf("option: %c\n",opt);
break;
case 'b':
printf("optarg1: %s\n",optarg);
break;
case 'c':
printf("optarg2: %s\n",optarg);
break;
case '?':
printf("unknown option :%c\n",optopt);
break;
}
}
for(;optind<argc;optind++)
printf("argument: %s\n",argv[optind]);
exit();
}

 编译 gcc -o getopt getopt.c 

 第一次运行 ./getopt -a -b "i am ok" -c

结果   

    option: a

    optarg1: i am ok

    optarg2: (null)

 第二次运行 ./getopt -a -b "i am ok" -c"I AM OK"

 结果

    option: a

    optarg1: i am ok

    optarg2: I AM OK

  

getopt()函数的更多相关文章

  1. getopt函数的使用——分析命令行参数

    getopt(分析命令行参数) getopt(分析命令行参数) 短参数的定义 返回值 范例 getopt_long 相关函数表头文件#include<unistd.h> 函数声明int g ...

  2. Linux c 下使用getopt()函数

    命令行参数解析函数 —— getopt() getopt()函数声明如下: #include <unistd.h> int getopt(int argc, char * const ar ...

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

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

  4. C语言-getopt函数

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

  5. 如何使用getopt()函数解析参数

    最近在写程序的过程中,把一部分时间都花费在程序对参数的处理上.今天听了学长说到getopt函数,才发现原来c里面还有一个专门解决参数处理的函数,查询了相关资料,这里简单总结一下. 使用int main ...

  6. liunx 系统调用 getopt() 函数

    命令行参数解析函数 -- getopt() getopt()函数声明如下: #include <unistd.h>int getopt(int argc, char * const arg ...

  7. Python中getopt()函数的使用

    在运行程序时,可能需要根据不同的条件,输入不同的命令行选项来实现不同的功能.目前有短选项和长选项两种格式.短选项格式为"-"加上单个字母选项:长选项为"--"加 ...

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

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

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

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

  10. linux之getopt 函数(转)

    命令行参数解析函数 —— getopt() getopt()函数声明如下: #include <unistd.h> int getopt(int argc, char * const ar ...

随机推荐

  1. hdu 5616 Jam's balance(dp 正反01背包)

    来自官方题解: AC代码: #pragma comment(linker, "/STACK:1024000000,1024000000") #include<iostream ...

  2. OC基础8:分类和协议

    "OC基础"这个分类的文章是我在自学Stephen G.Kochan的<Objective-C程序设计第6版>过程中的笔记. 1.关于分类(category): (1) ...

  3. SpringMVC+easyui显示数据

    近期做毕业设计,想用easyui,先学习一下CRUD.今天先弄了个表格显示数据库的数据.jsp页面还有非常多其他元素,我就不贴上去了.我显示数据的JSP为/WebContent/WEB-INF/vie ...

  4. C#整理4——循环语句

    一.循环语句  定义:可以反复执行某段代码,直到不满足循环条件为止. 循环的四要素:初始条件.循环条件.状态改变.循环体. 1.初始条件:循环最开始的状态. 2.循环条件:在什么条件下进行循环,不满足 ...

  5. javascript中的同源策略

    如果两个页面拥有相同的协议(protocol),端口(如果指定),和主机,那么这两个页面就是属于同一个源 览器有一个很重要的概念——同源策略(Same-Origin Policy).所谓同源是指,域名 ...

  6. Android开发环境的搭建之(四)虚拟设备AVD的基本配置

    配置Android系统语言包为中文简体.点击导航栏左数第二个“菜单”图标 选择“System settings”. 选择“Language & Input” 选择“Language” 选择“中 ...

  7. c#创建带参数的线程

    1.无参数线程的创建 Thread thread = new Thread(new ThreadStart(getpic)); thread.Start(); private void showmes ...

  8. BigDecimal用法详解(转)

    BigDecimal用法详解    http://www.cnblogs.com/linjiqin/p/3413894.html 一.简介Java在java.math包中提供的API类BigDecim ...

  9. HTML 5 JavaScript初步 编译运行.doc

    编译运行 解释运行 JavaScript:只有一种变量类型,var.数据类型:整型,小数,字符串,布尔型 1.如何把数值型字符串变成数字型: parseInt("字符串")——把字 ...

  10. Ubuntu 12.04 下安装git

    ---恢复内容开始--- 1.安装build-essential. 列出Git相关包(git-core 和 git-doc)所以来的各个安装包并安装: sudo apt-get build-dep g ...