1994-10-03 06:50:11 +08:00
|
|
|
/* Utility to accept --help and --version options as unobtrusively as possible.
|
2003-09-10 16:49:38 +08:00
|
|
|
|
2005-03-29 19:56:10 +08:00
|
|
|
Copyright (C) 1993, 1994, 1998, 1999, 2000, 2002, 2003, 2004, 2005 Free
|
2003-09-10 16:49:38 +08:00
|
|
|
Software Foundation, Inc.
|
1994-10-03 06:50:11 +08:00
|
|
|
|
|
|
|
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
|
1996-07-15 10:41:49 +08:00
|
|
|
along with this program; if not, write to the Free Software Foundation,
|
2005-05-14 15:58:06 +08:00
|
|
|
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
|
1994-10-03 06:50:11 +08:00
|
|
|
|
1996-07-15 10:41:49 +08:00
|
|
|
/* Written by Jim Meyering. */
|
1994-10-03 06:50:11 +08:00
|
|
|
|
2005-09-22 14:05:39 +08:00
|
|
|
#ifdef HAVE_CONFIG_H
|
1998-01-05 06:43:44 +08:00
|
|
|
# include <config.h>
|
1994-10-03 06:50:11 +08:00
|
|
|
#endif
|
|
|
|
|
2003-10-18 16:11:09 +08:00
|
|
|
/* Specification. */
|
2003-09-10 16:49:38 +08:00
|
|
|
#include "long-options.h"
|
|
|
|
|
2003-10-18 16:11:09 +08:00
|
|
|
#include <stdarg.h>
|
1994-10-03 06:50:11 +08:00
|
|
|
#include <stdio.h>
|
2003-09-19 03:39:12 +08:00
|
|
|
#include <stdlib.h>
|
2003-10-18 16:11:09 +08:00
|
|
|
#include <getopt.h>
|
2000-06-29 19:24:09 +08:00
|
|
|
|
1999-03-26 12:19:07 +08:00
|
|
|
#include "version-etc.h"
|
1994-10-03 06:50:11 +08:00
|
|
|
|
|
|
|
static struct option const long_options[] =
|
|
|
|
{
|
2005-03-29 19:56:10 +08:00
|
|
|
{"help", no_argument, NULL, 'h'},
|
|
|
|
{"version", no_argument, NULL, 'v'},
|
|
|
|
{NULL, 0, NULL, 0}
|
1994-10-03 06:50:11 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Process long options --help and --version, but only if argc == 2.
|
|
|
|
Be careful not to gobble up `--'. */
|
|
|
|
|
|
|
|
void
|
1999-03-04 13:08:01 +08:00
|
|
|
parse_long_options (int argc,
|
|
|
|
char **argv,
|
|
|
|
const char *command_name,
|
|
|
|
const char *package,
|
|
|
|
const char *version,
|
2004-01-22 06:35:55 +08:00
|
|
|
void (*usage_func) (int),
|
2003-10-18 16:11:09 +08:00
|
|
|
/* const char *author1, ...*/ ...)
|
1994-10-03 06:50:11 +08:00
|
|
|
{
|
|
|
|
int c;
|
|
|
|
int saved_opterr;
|
|
|
|
|
|
|
|
saved_opterr = opterr;
|
|
|
|
|
|
|
|
/* Don't print an error message for unrecognized options. */
|
|
|
|
opterr = 0;
|
|
|
|
|
|
|
|
if (argc == 2
|
1997-02-03 13:06:27 +08:00
|
|
|
&& (c = getopt_long (argc, argv, "+", long_options, NULL)) != -1)
|
1994-10-03 06:50:11 +08:00
|
|
|
{
|
|
|
|
switch (c)
|
|
|
|
{
|
|
|
|
case 'h':
|
2004-01-22 06:35:55 +08:00
|
|
|
(*usage_func) (EXIT_SUCCESS);
|
1994-10-03 06:50:11 +08:00
|
|
|
|
|
|
|
case 'v':
|
2003-10-18 16:11:09 +08:00
|
|
|
{
|
|
|
|
va_list authors;
|
|
|
|
va_start (authors, usage_func);
|
|
|
|
version_etc_va (stdout, command_name, package, version, authors);
|
|
|
|
exit (0);
|
|
|
|
}
|
1995-05-26 23:08:55 +08:00
|
|
|
|
1994-10-03 06:50:11 +08:00
|
|
|
default:
|
|
|
|
/* Don't process any other long-named options. */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Restore previous value. */
|
|
|
|
opterr = saved_opterr;
|
|
|
|
|
1997-01-25 13:37:15 +08:00
|
|
|
/* Reset this to zero so that getopt internals get initialized from
|
|
|
|
the probably-new parameters when/if getopt is called later. */
|
|
|
|
optind = 0;
|
1994-10-03 06:50:11 +08:00
|
|
|
}
|