Linux选项-getopt/getopt_long
Linux选项-getopt/getopt_long
一、命令行简介
解释分析命令行通常是所以程序的第一个任务,C语言通过argc和argv参数来访问它的命令行参数。
最简单的命令行处理技术可以通过if判断来表示,如下例:
- if(argc>1 && argv[1][0] =='-' && argv[1][1] =='h') //判断命令行参数是否为-h
- {
- do _ some thing();
- }
这样处理简单有序的命令行还可以,对于复杂的命令行处理显得有心无力,于是GNU提供两个函数专门用来处理命令行参数:
getopt 和getopt_long
二、getopt函数
getopt() 函数声明如下:
- #include <unistd.h>
- int getopt(int argc, char *const argv[], const char *optstring);
- extern char *optarg;
- extern int optind, opterr, optopt;
说 明:
函数的argc和argv参数通常直接从main()的参数直接传递而来。optstring是选项字母组成的字串。如果该字串里的任一字符后面有冒号,那么这个选项就要求有选项参数。
当给定getopt()命令参数的数量 (argc)、指向这些参数的数组 (argv) 和选项字串 (optstring) 后,getopt() 将返回第一个选项,并设置一些全局变量。使用相同的参数再次调用该函数时,它将返回下一个选项,并设置相应的全局变量。如果不再有可识别的选项,将返回 -1,此任务就完成了。
getopt()所设置的全家变量包括:
optarg ---- 当前选项参数字符(如果有的话)
optind ---- argv的当前索引值。当getopt()在while循环中使用时,循环结束后,剩下的字串视为操作数,在
argv[optind]至argv[argc-1]中可以找到。
optopt ---- 用于当发现无效选项字符的时候,getopt函数或者返回 "?" 或者返回 ":" 字符,并且optopt包含了
所发现的无效选项字符,或者当输入非法参数的时候,由optopt带回输入的非法参数(字符)
opterr ---- 这个变量非零时,getopt()函数为“无效选项”和“缺少参数选项,并输出其错误信息。
另外:
如果optstring参数的第一个字符是冒号,那么getopt会根据错误情况返回不同的字符:
(1):当错误是无效选项,getopt返回 "?"
(2):当错误是缺少选项参数,getopt返回 ":"
(3): 无错误发生时,正常返回的是对应的字符!
注:GNU getopt()第三个特点是optstring中的选项字符后面接两个冒号,就允许该选项有可选的选项参数。在选项参数不存在的情况下,GNU getopt()返回选项字符并将optarg设置为NULL。
例子:
- #include<stdio.h>
- #include<unistd.h>
结果:
[root@localhost opts]# ./getopt -a
input para is a
optarg=(null).optind=2.opterr=1.optopt=0=[END]
[root@localhost opts]# ./getopt -a -b
input para is a
optarg=(null).optind=2.opterr=1.optopt=0=[END]
option b requires an argument
optarg=(null).optind=3.opterr=1.optopt=98=b[END]
[root@localhost opts]# ./getopt -a -b mingming
input para is a
optarg=(null).optind=2.opterr=1.optopt=0=[END]
input para is b,and optarg is mingming
optarg=mingming.optind=4.opterr=1.optopt=0=[END]
[root@localhost opts]# ./getopt -a -d
input para is a
optarg=(null).optind=2.opterr=1.optopt=0=[END]
option d is invalid:ignored
optarg=(null).optind=3.opterr=1.optopt=100=d[END]
|
评论暂时关闭