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

       getopt()使用optstring所指的字串作为短参数列表,象"1ac:d::"就是一个短参数列表。短参数的定义是一个'-'后面跟一个字母或数字,象-a, -b就是一个短参数。每个数字或字母定义一个参数。 
  其中短参数在getopt定义里分为三种:
  1. 不带值的参数,它的定义即是参数本身。
  2. 必须带值的参数,它的定义是在参数本身后面再加一个冒号。
  3. 可选值的参数,它的定义是在参数本身后面加两个冒号 。
 
  在这里拿上面的"1ac:d::"作为样例进行说明,其中的1,a就是不带值的参数,c是必须带值的参数,d是可选值的参数。
  在实际调用中,'-1 -a -c cvalue -d', '-1 -a -c cvalue -ddvalue', '-1a -ddvalue -c cvalue'都是合法的。
 
  这里需要注意三点:
  1. 不带值的参数可以连写,象1和a是不带值的参数,它们可以-1 -a分开写,也可以-1a或-a1连写。
  2. 参数不分先后顺序,'-1a -c cvalue -ddvalue'和'-d -c cvalue -a1'的解析结果是一样的。
  3. 要注意可选值的参数的值与参数之间不能有空格,必须写成-ddvalue这样的格式,如果写成-d dvalue这样的格式就会解析错误。
返回值
   getopt()每次调用会逐次返回命令行传入的参数。
   当没有参数的最后的一次调用时,getopt()将返回-1。
   当解析到一个不在optstring里面的参数,或者一个必选值参数不带值时,返回'?'。
   当optstring是以':'开头时,缺值参数的情况下会返回':',而不是'?' 。
 
 
c.c文件
 #include <stdio.h>
#include <unistd.h> int main(int argc, char **argv)
{
int ch;
opterr = 0;
while ((ch = getopt(argc,argv,"a:bcde::f"))!=-1)
{
switch(ch)
{
case 'a':
printf("option a:'%s'\n",optarg);
break;
case 'b':
printf("option b :b\n");
break;
case 'e':
printf("option e:'%s'\n",optarg);
break;
default:
printf("other option :%c\n",ch);
}
}
printf("optopt +%c\n",optopt); return 1;
}

运行结果:

 ~ Home$ ./c -a
other option :?
optopt +a
~ Home$ ./c -a
option a:''
optopt +a
~ Home$ ./c -a123
option a:''
optopt +a
~ Home$ ./c -b
option b :b
optopt +b
~ Home$ ./c -cde
other option :c
other option :d
other option :?
optopt +e
~ Home$ ./c -e
other option :?
optopt +e
~ Home$ ./c -e123
option e:''
optopt +e
~ Home$ ./c -e
option e:''
optopt +e
~ Home$ ./c -f
other option :f
optopt +f
~ Home$ ./c -a -bcdf -e
option a:''
option b :b
other option :c
other option :d
other option :f
option e:''
optopt +e

当optstring(":a:bcde::f")是以':'开头时,缺值参数的情况下会返回':',而不是'?' 。

getopt(argc,argv,":a:bcde::f")  
1 $ ./c -a
2 other option ::
3 optopt +a

可变optstring

 #include <stdio.h>
#include <unistd.h> #ifdef RDP2
#define VNCOPT "V:Q"
#else
#define VNCOPT "V:"
#endif int main(int argc, char **argv)
{
int ch;
opterr = ;
while ((ch = getopt(argc,argv, VNCOPT ":a:bcde::f"))!=-)
{
switch(ch)
{
case 'V':
printf("option V:'%s'\n",optarg);
break;
case 'a':
printf("option a:'%s'\n",optarg);
break;
case 'b':
printf("option b :b\n");
break;
case 'e':
printf("option e:'%s'\n",optarg);
break;
default:
printf("other option :%c\n",ch);
}
}
printf("optopt +%c\n",optopt); return ;
}

reference:

http://vopit.blog.51cto.com/2400931/440453

 
 

[c language] getopt的更多相关文章

  1. [c language] getopt 其参数optind 及其main(int argc, char **argv) 参数解释

    getopt被用来解析命令行选项参数.#include <unistd.h> extern char *optarg; //选项的参数指针extern int optind, //下一次调 ...

  2. go语言项目汇总

    Horst Rutter edited this page 7 days ago · 529 revisions Indexes and search engines These sites prov ...

  3. Golang优秀开源项目汇总, 10大流行Go语言开源项目, golang 开源项目全集(golang/go/wiki/Projects), GitHub上优秀的Go开源项目

    Golang优秀开源项目汇总(持续更新...)我把这个汇总放在github上了, 后面更新也会在github上更新. https://github.com/hackstoic/golang-open- ...

  4. 使用 getopt() 进行命令行处理

    引言 在早期的 UNIX® 中,其命令行环境(当时的唯一用户界面)包含着数十种小的文本处理工具.这些工具非常小,通常可很好地完成一项工作.这些工具通过较长的命令管道链接在一起,前面的程序将其输出传递给 ...

  5. [记录]Shell中的getopts和getopt用法

    Shell中的getopts和getopt用法 1.getopts getopts(shell内置命令)不能直接处理长的选项(如:--prefix=/home等),getopts有两个参数,第一个参数 ...

  6. Java中的GetOpt操作

    在shell工具中,有专门的getopt函数,使用方法如下所示: while getopts "d:t:vh" opt; do case "${opt}" in ...

  7. Shell 参数(2) --解析命令行参数工具:getopts/getopt

    getopt 与 getopts 都是 Bash 中用来获取与分析命令行参数的工具,常用在 Shell 脚本中被用来分析脚本参数. 两者的比较 (1)getopts 是 Shell 内建命令,geto ...

  8. getopt使用

    参考: http://www.gnu.org/software/libc/manual/html_node/Example-of-Getopt.html http://en.wikipedia.org ...

  9. getopt,getoptlong学习

         getopt和getoptlong被用来解析命令行参数.   一.getopt #include <unistd.h> extern char *optarg; extern i ...

随机推荐

  1. Effective Java2读书笔记-类和接口(二)

    第15条:使可变性最小化 通过一个复数类来看不可变类. public final class Complex { private final double re; private final doub ...

  2. Codeforces 474D Flowers

    http://codeforces.com/problemset/problem/474/D 思路:F[i]=F[i-1]+(i>=K)F[i-k] #include<cstdio> ...

  3. Windows 8.1 with Update 镜像下载(增OEM单语言版)

    该系统已有更新的版本,请转至<Windows 8.1 with update 官方最新镜像汇总>下载. 2014年4月9日凌晨,微软向MSDN订阅用户开放了Windows 8.1 with ...

  4. Linux下编译第三方库的问题

    因为各个Linux发行版之间的差异还是挺大的,有一些预安装在系统上的基本库是不一样的(不仅仅是版本,有一些是有和无的区别). 那么问题来了: 编译第三方库./configure的时候一般我们不会定制那 ...

  5. logstash 中的贪婪匹配

    logstash 中的贪婪匹配: 10.252.142.174 - - [06/Sep/2016:08:41:36 +0800] "GET /api/validate/code/send?m ...

  6. HTML5 CSS3 诱人的实例 :模仿优酷视频截图功能

    一般的视频网站对于用户上传的视频,在用户上传完成后,可以对播放的视频进行截图,然后作为视频的展示图.项目中也可以引入这样的功能给用户一种不错的体验,而不是让用户额外上传一张展示图. 效果图: 看起来还 ...

  7. Sliding Window Maximum 解答

    Question Given an array of n integer with duplicate number, and a moving window(size k), move the wi ...

  8. Search in Rotated Sorted Array (I, II) 解答

    Question Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 ...

  9. UGUI 快捷键创建UGUI组件

      使用NGUI的时候还有xxx快捷键创建, spirte,label,button等等. 在UGUI里面的时候好像是没有快捷键的. 不知道以后多久才能有这个功能.  在家里闲无聊的时候写了一个脚本, ...

  10. samba服务器概述

    一.samba服务器概述 Samba是一个能让Linux系统应用Microsoft网络通信协议的软件.而SMB是Server Message Block的缩写,即为服务器消息块.SMB主要作为Micr ...