>From a882a0bdf4b2d19b25f42bb2d4194b526dde8589 Mon Sep 17 00:00:00 2001 From: Bernhard Voelker Date: Thu, 29 Nov 2018 09:06:26 +0100 Subject: [PATCH] long-options: add parse_gnu_standard_options_only * 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 (see also [1]), and fail for any other unknown long or short option. * lib/long-options.h (parse_gnu_standard_options_only): Declare it. [1] https://gnu.org/prep/standards/html_node/Command_002dLine-Interfaces.html --- ChangeLog | 12 +++++++++ lib/long-options.c | 58 +++++++++++++++++++++++++++++++++++++++++++- lib/long-options.h | 16 ++++++++++++ modules/long-options | 1 + 4 files changed, 86 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index af7a14fa9..3b25962b6 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,15 @@ +2018-11-29 Bernhard Voelker + + long-options: add parse_gnu_standard_options_only + * 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 (see also [1]), + and fail for any other unknown long or short option. + * lib/long-options.h (parse_gnu_standard_options_only): Declare it. + [1] + https://gnu.org/prep/standards/html_node/Command_002dLine-Interfaces.html + 2018-11-29 Akim Demaille bitset: rename ebitset/expandable.* as tbitset/table.* diff --git a/lib/long-options.c b/lib/long-options.c index eef1f2f35..b3f1df404 100644 --- a/lib/long-options.c +++ b/lib/long-options.c @@ -71,7 +71,7 @@ parse_long_options (int argc, va_list authors; va_start (authors, usage_func); version_etc_va (stdout, command_name, package, version, authors); - exit (0); + exit (EXIT_SUCCESS); } default: @@ -87,3 +87,59 @@ parse_long_options (int argc, the probably-new parameters when/if getopt is called later. */ optind = 0; } + +/* Process the GNU default long options --help and --version (see also + https://gnu.org/prep/standards/html_node/Command_002dLine-Interfaces.html), + and fail for any other unknown long or short option. + Use with SCAN_ALL=true to scan until "--", or with SCAN_ALL=false to stop + at the first non-option argument (or "--", whichever comes first). */ + +void +parse_gnu_standard_options_only (int argc, + char **argv, + const char *command_name, + const char *package, + const char *version, + bool scan_all, + void (*usage_func) (int), + /* const char *author1, ...*/ ...) +{ + int c; + int saved_opterr; + + saved_opterr = opterr; + + /* Print an error message for unrecognized options. */ + opterr = 1; + + const char *optstring = scan_all ? "" : "+"; + + if ((c = getopt_long (argc, argv, optstring, long_options, NULL)) != -1) + { + switch (c) + { + case 'h': + (*usage_func) (EXIT_SUCCESS); + break; + + case 'v': + { + va_list authors; + va_start (authors, usage_func); + version_etc_va (stdout, command_name, package, version, authors); + exit (EXIT_SUCCESS); + } + + default: + (*usage_func) (EXIT_FAILURE); + break; + } + } + + /* Restore previous value. */ + opterr = saved_opterr; + + /* Reset this to zero so that getopt internals get initialized from + the probably-new parameters when/if getopt is called later. */ + optind = 0; +} diff --git a/lib/long-options.h b/lib/long-options.h index 109a20375..44d6aa6ab 100644 --- a/lib/long-options.h +++ b/lib/long-options.h @@ -17,6 +17,11 @@ /* Written by Jim Meyering. */ +#ifndef LONG_OPTIONS_H_ +# define LONG_OPTIONS_H_ 1 + +# include + void parse_long_options (int _argc, char **_argv, const char *_command_name, @@ -24,3 +29,14 @@ void parse_long_options (int _argc, const char *_version, void (*_usage) (int), /* const char *author1, ...*/ ...); + +void parse_gnu_standard_options_only (int argc, + char **argv, + const char *command_name, + const char *package, + const char *version, + bool scan_all, + void (*usage_func) (int), + /* const char *author1, ...*/ ...); + +#endif /* LONG_OPTIONS_H_ */ diff --git a/modules/long-options b/modules/long-options index f1106c3cc..9bec13e75 100644 --- a/modules/long-options +++ b/modules/long-options @@ -7,6 +7,7 @@ lib/long-options.c Depends-on: getopt-gnu +stdbool stdlib version-etc -- 2.19.1