Unix
程序格式如下:
getopt [-dmp] [-s name] -f name file [file ...]
a)d
,m
和p
是可选option
,在一个[]
中表示它们可以一起使用;
[-s name]
表示s
是一个带参数的可选option
;-f name
表示f
是一个带参数的必选option
;file [file ...]
表示程序还需要一个或多个命令行参数。getopt
函数原型如下:
#include <unistd.h>
int getopt(int argc, char * const argv[], const char *optstring);
extern char *optarg;
extern int optind, opterr, optopt;
需要注意以下几点:
getopt
后,如果option
带参数,optarg
指向后面跟着的参数;optind
则表示下一次处理option
的index
。因此当getopt
解析完所有option
后,如果同argc
相同,则表示没有命令行参数。getopt
前两个参数直接从main
函数参数得到,第三个参数指定如何处理option
:"df:mps:"
。冒号表示前面的option
后面需要带参数。如果getopt
解析option
时遇到不在optstring
中的option
返回?
,把option
全部解析完返回-1
。getopt_long
和getopt_long_only
(参考getopt(3) – Linux man page):
#include <getopt.h>
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);
getopt_long
除了可以处理short option
外,还可以处理long option
(以--
开头)。关于struct option
定义如下:
struct option {
const char *name;
int has_arg;
int *flag;
int val;
};
The meanings of the different fields are:
name
is the name of the long option.
has_arg
is: no_argument (or 0) if the option does not take an argument; required_argument (or 1) if the option requires an argument; or optional_argument (or 2) if the option takes an optional argument.
flag
specifies how results are returned for a long option. If flag is NULL, then getopt_long() returns val. (For example, the calling program may set val to the equivalent short option character.) Otherwise, getopt_long() returns 0, and flag points to a variable which is set to val if the option is found, but left unchanged if the option is not found.
val
is the value to return, or to load into the variable pointed to by flag.
如果flag
是NULL
,getopt_long
会返回val
的值,因此通常会把flag
置成NULL
,把val
置成与long option
对应的short option
。否则getopt_long
会返回0
,并把val
的值赋给flag
。
参考下列代码(选自GNU binutils
中的size
命令)可以更好地了解getopt_long
:
#define OPTION_FORMAT (200)
#define OPTION_RADIX (OPTION_FORMAT + 1)
#define OPTION_TARGET (OPTION_RADIX + 1)
static struct option long_options[] =
{
{"common", no_argument, &show_common, 1},
{"format", required_argument, 0, OPTION_FORMAT},
{"radix", required_argument, 0, OPTION_RADIX},
{"target", required_argument, 0, OPTION_TARGET},
{"totals", no_argument, &show_totals, 1},
{"version", no_argument, &show_version, 1},
{"help", no_argument, &show_help, 1},
{0, no_argument, 0, 0}
};
while ((c = getopt_long (argc, argv, "ABHhVvdfotx", long_options,
(int *) 0)) != EOF)
switch (c)
{
case OPTION_FORMAT:
switch (*optarg)
{
case 'B':
case 'b':
berkeley_format = 1;
break;
case 'S':
case 's':
berkeley_format = 0;
break;
default:
non_fatal (_("invalid argument to --format: %s"), optarg);
usage (stderr, 1);
}
break;
......
case 0:
break;
......
}
{"format", required_argument, 0, OPTION_FORMAT}
的flag
是NULL
,所以getopt_long
返回值是OPTION_FORMAT
;根据optarg
确定应该使用哪种format
。而{"totals", no_argument, &show_totals, 1}
的flag
非NULL
,getopt_long
返回值是0
,show_totals
的值为1
。
getopt_long
和getopt_long_only
的区别:
getoptlongonly() is like getopt_long(), but ‘-‘ as well as “–” can indicate a long option. If an option that starts with ‘-‘ (not “–“) doesn’t match a long option, but does match a short option, it is parsed as a short option instead.