该数据结构中的每个元素对应了一个长选项,并且每个元素是由四个域组成。通常情况下,可以按以下规则使用。第一个元素,描述长选项的名称;第二个选项,代表该选项是否需要跟着参数,需要参数则为 1 ,反之为 0 ;第三个选项,可以赋为 NULL ;第四个选项,是该长选项对应的短选项名称。另外,数据结构的最后一个元素,要求所有域的内容均为 0 ,即 {NULL,0,NULL,0} 。下面举例说明,还是按照短选项为‘ -h ’‘ -o ’‘ -v ’的例子,该数据结构可以定义成如下形式:
const struct option long_options = {
{ “help”, 0, NULL, ‘h’ },
{ “output”, 1, NULL, ‘o’ },
{ “verbose”, 0, NULL, ‘v’ },
{ NULL, 0, NULL, 0 }
};
( 2 )使用 getopt_long() 的几条建议
2 调用方法
(a) 我们先看一下在函数库里, getopt_long() 函数是如何被声明的:
int getopt_long(int argc , char * const argv [] , const char *optstring , const struct option * longopts, int *longindex );
(b) 说明一下这五个变量吧。
前两个就不必多说了, main 函数的参数。第三个和第四个变量,分别是上面讲到的准备的两个数据结构。最后一个参数: longindex 参数一般赋为 NULL 即可;如果没有设置为 NULL ,那么它就指向一个变量,这个变量会被赋值为寻找到的长选项在 longopts 中的索引值,这可以用于错误诊断。
(c) 举例说明一下:
按以上所讲,参照( 1 )准备的两个数据结构,则调用方式可为:
getopt_long( argc, argv, short_options, long_options, NULL );
2 几种常见返回值
(a) 每次调用该函数,它都会分析一个选项,并且返回它的短选项,如果分析完毕,即已经没有选项了,则会返回 -1 。
(b) 如果 getopt_long() 在分析选项时,遇到一个没有定义过的选项,则返回值为‘ ? ’,此时,程序员可以打印出所定义命令行的使用信息给用户。
(c) 当处理一个带参数的选项时,全局变量 optarg 会指向它的参数
(d) 当函数分析完所有参数时,全局变量 optind ( into argv )会指向第一个‘非选项’的位置
2) 应用举例
/*===============================================================
* Code listing from "Advanced Linux Programming," by CodeSourcery LLC *
===============================================================*/
#include < getopt. h>
#include < stdio. h>
#include < stdlib. h>
/* The name of this program. * /
const char* program_name;
/* Prints usage information for this program to STREAM ( typically
stdout or stderr) , and exit the program with EXIT_CODE. Does not
return. * /
void print_usage ( FILE* stream, int exit_code)
{
fprintf ( stream, "Usage: %s options [ inputfile ... ]\n" , program_name) ;
fprintf ( stream,
" -h --help Display this usage information.\n"
" -o --output filename Write output to file.\n"
" -v --verbose Print verbose messages.\n" ) ;
exit ( exit_code) ;
}
/* Main program entry point. ARGC conains number of argument list
elements; ARGV is an array of pointers to them. * /
int main ( int argc, char* argv[ ] )
{
int next_option;
/* A string listing valid short options letters. * /
const char* const short_options = "ho:v" ;
/* An array describing valid long options. * /
const struct option long_options[ ] = {
{ "help" , 0, NULL, 'h' },
{ "output" , 1, NULL, 'o' },
{ "verbose" , 0, NULL, 'v' },
{ NULL, 0, NULL, 0 } /* Required at end of array. * /
};
/* The name of the file to receive program output, or NULL for
standard output. * /
const char* output_filename = NULL;
/* Whether to display verbose messages. * /
int verbose = 0;
/* Remember the name of the program, to incorporate in messages.
The name is stored in argv[ 0] . * /
program_name = argv[ 0] ;
do {
next_option = getopt_long ( argc, argv, short_options,
long_options, NULL) ;
switch ( next_option)
{
case 'h' : /* - h or - - help * /
/* User has requested usage information. Print it to standard
output, and exit with exit code zero ( normal termination) . * /
print_usage ( stdout, 0) ;
case 'o' : /* - o or - - output * /
/* This option takes an argument, the name of the output file. * /
output_filename = optarg;
break;
case 'v' : /* - v or - - verbose * /
verbose = 1;
break;
case '?' : /* The user specified an invalid option . * /
/* Print usage information to standard error, and exit with exit
code one ( indicating abonormal termination) . * /
print_usage ( stderr, 1) ;
case - 1: /* Done with options. * /
break;
default: /* Something else : unexpected. * /
abort ( ) ;
}
}
while ( next_option ! = - 1) ;
/* Done with options. OPTIND points to first non- option argument.
For demonstration purposes, print them if the verbose option was
specified. * /
if ( verbose) {
int i;
for ( i = optind; i < argc; + + i)
printf ( "Argument: %s\n" , argv[ i] ) ;
}
/* The main program goes here. * /
return 0;
}
