Mercurial > gnulib
annotate lib/long-options.c @ 18442:5a47f1f1de9b
long-options: avoid new GCC 7 warning from -Wimplicit-fallthrough
* lib/long-options.c (parse_long_options): Add a break statement
to avoid this new warning/failure:
$ CFLAGS='-O -Werror=implicit-fallthrough' ./gnulib-tool \
--create-testdir --dir=/t/x --with-tests --test long-options
../../gllib/long-options.c: In function ‘parse_long_options’:
../../gllib/long-options.c:66:12: error: this statement may \
fall through [-Werror=implicit-fallthrough]
(*usage_func) (EXIT_SUCCESS);
~^~~~~~~~~~~~~~~~~~~~~~~~~~~
author | Jim Meyering <meyering@fb.com> |
---|---|
date | Wed, 05 Oct 2016 10:43:09 -0700 |
parents | 31b2239ca59c |
children | 12df2165ec1c |
rev | line source |
---|---|
317 | 1 /* Utility to accept --help and --version options as unobtrusively as possible. |
4659 | 2 |
18189 | 3 Copyright (C) 1993-1994, 1998-2000, 2002-2006, 2009-2016 Free Software |
12518
b5e42ef33b49
update nearly all FSF copyright year lists to include 2009
Jim Meyering <meyering@redhat.com>
parents:
12421
diff
changeset
|
4 Foundation, Inc. |
317 | 5 |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
7302
diff
changeset
|
6 This program is free software: you can redistribute it and/or modify |
317 | 7 it under the terms of the GNU General Public License as published by |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
7302
diff
changeset
|
8 the Free Software Foundation; either version 3 of the License, or |
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
7302
diff
changeset
|
9 (at your option) any later version. |
317 | 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 | |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
7302
diff
changeset
|
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
317 | 18 |
649
89f4c1937ac7
update FSF address in copyright and remove any trailing blanks
Jim Meyering <jim@meyering.net>
parents:
446
diff
changeset
|
19 /* Written by Jim Meyering. */ |
317 | 20 |
7302
8a1a9361108c
* _fpending.c: Include <config.h> unconditionally, since we no
Paul Eggert <eggert@cs.ucla.edu>
parents:
6259
diff
changeset
|
21 #include <config.h> |
317 | 22 |
4771
ffc6ac581833
Merge version-etc-2 back into version-etc: Better way to internationalize
Bruno Haible <bruno@clisp.org>
parents:
4659
diff
changeset
|
23 /* Specification. */ |
4659 | 24 #include "long-options.h" |
25 | |
4771
ffc6ac581833
Merge version-etc-2 back into version-etc: Better way to internationalize
Bruno Haible <bruno@clisp.org>
parents:
4659
diff
changeset
|
26 #include <stdarg.h> |
317 | 27 #include <stdio.h> |
4771
ffc6ac581833
Merge version-etc-2 back into version-etc: Better way to internationalize
Bruno Haible <bruno@clisp.org>
parents:
4659
diff
changeset
|
28 #include <stdlib.h> |
317 | 29 #include <getopt.h> |
2669
3668079f961d
Include <stdlib.h>, for declaration of exit.
Jim Meyering <jim@meyering.net>
parents:
2507
diff
changeset
|
30 |
1755
f486d0e61ad0
(parse_long_options): Remove version-, copyright-,
Jim Meyering <jim@meyering.net>
parents:
1727
diff
changeset
|
31 #include "version-etc.h" |
317 | 32 |
33 static struct option const long_options[] = | |
34 { | |
5813 | 35 {"help", no_argument, NULL, 'h'}, |
36 {"version", no_argument, NULL, 'v'}, | |
37 {NULL, 0, NULL, 0} | |
317 | 38 }; |
39 | |
40 /* Process long options --help and --version, but only if argc == 2. | |
16235
18a38c9615f0
In commentary, do not use ` to quote.
Paul Eggert <eggert@cs.ucla.edu>
parents:
16201
diff
changeset
|
41 Be careful not to gobble up "--". */ |
317 | 42 |
43 void | |
1719 | 44 parse_long_options (int argc, |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
45 char **argv, |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
46 const char *command_name, |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
47 const char *package, |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
48 const char *version, |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
49 void (*usage_func) (int), |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
50 /* const char *author1, ...*/ ...) |
317 | 51 { |
52 int c; | |
53 int saved_opterr; | |
54 | |
55 saved_opterr = opterr; | |
56 | |
57 /* Don't print an error message for unrecognized options. */ | |
58 opterr = 0; | |
59 | |
60 if (argc == 2 | |
880
3bee11a1c21d
(parse_long_options): Compare getopt_long return
Jim Meyering <jim@meyering.net>
parents:
869
diff
changeset
|
61 && (c = getopt_long (argc, argv, "+", long_options, NULL)) != -1) |
317 | 62 { |
63 switch (c) | |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
64 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
65 case 'h': |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
66 (*usage_func) (EXIT_SUCCESS); |
18442
5a47f1f1de9b
long-options: avoid new GCC 7 warning from -Wimplicit-fallthrough
Jim Meyering <meyering@fb.com>
parents:
18189
diff
changeset
|
67 break; |
317 | 68 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
69 case 'v': |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
70 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
71 va_list authors; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
72 va_start (authors, usage_func); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
73 version_etc_va (stdout, command_name, package, version, authors); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
74 exit (0); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
75 } |
446
8461c3b8595a
Remove trailing white space.
Jim Meyering <jim@meyering.net>
parents:
317
diff
changeset
|
76 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
77 default: |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
78 /* Don't process any other long-named options. */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
79 break; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
80 } |
317 | 81 } |
82 | |
83 /* Restore previous value. */ | |
84 opterr = saved_opterr; | |
85 | |
869
ca8a87089389
(parse_long_options): Reset optind to zero
Jim Meyering <jim@meyering.net>
parents:
706
diff
changeset
|
86 /* 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
|
87 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
|
88 optind = 0; |
317 | 89 } |