/* Utility to accept --help and --version options as unobtrusively as possible.
Copyright (C) 1993, 1994, 1998, 1999, 2000, 2002, 2003, 2004, 2005,
2006 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
/* Written by Jim Meyering. */
//包含库里的头文件
#include
//包含本文件夹下的头文件,这个头文件是parse_long_options函数的声明
/* Specification. */
#include "long-options.h"
//再包含进来4个标准的头文件
#include
#include
#include
#include
//继续ing。。。
#include "version-etc.h"
//初始化结构体
static struct option const long_options[] =
{
{"help", no_argument, NULL, 'h'},
{"version", no_argument, NULL, 'v'},
{NULL, 0, NULL, 0}
};
这里贴一下从网上找到的中文解释(出自http://blog.chinaunix.net/u/12592/showart_429841.html):
struct options的定义如下:
struct option{
const char *name;
int has_arg;
int *flag;
int val;
};
对结构中的各元素解释如下:
const char *name
这是选项名,前面没有短横线。譬如"help"、"verbose"之类。
int has_arg
描述了选项是否有选项参数。如果有,是哪种类型的参数,此时,它的值一定是下表中的一个。
符号常量 数值 含义
no_argument 0 选项没有参数
required_argument 1 选项需要参数
optional_argument 2 选项参数可选
int *flag
如果这个指针为NULL,那么 getopt_long()返回该结构val字段中的数值。如果该指针不为NULL,getopt_long()会使得它所指向的变量中填入val字段中的数值,并且getopt_long()返回0。如果flag不是NULL,但未发现长选项,那么它所指向的变量的数值不变。
int val
这个值是发现了长选项时的返回值,或者flag不是NULL时载入*flag中的值。典型情况下,若flag不是NULL,那么val是个真/假值,譬如 1或0;另一方面,如果flag是NULL,那么 val通常是字符常量,若长选项与短选项一致,那么该字符常量应该与optstring中出现的这个选项的参数相同。
/* Process long options --help and --version, but only if argc == 2.
Be careful not to gobble up `--'. */
函数具体实现的地方:
void
parse_long_options (int argc,
char **argv,
const char *command_name,
const char *package,
const char *version,
void (*usage_func) (int),
/* const char *author1, ...*/ ...)
{
int c;//定义局部变量
int saved_opterr;
saved_opterr = opterr; 初始化saved_opterr
/* Don't print an error message for unrecognized options. */
opterr = 0;
if (argc == 2 如果传入的整型形参argc等于2并且
&& (c = getopt_long (argc, argv, "+", long_options(这里的这个结构体就是文件开头初始化的那个结构体了), NULL)) != -1)c的值是通过getopt_long得到的值
{
switch (c)根据getopt_long获得的c的值,选择应该进入哪个代码段执行程序
{
case 'h':如果是字符h,则调用注册的usage_func函数,传给这个函数的形参是EXIT_SUCCESS
(*usage_func) (EXIT_SUCCESS);
case 'v':如果是字符v,则打印下面列示的作者和帮助等信息,并返回0
{
va_list authors;
va_start (authors, usage_func);
version_etc_va (stdout, command_name, package, version, authors);
exit (0);
}
default:如果是其他值,则跳出switch语句块
/* Don't process any other long-named options. */
break;
}
}
/* Restore previous value. */
opterr = saved_opterr;opterr这时候得到的值是程序开始执行时的值
/* Reset this to zero so that getopt internals get initialized from
the probably-new parameters when/if getopt is called later. */
optind = 0;重新设置optind的值
}
这个解析函数早在第一个程序sync.c里面就见到了,当时没有太关注,今天粗略地看了看,日后再详细研究,关于命令行选项的问题,需要花时间专门看看。
阅读(1323) | 评论(0) | 转发(1) |