GNU C库中
#include <unistd.h>
extern char *optarg; //选项参数指针
extern int optind; //下一次调用getopt的时,从optind存储的位置处重新开始检索选项。
extern int optopt; //当opterr=0时,getopt不向STDERR_FILENO输出错误信息。
extern int opterr; //当命令行选项缺少必要的参数或字符错误,该选项存储在optopt中,getopt返回'?'
extern int optreset;
int
getopt(int argc, char * const argv[], const char * optsting);
字符串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。
用于检测Unix/Linux中传递给main函数的参数列表,gnu给出的样例程序:
#include <ctype.h>
#include <unistd.h>
#include <stdbool.h>
...
int main (int argc, char **argv){
bool aflag = false, bflag = false;
char *cvalue = NULL;
opterr = 0;
int c;
while ((c = getopt (argc, argv, "abc:")) != -1)
switch (c){
case 'a': aflag = true;
break;
case 'b': bflag = true;
break;
case 'c': cvalue = optarg;
break;
case '?':
if (optopt == 'c')
fprintf (stderr, "Option -%c requires an argument./n", optopt);
else if (isprint (optopt))
fprintf (stderr, "Unknown option `-%c'./n", optopt);
else
fprintf (stderr,
"Unknown option character `//x%x'./n",
optopt);
return 1;
default:
abort ();
}
printf ("aflag = %d, bflag = %d, cvalue = %s/n",
aflag, bflag, cvalue);
int index;
for (index = optind; index < argc; index++)
printf ("Non-option argument %s/n", argv[index]);
return 0;
}
测试运行结果:
$testopt
aflag = 0, bflag = 0, cvalue = (null)
$testopt -a -b
aflag = 1, bflag = 1, cvalue = (null)
$testopt -ab
aflag = 1, bflag = 1, cvalue = (null)
$testopt -c foo
aflag = 0, bflag = 0, cvalue = foo
$testopt -cfoo
aflag = 0, bflag = 0, cvalue = foo
$testopt arg1
aflag = 0, bflag = 0, cvalue = (null)
Non-option argument arg1
$testopt -a arg1
aflag = 1, bflag = 0, cvalue = (null)
Non-option argument arg1
$testopt -c foo arg1
aflag = 0, bflag = 0, cvalue = foo
Non-option argument arg1
$testopt -a -- -b
aflag = 1, bflag = 0, cvalue = (null)
Non-option argument -b
$testopt -a -
aflag = 1, bflag = 0, cvalue = (null)
Non-option argument -