Mercurial > gnulib
annotate lib/long-options.c @ 2669:3668079f961d
Include <stdlib.h>, for declaration of exit.
author | Jim Meyering <jim@meyering.net> |
---|---|
date | Thu, 29 Jun 2000 11:24:09 +0000 |
parents | 702adf3147be |
children | 22d3032f0239 |
rev | line source |
---|---|
317 | 1 /* Utility to accept --help and --version options as unobtrusively as possible. |
2507 | 2 Copyright (C) 1993, 1994, 1998, 1999, 2000 Free Software Foundation, Inc. |
317 | 3 |
4 This program is free software; you can redistribute it and/or modify | |
5 it under the terms of the GNU General Public License as published by | |
6 the Free Software Foundation; either version 2, or (at your option) | |
7 any later version. | |
8 | |
9 This program is distributed in the hope that it will be useful, | |
10 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 GNU General Public License for more details. | |
13 | |
14 You should have received a copy of the GNU General Public License | |
649
89f4c1937ac7
update FSF address in copyright and remove any trailing blanks
Jim Meyering <jim@meyering.net>
parents:
446
diff
changeset
|
15 along with this program; if not, write to the Free Software Foundation, |
89f4c1937ac7
update FSF address in copyright and remove any trailing blanks
Jim Meyering <jim@meyering.net>
parents:
446
diff
changeset
|
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
317 | 17 |
649
89f4c1937ac7
update FSF address in copyright and remove any trailing blanks
Jim Meyering <jim@meyering.net>
parents:
446
diff
changeset
|
18 /* Written by Jim Meyering. */ |
317 | 19 |
1278
9ad625a30b7d
Use #if, not #ifdef with HAVE_ macros
Jim Meyering <jim@meyering.net>
parents:
1199
diff
changeset
|
20 #if HAVE_CONFIG_H |
1199
e7da0dec4cd2
(parse_long_options): Check for write error to stdout before exiting.
Jim Meyering <jim@meyering.net>
parents:
880
diff
changeset
|
21 # include <config.h> |
317 | 22 #endif |
23 | |
24 #include <stdio.h> | |
25 #include <getopt.h> | |
2669
3668079f961d
Include <stdlib.h>, for declaration of exit.
Jim Meyering <jim@meyering.net>
parents:
2507
diff
changeset
|
26 #if HAVE_STDLIB_H |
3668079f961d
Include <stdlib.h>, for declaration of exit.
Jim Meyering <jim@meyering.net>
parents:
2507
diff
changeset
|
27 # include <stdlib.h> |
3668079f961d
Include <stdlib.h>, for declaration of exit.
Jim Meyering <jim@meyering.net>
parents:
2507
diff
changeset
|
28 #endif |
3668079f961d
Include <stdlib.h>, for declaration of exit.
Jim Meyering <jim@meyering.net>
parents:
2507
diff
changeset
|
29 |
317 | 30 #include "long-options.h" |
1755
f486d0e61ad0
(parse_long_options): Remove version-, copyright-,
Jim Meyering <jim@meyering.net>
parents:
1727
diff
changeset
|
31 #include "version-etc.h" |
317 | 32 |
1719 | 33 #if ENABLE_NLS |
34 # include <libintl.h> | |
35 # define _(Text) gettext (Text) | |
36 #else | |
37 # define _(Text) Text | |
38 #endif | |
39 | |
317 | 40 static struct option const long_options[] = |
41 { | |
42 {"help", no_argument, 0, 'h'}, | |
43 {"version", no_argument, 0, 'v'}, | |
44 {0, 0, 0, 0} | |
45 }; | |
46 | |
47 /* Process long options --help and --version, but only if argc == 2. | |
48 Be careful not to gobble up `--'. */ | |
49 | |
50 void | |
1719 | 51 parse_long_options (int argc, |
52 char **argv, | |
53 const char *command_name, | |
54 const char *package, | |
55 const char *version, | |
56 const char *authors, | |
1643
07db14dfcbca
(parse_long_options): Rename `usage' parameter
Jim Meyering <jim@meyering.net>
parents:
1278
diff
changeset
|
57 void (*usage_func)()) |
317 | 58 { |
59 int c; | |
60 int saved_opterr; | |
61 | |
62 saved_opterr = opterr; | |
63 | |
64 /* Don't print an error message for unrecognized options. */ | |
65 opterr = 0; | |
66 | |
67 if (argc == 2 | |
880
3bee11a1c21d
(parse_long_options): Compare getopt_long return
Jim Meyering <jim@meyering.net>
parents:
869
diff
changeset
|
68 && (c = getopt_long (argc, argv, "+", long_options, NULL)) != -1) |
317 | 69 { |
70 switch (c) | |
71 { | |
72 case 'h': | |
1643
07db14dfcbca
(parse_long_options): Rename `usage' parameter
Jim Meyering <jim@meyering.net>
parents:
1278
diff
changeset
|
73 (*usage_func) (0); |
317 | 74 |
75 case 'v': | |
1755
f486d0e61ad0
(parse_long_options): Remove version-, copyright-,
Jim Meyering <jim@meyering.net>
parents:
1727
diff
changeset
|
76 version_etc (stdout, command_name, package, version, authors); |
317 | 77 exit (0); |
446
8461c3b8595a
Remove trailing white space.
Jim Meyering <jim@meyering.net>
parents:
317
diff
changeset
|
78 |
317 | 79 default: |
80 /* Don't process any other long-named options. */ | |
81 break; | |
82 } | |
83 } | |
84 | |
85 /* Restore previous value. */ | |
86 opterr = saved_opterr; | |
87 | |
869
ca8a87089389
(parse_long_options): Reset optind to zero
Jim Meyering <jim@meyering.net>
parents:
706
diff
changeset
|
88 /* Reset this to zero so that getopt internals get initialized from |
ca8a87089389
(parse_long_options): Reset optind to zero
Jim Meyering <jim@meyering.net>
parents:
706
diff
changeset
|
89 the probably-new parameters when/if getopt is called later. */ |
ca8a87089389
(parse_long_options): Reset optind to zero
Jim Meyering <jim@meyering.net>
parents:
706
diff
changeset
|
90 optind = 0; |
317 | 91 } |