1 /* Utility to accept --help and --version options as unobtrusively as possible.
2
3 Copyright (C) 1993, 1994, 1998, 1999, 2000, 2002, 2003, 2004, 2005,
4 2006 Free Software Foundation, Inc.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software Foundation,
18 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
19
20 /* Written by Jim Meyering. */
21
22 #include <config.h>
23
24 /* Specification. */
25 #include "long-options.h"
26
27 #include <stdarg.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <getopt.h>
31
32 #include "version-etc.h"
33
34 static struct option const long_options[] =
35 {
36 {"help", no_argument, NULL, 'h'},
37 {"version", no_argument, NULL, 'v'},
38 {NULL, 0, NULL, 0}
39 };
40
41 /* Process long options --help and --version, but only if argc == 2.
42 Be careful not to gobble up `--'. */
43
44 void
parse_long_options(int argc,char ** argv,const char * command_name,const char * package,const char * version,void (* usage_func)(int),...)45 parse_long_options (int argc,
46 char **argv,
47 const char *command_name,
48 const char *package,
49 const char *version,
50 void (*usage_func) (int),
51 /* const char *author1, ...*/ ...)
52 {
53 int c;
54 int saved_opterr;
55
56 saved_opterr = opterr;
57
58 /* Don't print an error message for unrecognized options. */
59 opterr = 0;
60
61 if (argc == 2
62 && (c = getopt_long (argc, argv, "+", long_options, NULL)) != -1)
63 {
64 switch (c)
65 {
66 case 'h':
67 (*usage_func) (EXIT_SUCCESS);
68
69 case 'v':
70 {
71 va_list authors;
72 va_start (authors, usage_func);
73 version_etc_va (stdout, command_name, package, version, authors);
74 exit (0);
75 }
76
77 default:
78 /* Don't process any other long-named options. */
79 break;
80 }
81 }
82
83 /* Restore previous value. */
84 opterr = saved_opterr;
85
86 /* Reset this to zero so that getopt internals get initialized from
87 the probably-new parameters when/if getopt is called later. */
88 optind = 0;
89 }
90