getopt()函数
在讨论这个函数之前我们先理解两个概念:选项及其参数
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()函数的更多相关文章
- getopt函数的使用——分析命令行参数
getopt(分析命令行参数) getopt(分析命令行参数) 短参数的定义 返回值 范例 getopt_long 相关函数表头文件#include<unistd.h> 函数声明int g ...
- Linux c 下使用getopt()函数
命令行参数解析函数 —— getopt() getopt()函数声明如下: #include <unistd.h> int getopt(int argc, char * const ar ...
- Linux下getopt()函数的简单使用
最近在弄Linux C编程,本科的时候没好好学啊,希望学弟学妹们引以为鉴. 好了,虽然啰嗦了点,但确实是忠告.步入正题: 我们的主角----getopt()函数. 英雄不问出处,getopt()函数的 ...
- C语言-getopt函数
#include<unistd.h> int getopt(int argc,char *const argv[],const char *optstring); extern char ...
- 如何使用getopt()函数解析参数
最近在写程序的过程中,把一部分时间都花费在程序对参数的处理上.今天听了学长说到getopt函数,才发现原来c里面还有一个专门解决参数处理的函数,查询了相关资料,这里简单总结一下. 使用int main ...
- liunx 系统调用 getopt() 函数
命令行参数解析函数 -- getopt() getopt()函数声明如下: #include <unistd.h>int getopt(int argc, char * const arg ...
- Python中getopt()函数的使用
在运行程序时,可能需要根据不同的条件,输入不同的命令行选项来实现不同的功能.目前有短选项和长选项两种格式.短选项格式为"-"加上单个字母选项:长选项为"--"加 ...
- [转载]Linux下getopt()函数的简单使用
转载源地址:https://www.cnblogs.com/qingergege/p/5914218.html 1.getopt()函数的出处就是unistd.h头文件(哈哈),写代码的时候千万不要忘 ...
- Linux getopt()函数 getopt_long()函数---转
http://hi.baidu.com/scoundrelgg/item/d4083f8412eea05d26ebd97f Linux getopt()函数 getopt_long()函数 get_o ...
- linux之getopt 函数(转)
命令行参数解析函数 —— getopt() getopt()函数声明如下: #include <unistd.h> int getopt(int argc, char * const ar ...
随机推荐
- 《github一天一道算法题》:分治法求数组最大连续子序列和
看书.思考.写代码. /*************************************** * copyright@hustyangju * blog: http://blog.csdn. ...
- cassandra命令
压力测试:cassandra-stress [command] -node [nodes] -mode thrift user=[user] password=[password] example: ...
- C# Socket服务端和客户端互相send和receive
服务端 { ]; ; { ...
- Oracle视图基础
--表的另一种形式,看起来很像表 --用view可以实现复杂的query====select --创建一个视图 --当经常使用这个查询时封装成view /*create or replace 表示在创 ...
- Object-C 重载
方法重载要保证三个条件 1在同一个类中 2.方法参数类型相同 名称相同 3.方法的参数不同 请看下面的例子 @interface whgMyObject : NSObject -(void)print ...
- SVN(一次检出&二次检出)
一次检出: >进入经历文件夹 >输入svn checkout指令 >输入电脑密码 >输入用户名 >输入密码 >检出成功 第二次检出: >进入小涛文件夹 > ...
- 一个利用Dataflow实现的Actor
最近进行并发数据处理,学习到了 Actor模型,其中最简单的实现方式是一位大牛利用Dataflow实现的. 大牛的方案:http://www.jayway.com/2013/11/15/an-acto ...
- Android 测试工具集02
User scenario testing for Android(功能性测试框架) Robotium is an Android test automation framework that has ...
- Android Material Design之Toolbar与Palette
转:http://blog.csdn.net/jdsjlzx/article/details/41441083 前言 我们都知道Marterial Design是Google推出的全新UI设计规范,如 ...
- BroadcastReceiver浅析
1.什么是BroadcastReceiver? 本质上是属于一个监听器,但onXxxListenter只是程序级别的监听器,当程序退出时候监听器也随之关闭.而BroadcastReceiver是系统级 ...