博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
getopt、getopt_long 简介
阅读量:4361 次
发布时间:2019-06-07

本文共 4141 字,大约阅读时间需要 13 分钟。

转自

1、getopt被用来解析命令行选项参数。就不用自己写东东处理argv了。

#include"unistd.h"

extern char optarg; //选项的参数指针
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储存第一个不包含选项的命令行参数。

首先说一下什么是选项,什么是参数。
字符串optstring可以下列元素,

1.单个字符,表示选项,2.单个字符后接一个冒号:表示该选项后必须跟一个参数。参数紧跟在选项后或者以空格隔开。该参数的指针赋给optarg。3.单个字符后跟两个冒号,表示该选项后必须跟一个参数。参数必须紧跟在选项后不能以空格隔开。该参数的指针赋给optarg。(这个特性是GNU的扩张)。

getopt处理以'-’开头的命令行参数,如optstring="ab:c::d::",命令行为getopt.exe -a -b host -ckeke -d haha

在这个命令行参数中,-a和-h就是选项元素,去掉'-',a,b,c就是选项。host是b的参数,keke是c的参数。但haha并不是d的参数,因为它们中间有空格隔开。
还要注意的是默认情况下getopt会重新排列命令行参数的顺序,所以到最后所有不包含选项的命令行参数都排到最后。
如getopt.exe -a ima -b host -ckeke -d haha, 都最后命令行参数的顺序是: -a -b host -ckeke -d ima haha
如果optstring中的字符串以'+'加号开头或者环境变量POSIXLY_CORRE被设置。那么一遇到不包含选项的命令行参数,getopt就会停止,返回-1。

#include 
#include
#include
int main(int argc, char **argv){ int result; opterr = 0; //使getopt不行stderr输出错误信息 while( (result = getopt(argc, argv, "ab:c::")) != -1 ) { switch(result) { case 'a': printf("option=a, optopt=%c, optarg=%s\n", optopt, optarg); break; case 'b': printf("option=b, optopt=%c, optarg=%s\n", optopt, optarg); break; case 'c': printf("option=c, optopt=%c, optarg=%s\n", optopt, optarg); break; case '?': printf("result=?, optopt=%c, optarg=%s\n", optopt, optarg); break; default: printf("default, result=%c\n",result); break; } printf("argv[%d]=%s\n", optind, argv[optind]); } printf("result=-1, optind=%d\n", optind); //看看最后optind的位置 for(result = optind; result < argc; result++) printf("-----argv[%d]=%s\n", result, argv[result]); //看看最后的命令行参数,看顺序是否改变了哈。 for(result = 1; result < argc; result++) printf("\nat the end-----argv[%d]=%s\n", result, argv[result]); return 0;}unistd里有个 optind 变量,每次getopt后,这个索引指向argv里当前分析的字符串的下一个索引,因此argv[optind]就能得到下个字符串,通过判断是否以 '-'开头就可。下面是个测试程序#include
#include
int main(int argc, char* argv[]){ int tmp = 4; while( (tmp = getopt(argc, argv, "abck")) != -1 ) { printf("-%c\t", tmp); int opt = optind ; while( opt < argc ) { if ( argv[opt][0] != '-' ) { printf("%s\t", argv[opt]); opt ++; } else break; } printf("\n"); } getchar();}

函 数说明 getopt()用来分析命令行参数。参数argc和argv是由main()传递的参数个数和内容。参数optstring 则代表欲处理的选项字符串。此函数会返回在argv 中下一个的选项字母,此字母会对应参数optstring 中的字母。如果选项字符串里的字母后接着冒号“:”,则表示还有相关的参数,全域变量optarg 即会指向此额外参数。如果getopt()找不到符合的参数则会印出错信息,并将全域变量optopt设为“?”字符,如果不希望getopt()印出错 信息,则只要将全域变量opterr设为0即可。

返回值 如果找到符合的参数则返回此参数字母,如果参数不包含在参数optstring 的选项字母则返回“?”字符,分析结束则返回-1。
范例

#include
#include
int main(int argc,char **argv){ int ch; opterr = 0; while((ch = getopt(argc,argv,”a:bcde”))!= -1) 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 –boption b:b$./getopt –cother option:c$./getopt –aother option :?$./getopt –a12345option a:’12345’

getopt 函数

函数定义:

#include int getopt(int argc, char * const argv[], const char *optstring); extern char *optarg;extern int optind, opterr, optopt; #define _GNU_SOURCE#include int getopt_long(int argc, char * const argv[], const char *optstring, const struct option *longopts, int *longindex); int getopt_long_only(int argc, char * const argv[], const char *optstring, const struct option *longopts, int *longindex);

转载于:https://www.cnblogs.com/shark-cf/p/5682484.html

你可能感兴趣的文章
POM.xml 标签详解
查看>>
hdu 3635 Dragon Balls (并查集)
查看>>
文件操作
查看>>
7.java集合,泛型简单总结,IO流
查看>>
杭电2007 平方和与立方和
查看>>
JS邮箱验证-正则验证
查看>>
Quartz 2D绘图
查看>>
JS Fetch
查看>>
EJB 笔记
查看>>
【delete】Android自定义控件(四) 自定义ImageView动态设置ImageView的高度
查看>>
HDUOJ------(1230)火星A+B
查看>>
Servlet
查看>>
基于jquery地图特效全国网点查看代码
查看>>
【leetcode】867 - Transpose Matrix
查看>>
selenium动作链
查看>>
敏捷外包工程系列之二:人员结构(敏捷外包工程,敏捷开发,产品负责人,客户价值)...
查看>>
《设计你的人生》的部分经典语录
查看>>
mustache多次渲染和多个赋值
查看>>
《Flutter 实战》开源电子书
查看>>
Python 键盘记录
查看>>