comparison lib/long-options.c @ 40198:5a34193cbc07

long-options: add parse_gnu_standard_options_only Discussed in https://bugs.gnu.org/33468 . * lib/long-options.c (parse_long_options): Use EXIT_SUCCESS instead of 0. (parse_gnu_standard_options_only): Add function to process the GNU default options --help and --version and fail for any other unknown long or short option. See https://gnu.org/prep/standards/html_node/Command_002dLine-Interfaces.html . * lib/long-options.h (parse_gnu_standard_options_only): Declare it. * modules/long-options (depends-on): Add stdbool, exitfail. * top/maint.mk (sc_prohibit_long_options_without_use): Update syntax-check rule, add new function name.
author Bernhard Voelker <mail@bernhard-voelker.de>
date Thu, 29 Nov 2018 09:06:26 +0100
parents b06060465f09
children
comparison
equal deleted inserted replaced
40197:91454190d749 40198:5a34193cbc07
27 #include <stdio.h> 27 #include <stdio.h>
28 #include <stdlib.h> 28 #include <stdlib.h>
29 #include <getopt.h> 29 #include <getopt.h>
30 30
31 #include "version-etc.h" 31 #include "version-etc.h"
32 #include "exitfail.h"
32 33
33 static struct option const long_options[] = 34 static struct option const long_options[] =
34 { 35 {
35 {"help", no_argument, NULL, 'h'}, 36 {"help", no_argument, NULL, 'h'},
36 {"version", no_argument, NULL, 'v'}, 37 {"version", no_argument, NULL, 'v'},
69 case 'v': 70 case 'v':
70 { 71 {
71 va_list authors; 72 va_list authors;
72 va_start (authors, usage_func); 73 va_start (authors, usage_func);
73 version_etc_va (stdout, command_name, package, version, authors); 74 version_etc_va (stdout, command_name, package, version, authors);
74 exit (0); 75 exit (EXIT_SUCCESS);
75 } 76 }
76 77
77 default: 78 default:
78 /* Don't process any other long-named options. */ 79 /* Don't process any other long-named options. */
79 break; 80 break;
85 86
86 /* Reset this to zero so that getopt internals get initialized from 87 /* Reset this to zero so that getopt internals get initialized from
87 the probably-new parameters when/if getopt is called later. */ 88 the probably-new parameters when/if getopt is called later. */
88 optind = 0; 89 optind = 0;
89 } 90 }
91
92 /* Process the GNU default long options --help and --version (see also
93 https://gnu.org/prep/standards/html_node/Command_002dLine-Interfaces.html),
94 and fail for any other unknown long or short option.
95 Use with SCAN_ALL=true to scan until "--", or with SCAN_ALL=false to stop
96 at the first non-option argument (or "--", whichever comes first). */
97 void
98 parse_gnu_standard_options_only (int argc,
99 char **argv,
100 const char *command_name,
101 const char *package,
102 const char *version,
103 bool scan_all,
104 void (*usage_func) (int),
105 /* const char *author1, ...*/ ...)
106 {
107 int c;
108 int saved_opterr = opterr;
109
110 /* Print an error message for unrecognized options. */
111 opterr = 1;
112
113 const char *optstring = scan_all ? "" : "+";
114
115 if ((c = getopt_long (argc, argv, optstring, long_options, NULL)) != -1)
116 {
117 switch (c)
118 {
119 case 'h':
120 (*usage_func) (EXIT_SUCCESS);
121 break;
122
123 case 'v':
124 {
125 va_list authors;
126 va_start (authors, usage_func);
127 version_etc_va (stdout, command_name, package, version, authors);
128 exit (EXIT_SUCCESS);
129 }
130
131 default:
132 (*usage_func) (exit_failure);
133 break;
134 }
135 }
136
137 /* Restore previous value. */
138 opterr = saved_opterr;
139 }