From 3703298b8b57813971407c0b27d7cc7688a488cd Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Sat, 28 Jun 2025 23:48:03 +0200 Subject: [PATCH] Refactor: Use gnulib's 'options' module. * autogen.sh (GNULIB_MODULES_RUNTIME_FOR_SRC, GNULIB_MODULES_TOOLS_FOR_SRC): Add 'options'. Remove 'getopt-gnu'. * gettext-runtime/src/envsubst.c: Include options.h instead of . (long_options): Remove variable. (main): Define options array here. Call start_options, get_next_option instead of getopt_long(). * gettext-runtime/src/gettext.c: Likewise. * gettext-runtime/src/ngettext.c: Likewise. * gettext-runtime/src/printf_gettext.c: Likewise. * gettext-runtime/src/printf_ngettext.c: Likewise. * gettext-tools/src/cldr-plurals.c: Likewise. * gettext-tools/src/hostname.c: Likewise. * gettext-tools/src/msgattrib.c: Likewise. * gettext-tools/src/msgcat.c: Likewise. * gettext-tools/src/msgcmp.c: Likewise. * gettext-tools/src/msgcomm.c: Likewise. * gettext-tools/src/msgconv.c: Likewise. * gettext-tools/src/msgen.c: Likewise. * gettext-tools/src/msgexec.c: Likewise. * gettext-tools/src/msgfilter.c: Likewise. * gettext-tools/src/msgfmt.c: Likewise. * gettext-tools/src/msggrep.c: Likewise. * gettext-tools/src/msginit.c: Likewise. * gettext-tools/src/msgmerge.c: Likewise. * gettext-tools/src/msgunfmt.c: Likewise. * gettext-tools/src/msguniq.c: Likewise. * gettext-tools/src/recode-sr-latin.c: Likewise. * gettext-tools/src/urlget.c: Likewise. * gettext-tools/src/xgettext.c: Likewise. * gettext-tools/tests/tstgettext.c: Likewise. * gettext-tools/tests/tstngettext.c: Likewise. --- autogen.sh | 4 +- gettext-runtime/src/envsubst.c | 27 +++-- gettext-runtime/src/gettext.c | 34 ++++--- gettext-runtime/src/ngettext.c | 31 +++--- gettext-runtime/src/printf_gettext.c | 22 ++-- gettext-runtime/src/printf_ngettext.c | 22 ++-- gettext-tools/src/cldr-plurals.c | 27 ++--- gettext-tools/src/hostname.c | 33 +++--- gettext-tools/src/msgattrib.c | 104 +++++++++---------- gettext-tools/src/msgcat.c | 84 ++++++++-------- gettext-tools/src/msgcmp.c | 41 ++++---- gettext-tools/src/msgcomm.c | 84 ++++++++-------- gettext-tools/src/msgconv.c | 73 +++++++------- gettext-tools/src/msgen.c | 73 +++++++------- gettext-tools/src/msgexec.c | 40 ++++---- gettext-tools/src/msgfilter.c | 81 +++++++-------- gettext-tools/src/msgfmt.c | 100 +++++++++---------- gettext-tools/src/msggrep.c | 97 +++++++++--------- gettext-tools/src/msginit.c | 51 +++++----- gettext-tools/src/msgmerge.c | 93 ++++++++--------- gettext-tools/src/msgunfmt.c | 71 +++++++------ gettext-tools/src/msguniq.c | 78 ++++++++------- gettext-tools/src/recode-sr-latin.c | 25 +++-- gettext-tools/src/urlget.c | 28 +++--- gettext-tools/src/xgettext.c | 138 +++++++++++++------------- gettext-tools/tests/tstgettext.c | 42 ++++---- gettext-tools/tests/tstngettext.c | 35 ++++--- 27 files changed, 775 insertions(+), 763 deletions(-) diff --git a/autogen.sh b/autogen.sh index 934c5d78f..0aa68224e 100755 --- a/autogen.sh +++ b/autogen.sh @@ -106,13 +106,13 @@ if ! $skip_gnulib; then closeout error fzprintf-posix - getopt-gnu gettext-h havelib mbrtoc32 mbszero memmove noreturn + options progname propername quote @@ -218,7 +218,6 @@ if ! $skip_gnulib; then gcd getaddrinfo getline - getopt-gnu getrusage gettext-h gocomp-script @@ -247,6 +246,7 @@ if ! $skip_gnulib; then open opendir openmp-init + options pipe-filter-ii progname propername diff --git a/gettext-runtime/src/envsubst.c b/gettext-runtime/src/envsubst.c index 596c22131..26201e899 100644 --- a/gettext-runtime/src/envsubst.c +++ b/gettext-runtime/src/envsubst.c @@ -20,7 +20,6 @@ #endif #include -#include #include #include #include @@ -29,6 +28,7 @@ #include #include +#include "options.h" #include "attribute.h" #include "noreturn.h" #include "closeout.h" @@ -46,15 +46,6 @@ /* If true, substitution shall be performed on all variables. */ static bool all_variables; -/* Long options. */ -static const struct option long_options[] = -{ - { "help", no_argument, NULL, 'h' }, - { "variables", no_argument, NULL, 'v' }, - { "version", no_argument, NULL, 'V' }, - { NULL, 0, NULL, 0 } -}; - /* Forward declaration of local functions. */ _GL_NORETURN_FUNC static void usage (int status); static void print_variables (const char *string); @@ -69,8 +60,6 @@ main (int argc, char *argv[]) bool do_help = false; bool do_version = false; - int opt; - /* Set program name for message texts. */ set_program_name (argv[0]); @@ -86,10 +75,20 @@ main (int argc, char *argv[]) atexit (close_stdout); /* Parse command line options. */ - while ((opt = getopt_long (argc, argv, "hvV", long_options, NULL)) != EOF) + BEGIN_ALLOW_OMITTING_FIELD_INITIALIZERS + static const struct program_option options[] = + { + { "help", 'h', no_argument }, + { "variables", 'v', no_argument }, + { "version", 'V', no_argument }, + }; + END_ALLOW_OMITTING_FIELD_INITIALIZERS + start_options (argc, argv, options, MOVE_OPTIONS_FIRST, 0); + int opt; + while ((opt = get_next_option ()) != -1) switch (opt) { - case '\0': /* Long option. */ + case '\0': /* Long option with key == 0. */ break; case 'h': do_help = true; diff --git a/gettext-runtime/src/gettext.c b/gettext-runtime/src/gettext.c index fa39ea892..6dfa78569 100644 --- a/gettext-runtime/src/gettext.c +++ b/gettext-runtime/src/gettext.c @@ -19,7 +19,6 @@ # include #endif -#include #include #include #include @@ -27,6 +26,7 @@ #include #include +#include "options.h" #include "attribute.h" #include "noreturn.h" #include "closeout.h" @@ -48,24 +48,12 @@ static bool inhibit_added_newline; message catalog. */ static bool do_expand; -/* Long options. */ -static const struct option long_options[] = -{ - { "context", required_argument, NULL, 'c' }, - { "domain", required_argument, NULL, 'd' }, - { "help", no_argument, NULL, 'h' }, - { "shell-script", no_argument, NULL, 's' }, - { "version", no_argument, NULL, 'V' }, - { NULL, 0, NULL, 0 } -}; - /* Forward declaration of local functions. */ _GL_NORETURN_FUNC static void usage (int status); int main (int argc, char *argv[]) { - int optchar; const char *msgid; /* Default values for command line options. */ @@ -93,11 +81,25 @@ main (int argc, char *argv[]) atexit (close_stdout); /* Parse command line options. */ - while ((optchar = getopt_long (argc, argv, "+c:d:eEhnsV", long_options, NULL)) - != EOF) + BEGIN_ALLOW_OMITTING_FIELD_INITIALIZERS + static const struct program_option options[] = + { + { "context", 'c', required_argument }, + { "domain", 'd', required_argument }, + { "help", 'h', no_argument }, + { "shell-script", 's', no_argument }, + { "version", 'V', no_argument }, + { NULL, 'e', no_argument }, + { NULL, 'E', no_argument }, + { NULL, 'n', no_argument }, + }; + END_ALLOW_OMITTING_FIELD_INITIALIZERS + start_options (argc, argv, options, NON_OPTION_TERMINATES_OPTIONS, 0); + int optchar; + while ((optchar = get_next_option ()) != -1) switch (optchar) { - case '\0': /* Long option. */ + case '\0': /* Long option with key == 0. */ break; case 'c': context = optarg; diff --git a/gettext-runtime/src/ngettext.c b/gettext-runtime/src/ngettext.c index 43dadad8d..2f7fe644a 100644 --- a/gettext-runtime/src/ngettext.c +++ b/gettext-runtime/src/ngettext.c @@ -18,7 +18,6 @@ # include #endif -#include #include #include #include @@ -28,6 +27,7 @@ #include #include +#include "options.h" #include "attribute.h" #include "noreturn.h" #include "closeout.h" @@ -45,23 +45,12 @@ message catalog. */ static int do_expand; -/* Long options. */ -static const struct option long_options[] = -{ - { "context", required_argument, NULL, 'c' }, - { "domain", required_argument, NULL, 'd' }, - { "help", no_argument, NULL, 'h' }, - { "version", no_argument, NULL, 'V' }, - { NULL, 0, NULL, 0 } -}; - /* Forward declaration of local functions. */ _GL_NORETURN_FUNC static void usage (int status); int main (int argc, char *argv[]) { - int optchar; const char *msgid; const char *msgid_plural; const char *count; @@ -90,11 +79,23 @@ main (int argc, char *argv[]) atexit (close_stdout); /* Parse command line options. */ - while ((optchar = getopt_long (argc, argv, "+c:d:eEhV", long_options, NULL)) - != EOF) + BEGIN_ALLOW_OMITTING_FIELD_INITIALIZERS + static const struct program_option options[] = + { + { "context", 'c', required_argument }, + { "domain", 'd', required_argument }, + { "help", 'h', no_argument }, + { "version", 'V', no_argument }, + { NULL, 'e', no_argument }, + { NULL, 'E', no_argument }, + }; + END_ALLOW_OMITTING_FIELD_INITIALIZERS + start_options (argc, argv, options, NON_OPTION_TERMINATES_OPTIONS, 0); + int optchar; + while ((optchar = get_next_option ()) != -1) switch (optchar) { - case '\0': /* Long option. */ + case '\0': /* Long option with key == 0. */ break; case 'c': context = optarg; diff --git a/gettext-runtime/src/printf_gettext.c b/gettext-runtime/src/printf_gettext.c index e92e06a74..3ef0a8977 100644 --- a/gettext-runtime/src/printf_gettext.c +++ b/gettext-runtime/src/printf_gettext.c @@ -26,13 +26,13 @@ # include #endif -#include #include #include #include #include #include +#include "options.h" #include "printf-command.h" #include "noreturn.h" #include "closeout.h" @@ -73,22 +73,20 @@ main (int argc, char *argv[]) /* Parse command line options. */ { - /* Long options. */ - static const struct option long_options[] = + BEGIN_ALLOW_OMITTING_FIELD_INITIALIZERS + static const struct program_option options[] = { - { "context", required_argument, NULL, 'c' }, - { "help", no_argument, NULL, 'h' }, - { "version", no_argument, NULL, 'V' }, - { NULL, 0, NULL, 0 } + { "context", 'c', required_argument }, + { "help", 'h', no_argument }, + { "version", 'V', no_argument }, }; - + END_ALLOW_OMITTING_FIELD_INITIALIZERS + start_options (argc, argv, options, NON_OPTION_TERMINATES_OPTIONS, 0); int optchar; - - while ((optchar = getopt_long (argc, argv, "+c:hV", long_options, NULL)) - != EOF) + while ((optchar = get_next_option ()) != -1) switch (optchar) { - case '\0': /* Long option. */ + case '\0': /* Long option with key == 0. */ break; case 'c': context = optarg; diff --git a/gettext-runtime/src/printf_ngettext.c b/gettext-runtime/src/printf_ngettext.c index 9511cb067..b8a2efe50 100644 --- a/gettext-runtime/src/printf_ngettext.c +++ b/gettext-runtime/src/printf_ngettext.c @@ -31,13 +31,13 @@ #include #include -#include #include #include #include #include #include +#include "options.h" #include "printf-command.h" #include "noreturn.h" #include "closeout.h" @@ -78,22 +78,20 @@ main (int argc, char *argv[]) /* Parse command line options. */ { - /* Long options. */ - static const struct option long_options[] = + BEGIN_ALLOW_OMITTING_FIELD_INITIALIZERS + static const struct program_option options[] = { - { "context", required_argument, NULL, 'c' }, - { "help", no_argument, NULL, 'h' }, - { "version", no_argument, NULL, 'V' }, - { NULL, 0, NULL, 0 } + { "context", 'c', required_argument }, + { "help", 'h', no_argument }, + { "version", 'V', no_argument }, }; - + END_ALLOW_OMITTING_FIELD_INITIALIZERS + start_options (argc, argv, options, NON_OPTION_TERMINATES_OPTIONS, 0); int optchar; - - while ((optchar = getopt_long (argc, argv, "+c:hV", long_options, NULL)) - != EOF) + while ((optchar = get_next_option ()) != -1) switch (optchar) { - case '\0': /* Long option. */ + case '\0': /* Long option with key == 0. */ break; case 'c': context = optarg; diff --git a/gettext-tools/src/cldr-plurals.c b/gettext-tools/src/cldr-plurals.c index 0dbb64c0e..03f02224d 100644 --- a/gettext-tools/src/cldr-plurals.c +++ b/gettext-tools/src/cldr-plurals.c @@ -26,7 +26,7 @@ #include "c-ctype.h" #include #include -#include +#include "options.h" #include "gettext.h" #include #include @@ -211,22 +211,12 @@ or by email to <%s>.\n"), exit (status); } -/* Long options. */ -static const struct option long_options[] = -{ - { "cldr", no_argument, NULL, 'c' }, - { "help", no_argument, NULL, 'h' }, - { "version", no_argument, NULL, 'V' }, - { NULL, 0, NULL, 0 } -}; - int main (int argc, char **argv) { bool opt_cldr_format = false; bool do_help = false; bool do_version = false; - int optchar; /* Set program name for messages. */ set_program_name (argv[0]); @@ -243,10 +233,21 @@ main (int argc, char **argv) /* Ensure that write errors on stdout are detected. */ atexit (close_stdout); - while ((optchar = getopt_long (argc, argv, "chV", long_options, NULL)) != EOF) + /* Parse command line options. */ + BEGIN_ALLOW_OMITTING_FIELD_INITIALIZERS + static const struct program_option options[] = + { + { "cldr", 'c', no_argument }, + { "help", 'h', no_argument }, + { "version", 'V', no_argument }, + }; + END_ALLOW_OMITTING_FIELD_INITIALIZERS + start_options (argc, argv, options, MOVE_OPTIONS_FIRST, 0); + int optchar; + while ((optchar = get_next_option ()) != -1) switch (optchar) { - case '\0': /* Long option. */ + case '\0': /* Long option with key == 0. */ break; case 'c': diff --git a/gettext-tools/src/hostname.c b/gettext-tools/src/hostname.c index bb3e59dac..98fe8baf9 100644 --- a/gettext-tools/src/hostname.c +++ b/gettext-tools/src/hostname.c @@ -21,7 +21,6 @@ #endif #include -#include #include #include #include @@ -96,6 +95,7 @@ #include #include +#include "options.h" #include "noreturn.h" #include "closeout.h" #include "error-progname.h" @@ -112,18 +112,6 @@ /* Output format. */ static enum { default_format, short_format, long_format, ip_format } format; -/* Long options. */ -static const struct option long_options[] = -{ - { "fqdn", no_argument, NULL, 'f' }, - { "help", no_argument, NULL, 'h' }, - { "ip-address", no_argument, NULL, 'i' }, - { "long", no_argument, NULL, 'f' }, - { "short", no_argument, NULL, 's' }, - { "version", no_argument, NULL, 'V' }, - { NULL, 0, NULL, 0 } -}; - /* Forward declaration of local functions. */ _GL_NORETURN_FUNC static void usage (int status); @@ -132,7 +120,6 @@ static void print_hostname (void); int main (int argc, char *argv[]) { - int optchar; bool do_help; bool do_version; @@ -157,11 +144,23 @@ main (int argc, char *argv[]) format = default_format; /* Parse command line options. */ - while ((optchar = getopt_long (argc, argv, "fhisV", long_options, NULL)) - != EOF) + BEGIN_ALLOW_OMITTING_FIELD_INITIALIZERS + static const struct program_option options[] = + { + { "fqdn", 'f', no_argument }, + { "help", 'h', no_argument }, + { "ip-address", 'i', no_argument }, + { "long", 'f', no_argument }, + { "short", 's', no_argument }, + { "version", 'V', no_argument }, + }; + END_ALLOW_OMITTING_FIELD_INITIALIZERS + start_options (argc, argv, options, MOVE_OPTIONS_FIRST, 0); + int optchar; + while ((optchar = get_next_option ()) != -1) switch (optchar) { - case '\0': /* Long option. */ + case '\0': /* Long option with key == 0. */ break; case 'f': format = long_format; diff --git a/gettext-tools/src/msgattrib.c b/gettext-tools/src/msgattrib.c index b85838360..0556af694 100644 --- a/gettext-tools/src/msgattrib.c +++ b/gettext-tools/src/msgattrib.c @@ -20,7 +20,6 @@ # include "config.h" #endif -#include #include #include #include @@ -29,6 +28,7 @@ #include #include +#include "options.h" #include "noreturn.h" #include "closeout.h" #include "dir-list.h" @@ -81,50 +81,6 @@ enum }; static int to_change; -/* Long options. */ -static const struct option long_options[] = -{ - { "add-location", optional_argument, NULL, 'n' }, - { "clear-fuzzy", no_argument, NULL, CHAR_MAX + 8 }, - { "clear-obsolete", no_argument, NULL, CHAR_MAX + 10 }, - { "clear-previous", no_argument, NULL, CHAR_MAX + 18 }, - { "empty", no_argument, NULL, CHAR_MAX + 23 }, - { "color", optional_argument, NULL, CHAR_MAX + 19 }, - { "directory", required_argument, NULL, 'D' }, - { "escape", no_argument, NULL, 'E' }, - { "force-po", no_argument, &force_po, 1 }, - { "fuzzy", no_argument, NULL, CHAR_MAX + 11 }, - { "help", no_argument, NULL, 'h' }, - { "ignore-file", required_argument, NULL, CHAR_MAX + 15 }, - { "indent", no_argument, NULL, 'i' }, - { "no-escape", no_argument, NULL, 'e' }, - { "no-fuzzy", no_argument, NULL, CHAR_MAX + 3 }, - { "no-location", no_argument, NULL, CHAR_MAX + 22 }, - { "no-obsolete", no_argument, NULL, CHAR_MAX + 5 }, - { "no-wrap", no_argument, NULL, CHAR_MAX + 13 }, - { "obsolete", no_argument, NULL, CHAR_MAX + 12 }, - { "only-file", required_argument, NULL, CHAR_MAX + 14 }, - { "only-fuzzy", no_argument, NULL, CHAR_MAX + 4 }, - { "only-obsolete", no_argument, NULL, CHAR_MAX + 6 }, - { "output-file", required_argument, NULL, 'o' }, - { "previous", no_argument, NULL, CHAR_MAX + 21 }, - { "properties-input", no_argument, NULL, 'P' }, - { "properties-output", no_argument, NULL, 'p' }, - { "set-fuzzy", no_argument, NULL, CHAR_MAX + 7 }, - { "set-obsolete", no_argument, NULL, CHAR_MAX + 9 }, - { "sort-by-file", no_argument, NULL, 'F' }, - { "sort-output", no_argument, NULL, 's' }, - { "stringtable-input", no_argument, NULL, CHAR_MAX + 16 }, - { "stringtable-output", no_argument, NULL, CHAR_MAX + 17 }, - { "strict", no_argument, NULL, 'S' }, - { "style", required_argument, NULL, CHAR_MAX + 20 }, - { "translated", no_argument, NULL, CHAR_MAX + 1 }, - { "untranslated", no_argument, NULL, CHAR_MAX + 2 }, - { "version", no_argument, NULL, 'V' }, - { "width", required_argument, NULL, 'w' }, - { NULL, 0, NULL, 0 } -}; - /* Forward declaration of local functions. */ _GL_NORETURN_FUNC static void usage (int status); @@ -136,7 +92,6 @@ static msgdomain_list_ty *process_msgdomain_list (msgdomain_list_ty *mdlp, int main (int argc, char **argv) { - int optchar; bool do_help; bool do_version; char *output_file; @@ -176,11 +131,57 @@ main (int argc, char **argv) only_file = NULL; ignore_file = NULL; - while ((optchar = getopt_long (argc, argv, "D:eEFhino:pPsVw:", long_options, - NULL)) != EOF) + /* Parse command line options. */ + BEGIN_ALLOW_OMITTING_FIELD_INITIALIZERS + static const struct program_option options[] = + { + { "add-location", CHAR_MAX + 'n', optional_argument }, + { NULL, 'n', no_argument }, + { "clear-fuzzy", CHAR_MAX + 8, no_argument }, + { "clear-obsolete", CHAR_MAX + 10, no_argument }, + { "clear-previous", CHAR_MAX + 18, no_argument }, + { "empty", CHAR_MAX + 23, no_argument }, + { "color", CHAR_MAX + 19, optional_argument }, + { "directory", 'D', required_argument }, + { "escape", 'E', no_argument }, + { "force-po", 0, no_argument, &force_po, 1 }, + { "fuzzy", CHAR_MAX + 11, no_argument }, + { "help", 'h', no_argument }, + { "ignore-file", CHAR_MAX + 15, required_argument }, + { "indent", 'i', no_argument }, + { "no-escape", 'e', no_argument }, + { "no-fuzzy", CHAR_MAX + 3, no_argument }, + { "no-location", CHAR_MAX + 22, no_argument }, + { "no-obsolete", CHAR_MAX + 5, no_argument }, + { "no-wrap", CHAR_MAX + 13, no_argument }, + { "obsolete", CHAR_MAX + 12, no_argument }, + { "only-file", CHAR_MAX + 14, required_argument }, + { "only-fuzzy", CHAR_MAX + 4, no_argument }, + { "only-obsolete", CHAR_MAX + 6, no_argument }, + { "output-file", 'o', required_argument }, + { "previous", CHAR_MAX + 21, no_argument }, + { "properties-input", 'P', no_argument }, + { "properties-output", 'p', no_argument }, + { "set-fuzzy", CHAR_MAX + 7, no_argument }, + { "set-obsolete", CHAR_MAX + 9, no_argument }, + { "sort-by-file", 'F', no_argument }, + { "sort-output", 's', no_argument }, + { "stringtable-input", CHAR_MAX + 16, no_argument }, + { "stringtable-output", CHAR_MAX + 17, no_argument }, + { "strict", CHAR_MAX + 24, no_argument }, + { "style", CHAR_MAX + 20, required_argument }, + { "translated", CHAR_MAX + 1, no_argument }, + { "untranslated", CHAR_MAX + 2, no_argument }, + { "version", 'V', no_argument }, + { "width", 'w', required_argument }, + }; + END_ALLOW_OMITTING_FIELD_INITIALIZERS + start_options (argc, argv, options, MOVE_OPTIONS_FIRST, 0); + int optchar; + while ((optchar = get_next_option ()) != -1) switch (optchar) { - case '\0': /* Long option. */ + case '\0': /* Long option with key == 0. */ break; case 'D': @@ -207,7 +208,8 @@ main (int argc, char **argv) message_print_style_indent (); break; - case 'n': + case 'n': /* -n */ + case CHAR_MAX + 'n': /* --add-location[={full|yes|file|never|no}] */ if (handle_filepos_comment_option (optarg)) usage (EXIT_FAILURE); break; @@ -228,7 +230,7 @@ main (int argc, char **argv) sort_by_msgid = true; break; - case 'S': + case CHAR_MAX + 24: /* --strict */ message_print_style_uniforum (); break; diff --git a/gettext-tools/src/msgcat.c b/gettext-tools/src/msgcat.c index 595493e6c..d438e99ea 100644 --- a/gettext-tools/src/msgcat.c +++ b/gettext-tools/src/msgcat.c @@ -20,7 +20,6 @@ # include "config.h" #endif -#include #include #include #include @@ -29,6 +28,7 @@ #include #include +#include "options.h" #include "noreturn.h" #include "closeout.h" #include "dir-list.h" @@ -62,40 +62,6 @@ static int force_po; /* Target encoding. */ static const char *to_code; -/* Long options. */ -static const struct option long_options[] = -{ - { "add-location", optional_argument, NULL, 'n' }, - { "color", optional_argument, NULL, CHAR_MAX + 5 }, - { "directory", required_argument, NULL, 'D' }, - { "escape", no_argument, NULL, 'E' }, - { "files-from", required_argument, NULL, 'f' }, - { "force-po", no_argument, &force_po, 1 }, - { "help", no_argument, NULL, 'h' }, - { "indent", no_argument, NULL, 'i' }, - { "lang", required_argument, NULL, CHAR_MAX + 7 }, - { "no-escape", no_argument, NULL, 'e' }, - { "no-location", no_argument, NULL, CHAR_MAX + 8 }, - { "no-wrap", no_argument, NULL, CHAR_MAX + 2 }, - { "output-file", required_argument, NULL, 'o' }, - { "properties-input", no_argument, NULL, 'P' }, - { "properties-output", no_argument, NULL, 'p' }, - { "sort-by-file", no_argument, NULL, 'F' }, - { "sort-output", no_argument, NULL, 's' }, - { "strict", no_argument, NULL, 'S' }, - { "stringtable-input", no_argument, NULL, CHAR_MAX + 3 }, - { "stringtable-output", no_argument, NULL, CHAR_MAX + 4 }, - { "style", required_argument, NULL, CHAR_MAX + 6 }, - { "to-code", required_argument, NULL, 't' }, - { "unique", no_argument, NULL, 'u' }, - { "use-first", no_argument, NULL, CHAR_MAX + 1 }, - { "version", no_argument, NULL, 'V' }, - { "width", required_argument, NULL, 'w' }, - { "more-than", required_argument, NULL, '>' }, - { "less-than", required_argument, NULL, '<' }, - { NULL, 0, NULL, 0 } -}; - /* Forward declaration of local functions. */ _GL_NORETURN_FUNC static void usage (int status); @@ -105,7 +71,6 @@ int main (int argc, char **argv) { int cnt; - int optchar; bool do_help; bool do_version; char *output_file; @@ -145,11 +110,47 @@ main (int argc, char **argv) less_than = INT_MAX; use_first = false; - while ((optchar = getopt_long (argc, argv, "<:>:D:eEf:Fhino:pPst:uVw:", - long_options, NULL)) != EOF) + /* Parse command line options. */ + BEGIN_ALLOW_OMITTING_FIELD_INITIALIZERS + static const struct program_option options[] = + { + { "add-location", CHAR_MAX + 'n', optional_argument }, + { NULL, 'n', no_argument }, + { "color", CHAR_MAX + 5, optional_argument }, + { "directory", 'D', required_argument }, + { "escape", 'E', no_argument }, + { "files-from", 'f', required_argument }, + { "force-po", 0, no_argument, &force_po, 1 }, + { "help", 'h', no_argument }, + { "indent", 'i', no_argument }, + { "lang", CHAR_MAX + 7, required_argument }, + { "no-escape", 'e', no_argument }, + { "no-location", CHAR_MAX + 8, no_argument }, + { "no-wrap", CHAR_MAX + 2, no_argument }, + { "output-file", 'o', required_argument }, + { "properties-input", 'P', no_argument }, + { "properties-output", 'p', no_argument }, + { "sort-by-file", 'F', no_argument }, + { "sort-output", 's', no_argument }, + { "strict", CHAR_MAX + 9, no_argument }, + { "stringtable-input", CHAR_MAX + 3, no_argument }, + { "stringtable-output", CHAR_MAX + 4, no_argument }, + { "style", CHAR_MAX + 6, required_argument }, + { "to-code", 't', required_argument }, + { "unique", 'u', no_argument }, + { "use-first", CHAR_MAX + 1, no_argument }, + { "version", 'V', no_argument }, + { "width", 'w', required_argument }, + { "more-than", '>', required_argument }, + { "less-than", '<', required_argument }, + }; + END_ALLOW_OMITTING_FIELD_INITIALIZERS + start_options (argc, argv, options, MOVE_OPTIONS_FIRST, 0); + int optchar; + while ((optchar = get_next_option ()) != -1) switch (optchar) { - case '\0': /* Long option. */ + case '\0': /* Long option with key == 0. */ break; case '>': @@ -200,7 +201,8 @@ main (int argc, char **argv) message_print_style_indent (); break; - case 'n': + case 'n': /* -n */ + case CHAR_MAX + 'n': /* --add-location[={full|yes|file|never|no}] */ if (handle_filepos_comment_option (optarg)) usage (EXIT_FAILURE); break; @@ -221,7 +223,7 @@ main (int argc, char **argv) sort_by_msgid = true; break; - case 'S': + case CHAR_MAX + 9: /* --strict */ message_print_style_uniforum (); break; diff --git a/gettext-tools/src/msgcmp.c b/gettext-tools/src/msgcmp.c index b0dd71f4a..e8bf4cbb5 100644 --- a/gettext-tools/src/msgcmp.c +++ b/gettext-tools/src/msgcmp.c @@ -19,7 +19,6 @@ # include #endif -#include #include #include #include @@ -27,6 +26,7 @@ #include #include +#include "options.h" #include "noreturn.h" #include "closeout.h" #include "dir-list.h" @@ -66,21 +66,6 @@ static bool include_fuzzies = false; /* Whether to consider untranslated messages as translations. */ static bool include_untranslated = false; -/* Long options. */ -static const struct option long_options[] = -{ - { "directory", required_argument, NULL, 'D' }, - { "help", no_argument, NULL, 'h' }, - { "multi-domain", no_argument, NULL, 'm' }, - { "no-fuzzy-matching", no_argument, NULL, 'N' }, - { "properties-input", no_argument, NULL, 'P' }, - { "stringtable-input", no_argument, NULL, CHAR_MAX + 1 }, - { "use-fuzzy", no_argument, NULL, CHAR_MAX + 2 }, - { "use-untranslated", no_argument, NULL, CHAR_MAX + 3 }, - { "version", no_argument, NULL, 'V' }, - { NULL, 0, NULL, 0 } -}; - /* Forward declaration of local functions. */ _GL_NORETURN_FUNC static void usage (int status); @@ -91,7 +76,6 @@ static void compare (const char *fn1, const char *fn2, int main (int argc, char *argv[]) { - int optchar; bool do_help; bool do_version; catalog_input_format_ty input_syntax = &input_format_po; @@ -115,11 +99,28 @@ main (int argc, char *argv[]) do_help = false; do_version = false; - while ((optchar = getopt_long (argc, argv, "D:hmNPV", long_options, NULL)) - != EOF) + + /* Parse command line options. */ + BEGIN_ALLOW_OMITTING_FIELD_INITIALIZERS + static const struct program_option options[] = + { + { "directory", 'D', required_argument }, + { "help", 'h', no_argument }, + { "multi-domain", 'm', no_argument }, + { "no-fuzzy-matching", 'N', no_argument }, + { "properties-input", 'P', no_argument }, + { "stringtable-input", CHAR_MAX + 1, no_argument }, + { "use-fuzzy", CHAR_MAX + 2, no_argument }, + { "use-untranslated", CHAR_MAX + 3, no_argument }, + { "version", 'V', no_argument }, + }; + END_ALLOW_OMITTING_FIELD_INITIALIZERS + start_options (argc, argv, options, MOVE_OPTIONS_FIRST, 0); + int optchar; + while ((optchar = get_next_option ()) != -1) switch (optchar) { - case '\0': /* long option */ + case '\0': /* Long option with key == 0. */ break; case 'D': diff --git a/gettext-tools/src/msgcomm.c b/gettext-tools/src/msgcomm.c index 114b0d6aa..666d34611 100644 --- a/gettext-tools/src/msgcomm.c +++ b/gettext-tools/src/msgcomm.c @@ -20,7 +20,6 @@ # include #endif -#include #include #include #include @@ -29,6 +28,7 @@ #include #include +#include "options.h" #include "noreturn.h" #include "closeout.h" #include "dir-list.h" @@ -63,40 +63,6 @@ static int force_po; /* Target encoding. */ static const char *to_code; -/* Long options. */ -static const struct option long_options[] = -{ - { "add-location", optional_argument, NULL, 'n' }, - { "color", optional_argument, NULL, CHAR_MAX + 5 }, - { "directory", required_argument, NULL, 'D' }, - { "escape", no_argument, NULL, 'E' }, - { "files-from", required_argument, NULL, 'f' }, - { "force-po", no_argument, &force_po, 1 }, - { "help", no_argument, NULL, 'h' }, - { "indent", no_argument, NULL, 'i' }, - { "no-escape", no_argument, NULL, 'e' }, - { "no-location", no_argument, NULL, CHAR_MAX + 7 }, - { "no-wrap", no_argument, NULL, CHAR_MAX + 2 }, - { "omit-header", no_argument, NULL, CHAR_MAX + 1 }, - { "output", required_argument, NULL, 'o' }, /* for backward compatibility */ - { "output-file", required_argument, NULL, 'o' }, - { "properties-input", no_argument, NULL, 'P' }, - { "properties-output", no_argument, NULL, 'p' }, - { "sort-by-file", no_argument, NULL, 'F' }, - { "sort-output", no_argument, NULL, 's' }, - { "strict", no_argument, NULL, 'S' }, - { "stringtable-input", no_argument, NULL, CHAR_MAX + 3 }, - { "stringtable-output", no_argument, NULL, CHAR_MAX + 4 }, - { "style", required_argument, NULL, CHAR_MAX + 6 }, - { "to-code", required_argument, NULL, 't' }, - { "unique", no_argument, NULL, 'u' }, - { "version", no_argument, NULL, 'V' }, - { "width", required_argument, NULL, 'w' }, - { "more-than", required_argument, NULL, '>' }, - { "less-than", required_argument, NULL, '<' }, - { NULL, 0, NULL, 0 } -}; - /* Forward declaration of local functions. */ _GL_NORETURN_FUNC static void usage (int status); @@ -106,7 +72,6 @@ int main (int argc, char *argv[]) { int cnt; - int optchar; bool do_help = false; bool do_version = false; msgdomain_list_ty *result; @@ -140,11 +105,47 @@ main (int argc, char *argv[]) less_than = -1; use_first = false; - while ((optchar = getopt_long (argc, argv, "<:>:D:eEf:Fhino:pPst:uVw:", - long_options, NULL)) != EOF) + /* Parse command line options. */ + BEGIN_ALLOW_OMITTING_FIELD_INITIALIZERS + static const struct program_option options[] = + { + { "add-location", CHAR_MAX + 'n', optional_argument }, + { NULL, 'n', no_argument }, + { "color", CHAR_MAX + 5, optional_argument }, + { "directory", 'D', required_argument }, + { "escape", 'E', no_argument }, + { "files-from", 'f', required_argument }, + { "force-po", 0, no_argument, &force_po, 1 }, + { "help", 'h', no_argument }, + { "indent", 'i', no_argument }, + { "no-escape", 'e', no_argument }, + { "no-location", CHAR_MAX + 7, no_argument }, + { "no-wrap", CHAR_MAX + 2, no_argument }, + { "omit-header", CHAR_MAX + 1, no_argument }, + { "output", 'o', required_argument }, /* for backward compatibility */ + { "output-file", 'o', required_argument }, + { "properties-input", 'P', no_argument }, + { "properties-output", 'p', no_argument }, + { "sort-by-file", 'F', no_argument }, + { "sort-output", 's', no_argument }, + { "strict", CHAR_MAX + 8, no_argument }, + { "stringtable-input", CHAR_MAX + 3, no_argument }, + { "stringtable-output", CHAR_MAX + 4, no_argument }, + { "style", CHAR_MAX + 6, required_argument }, + { "to-code", 't', required_argument }, + { "unique", 'u', no_argument }, + { "version", 'V', no_argument }, + { "width", 'w', required_argument }, + { "more-than", '>', required_argument }, + { "less-than", '<', required_argument }, + }; + END_ALLOW_OMITTING_FIELD_INITIALIZERS + start_options (argc, argv, options, MOVE_OPTIONS_FIRST, 0); + int optchar; + while ((optchar = get_next_option ()) != -1) switch (optchar) { - case '\0': /* Long option. */ + case '\0': /* Long option with key == 0. */ break; case '>': @@ -195,7 +196,8 @@ main (int argc, char *argv[]) message_print_style_indent (); break; - case 'n': + case 'n': /* -n */ + case CHAR_MAX + 'n': /* --add-location[={full|yes|file|never|no}] */ if (handle_filepos_comment_option (optarg)) usage (EXIT_FAILURE); break; @@ -216,7 +218,7 @@ main (int argc, char *argv[]) sort_by_msgid = true; break; - case 'S': + case CHAR_MAX + 8: /* --strict */ message_print_style_uniforum (); break; diff --git a/gettext-tools/src/msgconv.c b/gettext-tools/src/msgconv.c index b6dc494cf..f47a83c32 100644 --- a/gettext-tools/src/msgconv.c +++ b/gettext-tools/src/msgconv.c @@ -20,7 +20,6 @@ # include "config.h" #endif -#include #include #include #include @@ -29,6 +28,7 @@ #include #include +#include "options.h" #include "noreturn.h" #include "closeout.h" #include "dir-list.h" @@ -60,34 +60,6 @@ static int force_po; /* Target encoding. */ static const char *to_code; -/* Long options. */ -static const struct option long_options[] = -{ - { "add-location", optional_argument, NULL, 'n' }, - { "color", optional_argument, NULL, CHAR_MAX + 4 }, - { "directory", required_argument, NULL, 'D' }, - { "escape", no_argument, NULL, 'E' }, - { "force-po", no_argument, &force_po, 1 }, - { "help", no_argument, NULL, 'h' }, - { "indent", no_argument, NULL, 'i' }, - { "no-escape", no_argument, NULL, 'e' }, - { "no-location", no_argument, NULL, CHAR_MAX + 6 }, - { "no-wrap", no_argument, NULL, CHAR_MAX + 1 }, - { "output-file", required_argument, NULL, 'o' }, - { "properties-input", no_argument, NULL, 'P' }, - { "properties-output", no_argument, NULL, 'p' }, - { "sort-by-file", no_argument, NULL, 'F' }, - { "sort-output", no_argument, NULL, 's' }, - { "strict", no_argument, NULL, 'S' }, - { "stringtable-input", no_argument, NULL, CHAR_MAX + 2 }, - { "stringtable-output", no_argument, NULL, CHAR_MAX + 3 }, - { "style", required_argument, NULL, CHAR_MAX + 5 }, - { "to-code", required_argument, NULL, 't' }, - { "version", no_argument, NULL, 'V' }, - { "width", required_argument, NULL, 'w' }, - { NULL, 0, NULL, 0 } -}; - /* Forward declaration of local functions. */ _GL_NORETURN_FUNC static void usage (int status); @@ -96,7 +68,6 @@ _GL_NORETURN_FUNC static void usage (int status); int main (int argc, char **argv) { - int opt; bool do_help; bool do_version; char *output_file; @@ -130,12 +101,41 @@ main (int argc, char **argv) output_file = NULL; input_file = NULL; - while ((opt = getopt_long (argc, argv, "D:eEFhino:pPst:Vw:", long_options, - NULL)) - != EOF) + /* Parse command line options. */ + BEGIN_ALLOW_OMITTING_FIELD_INITIALIZERS + static const struct program_option options[] = + { + { "add-location", CHAR_MAX + 'n', optional_argument }, + { NULL, 'n', no_argument }, + { "color", CHAR_MAX + 4, optional_argument }, + { "directory", 'D', required_argument }, + { "escape", 'E', no_argument }, + { "force-po", 0, no_argument, &force_po, 1 }, + { "help", 'h', no_argument }, + { "indent", 'i', no_argument }, + { "no-escape", 'e', no_argument }, + { "no-location", CHAR_MAX + 6, no_argument }, + { "no-wrap", CHAR_MAX + 1, no_argument }, + { "output-file", 'o', required_argument }, + { "properties-input", 'P', no_argument }, + { "properties-output", 'p', no_argument }, + { "sort-by-file", 'F', no_argument }, + { "sort-output", 's', no_argument }, + { "strict", CHAR_MAX + 7, no_argument }, + { "stringtable-input", CHAR_MAX + 2, no_argument }, + { "stringtable-output", CHAR_MAX + 3, no_argument }, + { "style", CHAR_MAX + 5, required_argument }, + { "to-code", 't', required_argument }, + { "version", 'V', no_argument }, + { "width", 'w', required_argument }, + }; + END_ALLOW_OMITTING_FIELD_INITIALIZERS + start_options (argc, argv, options, MOVE_OPTIONS_FIRST, 0); + int opt; + while ((opt = get_next_option ()) != -1) switch (opt) { - case '\0': /* Long option. */ + case '\0': /* Long option with key == 0. */ break; case 'D': @@ -162,7 +162,8 @@ main (int argc, char **argv) message_print_style_indent (); break; - case 'n': + case 'n': /* -n */ + case CHAR_MAX + 'n': /* --add-location[={full|yes|file|never|no}] */ if (handle_filepos_comment_option (optarg)) usage (EXIT_FAILURE); break; @@ -183,7 +184,7 @@ main (int argc, char **argv) sort_by_msgid = true; break; - case 'S': + case CHAR_MAX + 7: /* --strict */ message_print_style_uniforum (); break; diff --git a/gettext-tools/src/msgen.c b/gettext-tools/src/msgen.c index f7fb82d00..f9d9b20a6 100644 --- a/gettext-tools/src/msgen.c +++ b/gettext-tools/src/msgen.c @@ -20,7 +20,6 @@ # include "config.h" #endif -#include #include #include #include @@ -29,6 +28,7 @@ #include #include +#include "options.h" #include "noreturn.h" #include "closeout.h" #include "dir-list.h" @@ -60,34 +60,6 @@ /* Force output of PO file even if empty. */ static int force_po; -/* Long options. */ -static const struct option long_options[] = -{ - { "add-location", optional_argument, NULL, 'n' }, - { "color", optional_argument, NULL, CHAR_MAX + 5 }, - { "directory", required_argument, NULL, 'D' }, - { "escape", no_argument, NULL, 'E' }, - { "force-po", no_argument, &force_po, 1 }, - { "help", no_argument, NULL, 'h' }, - { "indent", no_argument, NULL, 'i' }, - { "lang", required_argument, NULL, CHAR_MAX + 4 }, - { "no-escape", no_argument, NULL, 'e' }, - { "no-location", no_argument, NULL, CHAR_MAX + 7 }, - { "no-wrap", no_argument, NULL, CHAR_MAX + 1 }, - { "output-file", required_argument, NULL, 'o' }, - { "properties-input", no_argument, NULL, 'P' }, - { "properties-output", no_argument, NULL, 'p' }, - { "sort-by-file", no_argument, NULL, 'F' }, - { "sort-output", no_argument, NULL, 's' }, - { "strict", no_argument, NULL, 'S' }, - { "stringtable-input", no_argument, NULL, CHAR_MAX + 2 }, - { "stringtable-output", no_argument, NULL, CHAR_MAX + 3 }, - { "style", required_argument, NULL, CHAR_MAX + 6 }, - { "version", no_argument, NULL, 'V' }, - { "width", required_argument, NULL, 'w' }, - { NULL, 0, NULL, 0 } -}; - /* Forward declaration of local functions. */ _GL_NORETURN_FUNC static void usage (int status); @@ -97,7 +69,6 @@ static msgdomain_list_ty *fill_header (msgdomain_list_ty *mdlp); int main (int argc, char **argv) { - int opt; bool do_help; bool do_version; char *output_file; @@ -131,12 +102,41 @@ main (int argc, char **argv) do_version = false; output_file = NULL; - while ((opt = getopt_long (argc, argv, - "D:eEFhino:pPsVw:", - long_options, NULL)) != EOF) + /* Parse command line options. */ + BEGIN_ALLOW_OMITTING_FIELD_INITIALIZERS + static const struct program_option options[] = + { + { "add-location", CHAR_MAX + 'n', optional_argument }, + { NULL, 'n', no_argument }, + { "color", CHAR_MAX + 5, optional_argument }, + { "directory", 'D', required_argument }, + { "escape", 'E', no_argument }, + { "force-po", 0, no_argument, &force_po, 1 }, + { "help", 'h', no_argument }, + { "indent", 'i', no_argument }, + { "lang", CHAR_MAX + 4, required_argument }, + { "no-escape", 'e', no_argument }, + { "no-location", CHAR_MAX + 7, no_argument }, + { "no-wrap", CHAR_MAX + 1, no_argument }, + { "output-file", 'o', required_argument }, + { "properties-input", 'P', no_argument }, + { "properties-output", 'p', no_argument }, + { "sort-by-file", 'F', no_argument }, + { "sort-output", 's', no_argument }, + { "strict", CHAR_MAX + 8, no_argument }, + { "stringtable-input", CHAR_MAX + 2, no_argument }, + { "stringtable-output", CHAR_MAX + 3, no_argument }, + { "style", CHAR_MAX + 6, required_argument }, + { "version", 'V', no_argument }, + { "width", 'w', required_argument }, + }; + END_ALLOW_OMITTING_FIELD_INITIALIZERS + start_options (argc, argv, options, MOVE_OPTIONS_FIRST, 0); + int opt; + while ((opt = get_next_option ()) != -1) switch (opt) { - case '\0': /* Long option. */ + case '\0': /* Long option with key == 0. */ break; case 'D': @@ -163,7 +163,8 @@ main (int argc, char **argv) message_print_style_indent (); break; - case 'n': + case 'n': /* -n */ + case CHAR_MAX + 'n': /* --add-location[={full|yes|file|never|no}] */ if (handle_filepos_comment_option (optarg)) usage (EXIT_FAILURE); break; @@ -184,7 +185,7 @@ main (int argc, char **argv) sort_by_msgid = true; break; - case 'S': + case CHAR_MAX + 8: /* --strict */ message_print_style_uniforum (); break; diff --git a/gettext-tools/src/msgexec.c b/gettext-tools/src/msgexec.c index e7ab4263f..bc52c4234 100644 --- a/gettext-tools/src/msgexec.c +++ b/gettext-tools/src/msgexec.c @@ -21,7 +21,6 @@ #endif #include -#include #include #include #include @@ -32,6 +31,7 @@ #include #include +#include "options.h" #include "noreturn.h" #include "closeout.h" #include "dir-list.h" @@ -77,19 +77,6 @@ static bool newline; /* Maximum exit code encountered. */ static int exitcode; -/* Long options. */ -static const struct option long_options[] = -{ - { "directory", required_argument, NULL, 'D' }, - { "help", no_argument, NULL, 'h' }, - { "input", required_argument, NULL, 'i' }, - { "newline", no_argument, NULL, CHAR_MAX + 2 }, - { "properties-input", no_argument, NULL, 'P' }, - { "stringtable-input", no_argument, NULL, CHAR_MAX + 1 }, - { "version", no_argument, NULL, 'V' }, - { NULL, 0, NULL, 0 } -}; - /* Forward declaration of local functions. */ _GL_NORETURN_FUNC static void usage (int status); @@ -99,7 +86,6 @@ static void process_msgdomain_list (const msgdomain_list_ty *mdlp); int main (int argc, char **argv) { - int opt; bool do_help; bool do_version; const char *input_file; @@ -129,13 +115,27 @@ main (int argc, char **argv) do_version = false; input_file = NULL; - /* The '+' in the options string causes option parsing to terminate when - the first non-option, i.e. the subprogram name, is encountered. */ - while ((opt = getopt_long (argc, argv, "+D:hi:PV", long_options, NULL)) - != EOF) + /* Parse command line options. */ + BEGIN_ALLOW_OMITTING_FIELD_INITIALIZERS + static const struct program_option options[] = + { + { "directory", 'D', required_argument }, + { "help", 'h', no_argument }, + { "input", 'i', required_argument }, + { "newline", CHAR_MAX + 2, no_argument }, + { "properties-input", 'P', no_argument }, + { "stringtable-input", CHAR_MAX + 1, no_argument }, + { "version", 'V', no_argument }, + }; + END_ALLOW_OMITTING_FIELD_INITIALIZERS + /* The flag NON_OPTION_TERMINATES_OPTIONS causes option parsing to terminate + when the first non-option, i.e. the subprogram name, is encountered. */ + start_options (argc, argv, options, NON_OPTION_TERMINATES_OPTIONS, 0); + int opt; + while ((opt = get_next_option ()) != -1) switch (opt) { - case '\0': /* Long option. */ + case '\0': /* Long option with key == 0. */ break; case 'D': diff --git a/gettext-tools/src/msgfilter.c b/gettext-tools/src/msgfilter.c index 9d3ed88af..f7dfbb6d8 100644 --- a/gettext-tools/src/msgfilter.c +++ b/gettext-tools/src/msgfilter.c @@ -20,7 +20,6 @@ # include "config.h" #endif -#include #include #include #include @@ -33,6 +32,7 @@ #include #include +#include "options.h" #include "noreturn.h" #include "closeout.h" #include "dir-list.h" @@ -89,36 +89,6 @@ static bool newline; /* Filter function. */ static void (*filter) (const char *str, size_t len, char **resultp, size_t *lengthp); -/* Long options. */ -static const struct option long_options[] = -{ - { "add-location", optional_argument, NULL, 'n' }, - { "color", optional_argument, NULL, CHAR_MAX + 6 }, - { "directory", required_argument, NULL, 'D' }, - { "escape", no_argument, NULL, 'E' }, - { "force-po", no_argument, &force_po, 1 }, - { "help", no_argument, NULL, 'h' }, - { "indent", no_argument, NULL, CHAR_MAX + 1 }, - { "input", required_argument, NULL, 'i' }, - { "keep-header", no_argument, &keep_header, 1 }, - { "newline", no_argument, NULL, CHAR_MAX + 9 }, - { "no-escape", no_argument, NULL, CHAR_MAX + 2 }, - { "no-location", no_argument, NULL, CHAR_MAX + 8 }, - { "no-wrap", no_argument, NULL, CHAR_MAX + 3 }, - { "output-file", required_argument, NULL, 'o' }, - { "properties-input", no_argument, NULL, 'P' }, - { "properties-output", no_argument, NULL, 'p' }, - { "sort-by-file", no_argument, NULL, 'F' }, - { "sort-output", no_argument, NULL, 's' }, - { "strict", no_argument, NULL, 'S' }, - { "stringtable-input", no_argument, NULL, CHAR_MAX + 4 }, - { "stringtable-output", no_argument, NULL, CHAR_MAX + 5 }, - { "style", required_argument, NULL, CHAR_MAX + 7 }, - { "version", no_argument, NULL, 'V' }, - { "width", required_argument, NULL, 'w' }, - { NULL, 0, NULL, 0 } -}; - /* Forward declaration of local functions. */ _GL_NORETURN_FUNC static void usage (int status); @@ -129,7 +99,6 @@ static msgdomain_list_ty *process_msgdomain_list (msgdomain_list_ty *mdlp); int main (int argc, char **argv) { - int opt; bool do_help; bool do_version; char *output_file; @@ -164,14 +133,45 @@ main (int argc, char **argv) output_file = NULL; input_file = NULL; - /* The '+' in the options string causes option parsing to terminate when - the first non-option, i.e. the subprogram name, is encountered. */ - while ((opt = getopt_long (argc, argv, "+D:EFhi:no:pPsVw:", long_options, - NULL)) - != EOF) + /* Parse command line options. */ + BEGIN_ALLOW_OMITTING_FIELD_INITIALIZERS + static const struct program_option options[] = + { + { "add-location", CHAR_MAX + 'n', optional_argument }, + { NULL, 'n', no_argument }, + { "color", CHAR_MAX + 6, optional_argument }, + { "directory", 'D', required_argument }, + { "escape", 'E', no_argument }, + { "force-po", 0, no_argument, &force_po, 1 }, + { "help", 'h', no_argument }, + { "indent", CHAR_MAX + 1, no_argument }, + { "input", 'i', required_argument }, + { "keep-header", 0, no_argument, &keep_header, 1 }, + { "newline", CHAR_MAX + 9, no_argument }, + { "no-escape", CHAR_MAX + 2, no_argument }, + { "no-location", CHAR_MAX + 8, no_argument }, + { "no-wrap", CHAR_MAX + 3, no_argument }, + { "output-file", 'o', required_argument }, + { "properties-input", 'P', no_argument }, + { "properties-output", 'p', no_argument }, + { "sort-by-file", 'F', no_argument }, + { "sort-output", 's', no_argument }, + { "strict", CHAR_MAX + 10, no_argument }, + { "stringtable-input", CHAR_MAX + 4, no_argument }, + { "stringtable-output", CHAR_MAX + 5, no_argument }, + { "style", CHAR_MAX + 7, required_argument }, + { "version", 'V', no_argument }, + { "width", 'w', required_argument }, + }; + END_ALLOW_OMITTING_FIELD_INITIALIZERS + /* The flag NON_OPTION_TERMINATES_OPTIONS causes option parsing to terminate + when the first non-option, i.e. the subprogram name, is encountered. */ + start_options (argc, argv, options, NON_OPTION_TERMINATES_OPTIONS, 0); + int opt; + while ((opt = get_next_option ()) != -1) switch (opt) { - case '\0': /* Long option. */ + case '\0': /* Long option with key == 0. */ break; case 'D': @@ -199,7 +199,8 @@ main (int argc, char **argv) input_file = optarg; break; - case 'n': + case 'n': /* -n */ + case CHAR_MAX + 'n': /* --add-location[={full|yes|file|never|no}] */ if (handle_filepos_comment_option (optarg)) usage (EXIT_FAILURE); break; @@ -220,7 +221,7 @@ main (int argc, char **argv) sort_by_msgid = true; break; - case 'S': + case CHAR_MAX + 10: /* --strict */ message_print_style_uniforum (); break; diff --git a/gettext-tools/src/msgfmt.c b/gettext-tools/src/msgfmt.c index 39008c793..863276e6f 100644 --- a/gettext-tools/src/msgfmt.c +++ b/gettext-tools/src/msgfmt.c @@ -20,7 +20,6 @@ #endif #include -#include #include #include #include @@ -31,6 +30,7 @@ #include #include +#include "options.h" #include "noreturn.h" #include "closeout.h" #include "str-list.h" @@ -178,49 +178,6 @@ static int msgs_fuzzy; /* If not zero print statistics about translation at the end. */ static int do_statistics; -/* Long options. */ -static const struct option long_options[] = -{ - { "alignment", required_argument, NULL, 'a' }, - { "check", no_argument, NULL, 'c' }, - { "check-accelerators", optional_argument, NULL, CHAR_MAX + 1 }, - { "check-compatibility", no_argument, NULL, 'C' }, - { "check-domain", no_argument, NULL, CHAR_MAX + 2 }, - { "check-format", no_argument, NULL, CHAR_MAX + 3 }, - { "check-header", no_argument, NULL, CHAR_MAX + 4 }, - { "csharp", no_argument, NULL, CHAR_MAX + 10 }, - { "csharp-resources", no_argument, NULL, CHAR_MAX + 11 }, - { "desktop", no_argument, NULL, CHAR_MAX + 15 }, - { "directory", required_argument, NULL, 'D' }, - { "endianness", required_argument, NULL, CHAR_MAX + 13 }, - { "help", no_argument, NULL, 'h' }, - { "java", no_argument, NULL, 'j' }, - { "java2", no_argument, NULL, CHAR_MAX + 5 }, - { "keyword", optional_argument, NULL, 'k' }, - { "language", required_argument, NULL, 'L' }, - { "locale", required_argument, NULL, 'l' }, - { "no-convert", no_argument, NULL, CHAR_MAX + 17 }, - { "no-hash", no_argument, NULL, CHAR_MAX + 6 }, - { "no-redundancy", no_argument, NULL, CHAR_MAX + 18 }, - { "output-file", required_argument, NULL, 'o' }, - { "properties-input", no_argument, NULL, 'P' }, - { "qt", no_argument, NULL, CHAR_MAX + 9 }, - { "replace-text", no_argument, NULL, CHAR_MAX + 19 }, - { "resource", required_argument, NULL, 'r' }, - { "source", no_argument, NULL, CHAR_MAX + 14 }, - { "statistics", no_argument, &do_statistics, 1 }, - { "strict", no_argument, NULL, 'S' }, - { "stringtable-input", no_argument, NULL, CHAR_MAX + 8 }, - { "tcl", no_argument, NULL, CHAR_MAX + 7 }, - { "template", required_argument, NULL, CHAR_MAX + 16 }, - { "use-fuzzy", no_argument, NULL, 'f' }, - { "use-untranslated", no_argument, NULL, CHAR_MAX + 12 }, - { "verbose", no_argument, NULL, 'v' }, - { "version", no_argument, NULL, 'V' }, - { "xml", no_argument, NULL, 'x' }, - { NULL, 0, NULL, 0 } -}; - /* Forward declaration of local functions. */ _GL_NORETURN_FUNC static void usage (int status); @@ -242,7 +199,6 @@ static int msgfmt_xml_bulk (const char *directory, int main (int argc, char *argv[]) { - int opt; bool do_help = false; bool do_version = false; bool strict_uniforum = false; @@ -274,12 +230,56 @@ main (int argc, char *argv[]) /* Ensure that write errors on stdout are detected. */ atexit (close_stdout); - while ((opt = getopt_long (argc, argv, "a:cCd:D:fhjk::l:L:o:Pr:vVx", - long_options, NULL)) - != EOF) + /* Parse command line options. */ + BEGIN_ALLOW_OMITTING_FIELD_INITIALIZERS + static const struct program_option options[] = + { + { "alignment", 'a', required_argument }, + { "check", 'c', no_argument }, + { "check-accelerators", CHAR_MAX + 1, optional_argument }, + { "check-compatibility", 'C', no_argument }, + { "check-domain", CHAR_MAX + 2, no_argument }, + { "check-format", CHAR_MAX + 3, no_argument }, + { "check-header", CHAR_MAX + 4, no_argument }, + { "csharp", CHAR_MAX + 10, no_argument }, + { "csharp-resources", CHAR_MAX + 11, no_argument }, + { "desktop", CHAR_MAX + 15, no_argument }, + { "directory", 'D', required_argument }, + { "endianness", CHAR_MAX + 13, required_argument }, + { "help", 'h', no_argument }, + { "java", 'j', no_argument }, + { "java2", CHAR_MAX + 5, no_argument }, + { "keyword", 'k', optional_argument }, + { "language", 'L', required_argument }, + { "locale", 'l', required_argument }, + { "no-convert", CHAR_MAX + 17, no_argument }, + { "no-hash", CHAR_MAX + 6, no_argument }, + { "no-redundancy", CHAR_MAX + 18, no_argument }, + { "output-file", 'o', required_argument }, + { "properties-input", 'P', no_argument }, + { "qt", CHAR_MAX + 9, no_argument }, + { "replace-text", CHAR_MAX + 19, no_argument }, + { "resource", 'r', required_argument }, + { "source", CHAR_MAX + 14, no_argument }, + { "statistics", 0, no_argument, &do_statistics, 1 }, + { "strict", CHAR_MAX + 20, no_argument }, + { "stringtable-input", CHAR_MAX + 8, no_argument }, + { "tcl", CHAR_MAX + 7, no_argument }, + { "template", CHAR_MAX + 16, required_argument }, + { "use-fuzzy", 'f', no_argument }, + { "use-untranslated", CHAR_MAX + 12, no_argument }, + { "verbose", 'v', no_argument }, + { "version", 'V', no_argument }, + { "xml", 'x', no_argument }, + { NULL, 'd', required_argument }, + }; + END_ALLOW_OMITTING_FIELD_INITIALIZERS + start_options (argc, argv, options, MOVE_OPTIONS_FIRST, 0); + int opt; + while ((opt = get_next_option ()) != -1) switch (opt) { - case '\0': /* Long option. */ + case '\0': /* Long option with key == 0. */ break; case 'a': if (isdigit ((unsigned char) optarg[0])) @@ -351,7 +351,7 @@ main (int argc, char *argv[]) java_resource_name = optarg; csharp_resource_name = optarg; break; - case 'S': + case CHAR_MAX + 20: /* --strict */ strict_uniforum = true; break; case 'v': diff --git a/gettext-tools/src/msggrep.c b/gettext-tools/src/msggrep.c index 6a654678d..b31b6f757 100644 --- a/gettext-tools/src/msggrep.c +++ b/gettext-tools/src/msggrep.c @@ -23,7 +23,6 @@ #include #include -#include #include #include #include @@ -40,6 +39,7 @@ #include #include +#include "options.h" #include "noreturn.h" #include "closeout.h" #include "dir-list.h" @@ -91,46 +91,6 @@ struct grep_task { }; static struct grep_task grep_task[5]; -/* Long options. */ -static const struct option long_options[] = -{ - { "add-location", optional_argument, NULL, 'n' }, - { "color", optional_argument, NULL, CHAR_MAX + 9 }, - { "comment", no_argument, NULL, 'C' }, - { "directory", required_argument, NULL, 'D' }, - { "domain", required_argument, NULL, 'M' }, - { "escape", no_argument, NULL, CHAR_MAX + 1 }, - { "extended-regexp", no_argument, NULL, 'E' }, - { "extracted-comment", no_argument, NULL, 'X' }, - { "file", required_argument, NULL, 'f' }, - { "fixed-strings", no_argument, NULL, 'F' }, - { "force-po", no_argument, &force_po, 1 }, - { "help", no_argument, NULL, 'h' }, - { "ignore-case", no_argument, NULL, 'i' }, - { "indent", no_argument, NULL, CHAR_MAX + 2 }, - { "invert-match", no_argument, NULL, 'v' }, - { "location", required_argument, NULL, 'N' }, - { "msgctxt", no_argument, NULL, 'J' }, - { "msgid", no_argument, NULL, 'K' }, - { "msgstr", no_argument, NULL, 'T' }, - { "no-escape", no_argument, NULL, CHAR_MAX + 3 }, - { "no-location", no_argument, NULL, CHAR_MAX + 11 }, - { "no-wrap", no_argument, NULL, CHAR_MAX + 6 }, - { "output-file", required_argument, NULL, 'o' }, - { "properties-input", no_argument, NULL, 'P' }, - { "properties-output", no_argument, NULL, 'p' }, - { "regexp", required_argument, NULL, 'e' }, - { "sort-by-file", no_argument, NULL, CHAR_MAX + 4 }, - { "sort-output", no_argument, NULL, CHAR_MAX + 5 }, - { "strict", no_argument, NULL, 'S' }, - { "stringtable-input", no_argument, NULL, CHAR_MAX + 7 }, - { "stringtable-output", no_argument, NULL, CHAR_MAX + 8 }, - { "style", required_argument, NULL, CHAR_MAX + 10 }, - { "version", no_argument, NULL, 'V' }, - { "width", required_argument, NULL, 'w' }, - { NULL, 0, NULL, 0 } -}; - /* Forward declaration of local functions. */ _GL_NORETURN_FUNC static void no_pass (int opt); @@ -141,7 +101,6 @@ static msgdomain_list_ty *process_msgdomain_list (msgdomain_list_ty *mdlp); int main (int argc, char **argv) { - int opt; bool do_help; bool do_version; char *output_file; @@ -191,12 +150,53 @@ main (int argc, char **argv) gt->case_insensitive = false; } - while ((opt = getopt_long (argc, argv, "CD:e:Ef:FhiJKM:nN:o:pPTvVw:X", - long_options, NULL)) - != EOF) + /* Parse command line options. */ + BEGIN_ALLOW_OMITTING_FIELD_INITIALIZERS + static const struct program_option options[] = + { + { "add-location", CHAR_MAX + 'n', optional_argument }, + { NULL, 'n', no_argument }, + { "color", CHAR_MAX + 9, optional_argument }, + { "comment", 'C', no_argument }, + { "directory", 'D', required_argument }, + { "domain", 'M', required_argument }, + { "escape", CHAR_MAX + 1, no_argument }, + { "extended-regexp", 'E', no_argument }, + { "extracted-comment", 'X', no_argument }, + { "file", 'f', required_argument }, + { "fixed-strings", 'F', no_argument }, + { "force-po", 0, no_argument, &force_po, 1 }, + { "help", 'h', no_argument }, + { "ignore-case", 'i', no_argument }, + { "indent", CHAR_MAX + 2, no_argument }, + { "invert-match", 'v', no_argument }, + { "location", 'N', required_argument }, + { "msgctxt", 'J', no_argument }, + { "msgid", 'K', no_argument }, + { "msgstr", 'T', no_argument }, + { "no-escape", CHAR_MAX + 3, no_argument }, + { "no-location", CHAR_MAX + 11, no_argument }, + { "no-wrap", CHAR_MAX + 6, no_argument }, + { "output-file", 'o', required_argument }, + { "properties-input", 'P', no_argument }, + { "properties-output", 'p', no_argument }, + { "regexp", 'e', required_argument }, + { "sort-by-file", CHAR_MAX + 4, no_argument }, + { "sort-output", CHAR_MAX + 5, no_argument }, + { "strict", CHAR_MAX + 12, no_argument }, + { "stringtable-input", CHAR_MAX + 7, no_argument }, + { "stringtable-output", CHAR_MAX + 8, no_argument }, + { "style", CHAR_MAX + 10, required_argument }, + { "version", 'V', no_argument }, + { "width", 'w', required_argument }, + }; + END_ALLOW_OMITTING_FIELD_INITIALIZERS + start_options (argc, argv, options, MOVE_OPTIONS_FIRST, 0); + int opt; + while ((opt = get_next_option ()) != -1) switch (opt) { - case '\0': /* Long option. */ + case '\0': /* Long option with key == 0. */ break; case 'C': @@ -305,7 +305,8 @@ main (int argc, char **argv) string_list_append (domain_names, optarg); break; - case 'n': + case 'n': /* -n */ + case CHAR_MAX + 'n': /* --add-location[={full|yes|file|never|no}] */ if (handle_filepos_comment_option (optarg)) usage (EXIT_FAILURE); break; @@ -326,7 +327,7 @@ main (int argc, char **argv) input_syntax = &input_format_properties; break; - case 'S': + case CHAR_MAX + 12: /* --strict */ message_print_style_uniforum (); break; diff --git a/gettext-tools/src/msginit.c b/gettext-tools/src/msginit.c index 0dabed15e..4182ec9e3 100644 --- a/gettext-tools/src/msginit.c +++ b/gettext-tools/src/msginit.c @@ -24,7 +24,6 @@ #include #include #include -#include #include #include #include @@ -42,7 +41,7 @@ #include #include - +#include "options.h" #include "noreturn.h" #include "closeout.h" #include "error-progname.h" @@ -106,26 +105,6 @@ static const char *language; /* If true, the user is not considered to be the translator. */ static bool no_translator; -/* Long options. */ -static const struct option long_options[] = -{ - { "color", optional_argument, NULL, CHAR_MAX + 5 }, - { "help", no_argument, NULL, 'h' }, - { "input", required_argument, NULL, 'i' }, - { "locale", required_argument, NULL, 'l' }, - { "no-translator", no_argument, NULL, CHAR_MAX + 1 }, - { "no-wrap", no_argument, NULL, CHAR_MAX + 2 }, - { "output-file", required_argument, NULL, 'o' }, - { "properties-input", no_argument, NULL, 'P' }, - { "properties-output", no_argument, NULL, 'p' }, - { "stringtable-input", no_argument, NULL, CHAR_MAX + 3 }, - { "stringtable-output", no_argument, NULL, CHAR_MAX + 4 }, - { "style", required_argument, NULL, CHAR_MAX + 6 }, - { "version", no_argument, NULL, 'V' }, - { "width", required_argument, NULL, 'w' }, - { NULL, 0, NULL, 0 } -}; - /* Forward declaration of local functions. */ _GL_NORETURN_FUNC static void usage (int status); static const char *find_pot (void); @@ -139,7 +118,6 @@ static msgdomain_list_ty *update_msgstr_plurals (msgdomain_list_ty *mdlp); int main (int argc, char **argv) { - int opt; bool do_help; bool do_version; char *output_file; @@ -172,11 +150,32 @@ main (int argc, char **argv) input_file = NULL; locale = NULL; - while ((opt = getopt_long (argc, argv, "hi:l:o:pPVw:", long_options, NULL)) - != EOF) + /* Parse command line options. */ + BEGIN_ALLOW_OMITTING_FIELD_INITIALIZERS + static const struct program_option options[] = + { + { "color", CHAR_MAX + 5, optional_argument }, + { "help", 'h', no_argument }, + { "input", 'i', required_argument }, + { "locale", 'l', required_argument }, + { "no-translator", CHAR_MAX + 1, no_argument }, + { "no-wrap", CHAR_MAX + 2, no_argument }, + { "output-file", 'o', required_argument }, + { "properties-input", 'P', no_argument }, + { "properties-output", 'p', no_argument }, + { "stringtable-input", CHAR_MAX + 3, no_argument }, + { "stringtable-output", CHAR_MAX + 4, no_argument }, + { "style", CHAR_MAX + 6, required_argument }, + { "version", 'V', no_argument }, + { "width", 'w', required_argument }, + }; + END_ALLOW_OMITTING_FIELD_INITIALIZERS + start_options (argc, argv, options, MOVE_OPTIONS_FIRST, 0); + int opt; + while ((opt = get_next_option ()) != -1) switch (opt) { - case '\0': /* Long option. */ + case '\0': /* Long option with key == 0. */ break; case 'h': diff --git a/gettext-tools/src/msgmerge.c b/gettext-tools/src/msgmerge.c index 095c18753..6e8afce04 100644 --- a/gettext-tools/src/msgmerge.c +++ b/gettext-tools/src/msgmerge.c @@ -20,7 +20,6 @@ #endif #include -#include #include #include #include @@ -34,6 +33,7 @@ #include #include +#include "options.h" #include "noreturn.h" #include "closeout.h" #include "dir-list.h" @@ -120,45 +120,6 @@ static bool update_mode = false; static const char *version_control_string; static const char *backup_suffix_string; -/* Long options. */ -static const struct option long_options[] = -{ - { "add-location", optional_argument, NULL, 'n' }, - { "backup", required_argument, NULL, CHAR_MAX + 1 }, - { "color", optional_argument, NULL, CHAR_MAX + 9 }, - { "compendium", required_argument, NULL, 'C' }, - { "directory", required_argument, NULL, 'D' }, - { "escape", no_argument, NULL, 'E' }, - { "for-msgfmt", no_argument, NULL, CHAR_MAX + 12 }, - { "force-po", no_argument, &force_po, 1 }, - { "help", no_argument, NULL, 'h' }, - { "indent", no_argument, NULL, 'i' }, - { "lang", required_argument, NULL, CHAR_MAX + 8 }, - { "multi-domain", no_argument, NULL, 'm' }, - { "no-escape", no_argument, NULL, 'e' }, - { "no-fuzzy-matching", no_argument, NULL, 'N' }, - { "no-location", no_argument, NULL, CHAR_MAX + 11 }, - { "no-wrap", no_argument, NULL, CHAR_MAX + 4 }, - { "output-file", required_argument, NULL, 'o' }, - { "previous", no_argument, NULL, CHAR_MAX + 7 }, - { "properties-input", no_argument, NULL, 'P' }, - { "properties-output", no_argument, NULL, 'p' }, - { "quiet", no_argument, NULL, 'q' }, - { "sort-by-file", no_argument, NULL, 'F' }, - { "sort-output", no_argument, NULL, 's' }, - { "silent", no_argument, NULL, 'q' }, - { "strict", no_argument, NULL, CHAR_MAX + 2 }, - { "stringtable-input", no_argument, NULL, CHAR_MAX + 5 }, - { "stringtable-output", no_argument, NULL, CHAR_MAX + 6 }, - { "style", required_argument, NULL, CHAR_MAX + 10 }, - { "suffix", required_argument, NULL, CHAR_MAX + 3 }, - { "update", no_argument, NULL, 'U' }, - { "verbose", no_argument, NULL, 'v' }, - { "version", no_argument, NULL, 'V' }, - { "width", required_argument, NULL, 'w' }, - { NULL, 0, NULL, 0 } -}; - struct statistics { @@ -181,7 +142,6 @@ static msgdomain_list_ty *merge (const char *fn1, const char *fn2, int main (int argc, char **argv) { - int opt; bool do_help; bool do_version; char *output_file; @@ -218,12 +178,52 @@ main (int argc, char **argv) output_file = NULL; color = NULL; - while ((opt = getopt_long (argc, argv, "C:D:eEFhimnNo:pPqsUvVw:", - long_options, NULL)) - != EOF) + /* Parse command line options. */ + BEGIN_ALLOW_OMITTING_FIELD_INITIALIZERS + static const struct program_option options[] = + { + { "add-location", CHAR_MAX + 'n', optional_argument }, + { NULL, 'n', no_argument }, + { "backup", CHAR_MAX + 1, required_argument }, + { "color", CHAR_MAX + 9, optional_argument }, + { "compendium", 'C', required_argument }, + { "directory", 'D', required_argument }, + { "escape", 'E', no_argument }, + { "for-msgfmt", CHAR_MAX + 12, no_argument }, + { "force-po", 0, no_argument, &force_po, 1 }, + { "help", 'h', no_argument }, + { "indent", 'i', no_argument }, + { "lang", CHAR_MAX + 8, required_argument }, + { "multi-domain", 'm', no_argument }, + { "no-escape", 'e', no_argument }, + { "no-fuzzy-matching", 'N', no_argument }, + { "no-location", CHAR_MAX + 11, no_argument }, + { "no-wrap", CHAR_MAX + 4, no_argument }, + { "output-file", 'o', required_argument }, + { "previous", CHAR_MAX + 7, no_argument }, + { "properties-input", 'P', no_argument }, + { "properties-output", 'p', no_argument }, + { "quiet", 'q', no_argument }, + { "sort-by-file", 'F', no_argument }, + { "sort-output", 's', no_argument }, + { "silent", 'q', no_argument }, + { "strict", CHAR_MAX + 2, no_argument }, + { "stringtable-input", CHAR_MAX + 5, no_argument }, + { "stringtable-output", CHAR_MAX + 6, no_argument }, + { "style", CHAR_MAX + 10, required_argument }, + { "suffix", CHAR_MAX + 3, required_argument }, + { "update", 'U', no_argument }, + { "verbose", 'v', no_argument }, + { "version", 'V', no_argument }, + { "width", 'w', required_argument }, + }; + END_ALLOW_OMITTING_FIELD_INITIALIZERS + start_options (argc, argv, options, MOVE_OPTIONS_FIRST, 0); + int opt; + while ((opt = get_next_option ()) != -1) switch (opt) { - case '\0': /* Long option. */ + case '\0': /* Long option with key == 0. */ break; case 'C': @@ -258,7 +258,8 @@ main (int argc, char **argv) multi_domain_mode = true; break; - case 'n': + case 'n': /* -n */ + case CHAR_MAX + 'n': /* --add-location[={full|yes|file|never|no}] */ if (handle_filepos_comment_option (optarg)) usage (EXIT_FAILURE); break; diff --git a/gettext-tools/src/msgunfmt.c b/gettext-tools/src/msgunfmt.c index 2724fb4ff..3777dcd91 100644 --- a/gettext-tools/src/msgunfmt.c +++ b/gettext-tools/src/msgunfmt.c @@ -19,7 +19,6 @@ # include #endif -#include #include #include #include @@ -29,6 +28,7 @@ #include #include +#include "options.h" #include "noreturn.h" #include "closeout.h" #include "error-progname.h" @@ -78,34 +78,6 @@ static const char *tcl_base_directory; /* Force output of PO file even if empty. */ static int force_po; -/* Long options. */ -static const struct option long_options[] = -{ - { "color", optional_argument, NULL, CHAR_MAX + 6 }, - { "csharp", no_argument, NULL, CHAR_MAX + 4 }, - { "csharp-resources", no_argument, NULL, CHAR_MAX + 5 }, - { "escape", no_argument, NULL, 'E' }, - { "force-po", no_argument, &force_po, 1 }, - { "help", no_argument, NULL, 'h' }, - { "indent", no_argument, NULL, 'i' }, - { "java", no_argument, NULL, 'j' }, - { "locale", required_argument, NULL, 'l' }, - { "no-escape", no_argument, NULL, 'e' }, - { "no-wrap", no_argument, NULL, CHAR_MAX + 2 }, - { "output-file", required_argument, NULL, 'o' }, - { "properties-output", no_argument, NULL, 'p' }, - { "resource", required_argument, NULL, 'r' }, - { "sort-output", no_argument, NULL, 's' }, - { "strict", no_argument, NULL, 'S' }, - { "stringtable-output", no_argument, NULL, CHAR_MAX + 3 }, - { "style", required_argument, NULL, CHAR_MAX + 7 }, - { "tcl", no_argument, NULL, CHAR_MAX + 1 }, - { "verbose", no_argument, NULL, 'v' }, - { "version", no_argument, NULL, 'V' }, - { "width", required_argument, NULL, 'w' }, - { NULL, 0, NULL, 0 } -}; - /* Forward declaration of local functions. */ _GL_NORETURN_FUNC static void usage (int status); @@ -115,7 +87,6 @@ static void read_one_file (message_list_ty *mlp, const char *filename); int main (int argc, char **argv) { - int optchar; bool do_help = false; bool do_version = false; const char *output_file = "-"; @@ -139,13 +110,41 @@ main (int argc, char **argv) /* Ensure that write errors on stdout are detected. */ atexit (close_stdout); - while ((optchar = getopt_long (argc, argv, "d:eEhijl:o:pr:svVw:", - long_options, NULL)) - != EOF) + /* Parse command line options. */ + BEGIN_ALLOW_OMITTING_FIELD_INITIALIZERS + static const struct program_option options[] = + { + { "color", CHAR_MAX + 6, optional_argument }, + { "csharp", CHAR_MAX + 4, no_argument }, + { "csharp-resources", CHAR_MAX + 5, no_argument }, + { "escape", 'E', no_argument }, + { "force-po", 0, no_argument, &force_po, 1 }, + { "help", 'h', no_argument }, + { "indent", 'i', no_argument }, + { "java", 'j', no_argument }, + { "locale", 'l', required_argument }, + { "no-escape", 'e', no_argument }, + { "no-wrap", CHAR_MAX + 2, no_argument }, + { "output-file", 'o', required_argument }, + { "properties-output", 'p', no_argument }, + { "resource", 'r', required_argument }, + { "sort-output", 's', no_argument }, + { "strict", CHAR_MAX + 8, no_argument }, + { "stringtable-output", CHAR_MAX + 3, no_argument }, + { "style", CHAR_MAX + 7, required_argument }, + { "tcl", CHAR_MAX + 1, no_argument }, + { "verbose", 'v', no_argument }, + { "version", 'V', no_argument }, + { "width", 'w', required_argument }, + { NULL, 'd', required_argument }, + }; + END_ALLOW_OMITTING_FIELD_INITIALIZERS + start_options (argc, argv, options, MOVE_OPTIONS_FIRST, 0); + int optchar; + while ((optchar = get_next_option ()) != -1) switch (optchar) { - case '\0': - /* long option */ + case '\0': /* Long option with key == 0. */ break; case 'd': @@ -196,7 +195,7 @@ main (int argc, char **argv) sort_by_msgid = true; break; - case 'S': + case CHAR_MAX + 8: /* --strict */ message_print_style_uniforum (); break; diff --git a/gettext-tools/src/msguniq.c b/gettext-tools/src/msguniq.c index a085152a8..38be7b8c0 100644 --- a/gettext-tools/src/msguniq.c +++ b/gettext-tools/src/msguniq.c @@ -20,7 +20,6 @@ # include "config.h" #endif -#include #include #include #include @@ -29,6 +28,7 @@ #include #include +#include "options.h" #include "noreturn.h" #include "closeout.h" #include "dir-list.h" @@ -60,37 +60,6 @@ static int force_po; /* Target encoding. */ static const char *to_code; -/* Long options. */ -static const struct option long_options[] = -{ - { "add-location", optional_argument, NULL, 'n' }, - { "color", optional_argument, NULL, CHAR_MAX + 5 }, - { "directory", required_argument, NULL, 'D' }, - { "escape", no_argument, NULL, 'E' }, - { "force-po", no_argument, &force_po, 1 }, - { "help", no_argument, NULL, 'h' }, - { "indent", no_argument, NULL, 'i' }, - { "no-escape", no_argument, NULL, 'e' }, - { "no-location", no_argument, NULL, CHAR_MAX + 7 }, - { "no-wrap", no_argument, NULL, CHAR_MAX + 2 }, - { "output-file", required_argument, NULL, 'o' }, - { "properties-input", no_argument, NULL, 'P' }, - { "properties-output", no_argument, NULL, 'p' }, - { "repeated", no_argument, NULL, 'd' }, - { "sort-by-file", no_argument, NULL, 'F' }, - { "sort-output", no_argument, NULL, 's' }, - { "strict", no_argument, NULL, 'S' }, - { "stringtable-input", no_argument, NULL, CHAR_MAX + 3 }, - { "stringtable-output", no_argument, NULL, CHAR_MAX + 4 }, - { "style", required_argument, NULL, CHAR_MAX + 6 }, - { "to-code", required_argument, NULL, 't' }, - { "unique", no_argument, NULL, 'u' }, - { "use-first", no_argument, NULL, CHAR_MAX + 1 }, - { "version", no_argument, NULL, 'V' }, - { "width", required_argument, NULL, 'w' }, - { NULL, 0, NULL, 0 } -}; - /* Forward declaration of local functions. */ _GL_NORETURN_FUNC static void usage (int status); @@ -99,7 +68,6 @@ _GL_NORETURN_FUNC static void usage (int status); int main (int argc, char **argv) { - int optchar; bool do_help; bool do_version; char *output_file; @@ -137,11 +105,44 @@ main (int argc, char **argv) less_than = INT_MAX; use_first = false; - while ((optchar = getopt_long (argc, argv, "dD:eEFhino:pPst:uVw:", - long_options, NULL)) != EOF) + /* Parse command line options. */ + BEGIN_ALLOW_OMITTING_FIELD_INITIALIZERS + static const struct program_option options[] = + { + { "add-location", CHAR_MAX + 'n', optional_argument }, + { NULL, 'n', no_argument }, + { "color", CHAR_MAX + 5, optional_argument }, + { "directory", 'D', required_argument }, + { "escape", 'E', no_argument }, + { "force-po", 0, no_argument, &force_po, 1 }, + { "help", 'h', no_argument }, + { "indent", 'i', no_argument }, + { "no-escape", 'e', no_argument }, + { "no-location", CHAR_MAX + 7, no_argument }, + { "no-wrap", CHAR_MAX + 2, no_argument }, + { "output-file", 'o', required_argument }, + { "properties-input", 'P', no_argument }, + { "properties-output", 'p', no_argument }, + { "repeated", 'd', no_argument }, + { "sort-by-file", 'F', no_argument }, + { "sort-output", 's', no_argument }, + { "strict", CHAR_MAX + 8, no_argument }, + { "stringtable-input", CHAR_MAX + 3, no_argument }, + { "stringtable-output", CHAR_MAX + 4, no_argument }, + { "style", CHAR_MAX + 6, required_argument }, + { "to-code", 't', required_argument }, + { "unique", 'u', no_argument }, + { "use-first", CHAR_MAX + 1, no_argument }, + { "version", 'V', no_argument }, + { "width", 'w', required_argument }, + }; + END_ALLOW_OMITTING_FIELD_INITIALIZERS + start_options (argc, argv, options, MOVE_OPTIONS_FIRST, 0); + int optchar; + while ((optchar = get_next_option ()) != -1) switch (optchar) { - case '\0': /* Long option. */ + case '\0': /* Long option with key == 0. */ break; case 'd': @@ -173,7 +174,8 @@ main (int argc, char **argv) message_print_style_indent (); break; - case 'n': + case 'n': /* -n */ + case CHAR_MAX + 'n': /* --add-location[={full|yes|file|never|no}] */ if (handle_filepos_comment_option (optarg)) usage (EXIT_FAILURE); break; @@ -194,7 +196,7 @@ main (int argc, char **argv) sort_by_msgid = true; break; - case 'S': + case CHAR_MAX + 8: /* --strict */ message_print_style_uniforum (); break; diff --git a/gettext-tools/src/recode-sr-latin.c b/gettext-tools/src/recode-sr-latin.c index 50292ce97..0ff4162b4 100644 --- a/gettext-tools/src/recode-sr-latin.c +++ b/gettext-tools/src/recode-sr-latin.c @@ -20,7 +20,6 @@ #endif #include -#include #include #include #include @@ -31,6 +30,7 @@ #endif #include +#include "options.h" #include "noreturn.h" #include "closeout.h" #include "progname.h" @@ -47,14 +47,6 @@ #define _(str) gettext (str) -/* Long options. */ -static const struct option long_options[] = -{ - { "help", no_argument, NULL, 'h' }, - { "version", no_argument, NULL, 'V' }, - { NULL, 0, NULL, 0 } -}; - /* Forward declaration of local functions. */ _GL_NORETURN_FUNC static void usage (int status); static void process (FILE *stream); @@ -66,8 +58,6 @@ main (int argc, char *argv[]) bool do_help = false; bool do_version = false; - int opt; - /* Set program name for message texts. */ set_program_name (argv[0]); @@ -83,10 +73,19 @@ main (int argc, char *argv[]) atexit (close_stdout); /* Parse command line options. */ - while ((opt = getopt_long (argc, argv, "hV", long_options, NULL)) != EOF) + BEGIN_ALLOW_OMITTING_FIELD_INITIALIZERS + static const struct program_option options[] = + { + { "help", 'h', no_argument }, + { "version", 'V', no_argument }, + }; + END_ALLOW_OMITTING_FIELD_INITIALIZERS + start_options (argc, argv, options, MOVE_OPTIONS_FIRST, 0); + int opt; + while ((opt = get_next_option ()) != -1) switch (opt) { - case '\0': /* Long option. */ + case '\0': /* Long option with key == 0. */ break; case 'h': do_help = true; diff --git a/gettext-tools/src/urlget.c b/gettext-tools/src/urlget.c index c1ade19eb..16941294d 100644 --- a/gettext-tools/src/urlget.c +++ b/gettext-tools/src/urlget.c @@ -22,7 +22,6 @@ #include #include -#include #include #include #include @@ -30,6 +29,7 @@ #include #include +#include "options.h" #include "noreturn.h" #include "closeout.h" #include "error-progname.h" @@ -69,16 +69,6 @@ produces no output for more than 10 seconds for no apparent reason. */ static bool verbose = true; -/* Long options. */ -static const struct option long_options[] = -{ - { "help", no_argument, NULL, 'h' }, - { "quiet", no_argument, NULL, 'q' }, - { "silent", no_argument, NULL, 'q' }, - { "version", no_argument, NULL, 'V' }, - { NULL, 0, NULL, 0 } -}; - /* Forward declaration of local functions. */ _GL_NORETURN_FUNC static void usage (int status); @@ -88,7 +78,6 @@ static void fetch (const char *url, const char *file); int main (int argc, char *argv[]) { - int optchar; bool do_help; bool do_version; @@ -112,10 +101,21 @@ main (int argc, char *argv[]) do_version = false; /* Parse command line options. */ - while ((optchar = getopt_long (argc, argv, "hqV", long_options, NULL)) != EOF) + BEGIN_ALLOW_OMITTING_FIELD_INITIALIZERS + static const struct program_option options[] = + { + { "help", 'h', no_argument }, + { "quiet", 'q', no_argument }, + { "silent", 'q', no_argument }, + { "version", 'V', no_argument }, + }; + END_ALLOW_OMITTING_FIELD_INITIALIZERS + start_options (argc, argv, options, MOVE_OPTIONS_FIRST, 0); + int optchar; + while ((optchar = get_next_option ()) != -1) switch (optchar) { - case '\0': /* Long option. */ + case '\0': /* Long option with key == 0. */ break; case 'h': /* --help */ do_help = true; diff --git a/gettext-tools/src/xgettext.c b/gettext-tools/src/xgettext.c index 1671ff96c..c10186db1 100644 --- a/gettext-tools/src/xgettext.c +++ b/gettext-tools/src/xgettext.c @@ -25,7 +25,6 @@ #include #include -#include #include #include #include @@ -43,6 +42,7 @@ #include #include +#include "options.h" #include "noreturn.h" #include "rc-str-list.h" #include "xg-encoding.h" @@ -260,66 +260,6 @@ static string_list_ty files_for_vc_mtime; shall be ignored. */ static gl_set_t generated_files; -/* Long options. */ -static const struct option long_options[] = -{ - { "add-comments", optional_argument, NULL, 'c' }, - { "add-location", optional_argument, NULL, 'n' }, - { "boost", no_argument, NULL, CHAR_MAX + 11 }, - { "c++", no_argument, NULL, 'C' }, - { "check", required_argument, NULL, CHAR_MAX + 17 }, - { "color", optional_argument, NULL, CHAR_MAX + 14 }, - { "copyright-holder", required_argument, NULL, CHAR_MAX + 1 }, - { "debug", no_argument, &do_debug, 1 }, - { "default-domain", required_argument, NULL, 'd' }, - { "directory", required_argument, NULL, 'D' }, - { "escape", no_argument, NULL, 'E' }, - { "exclude-file", required_argument, NULL, 'x' }, - { "extract-all", no_argument, NULL, 'a' }, - { "files-from", required_argument, NULL, 'f' }, - { "flag", required_argument, NULL, CHAR_MAX + 8 }, - { "force-po", no_argument, &force_po, 1 }, - { "foreign-user", no_argument, NULL, CHAR_MAX + 2 }, - { "from-code", required_argument, NULL, CHAR_MAX + 3 }, - { "generated", required_argument, NULL, CHAR_MAX + 24 }, - { "help", no_argument, NULL, 'h' }, - { "indent", no_argument, NULL, 'i' }, - { "its", required_argument, NULL, CHAR_MAX + 20 }, - { "itstool", no_argument, NULL, CHAR_MAX + 19 }, - { "join-existing", no_argument, NULL, 'j' }, - { "kde", no_argument, NULL, CHAR_MAX + 10 }, - { "keyword", optional_argument, NULL, 'k' }, - { "language", required_argument, NULL, 'L' }, - { "msgid-bugs-address", required_argument, NULL, CHAR_MAX + 5 }, - { "msgstr-prefix", optional_argument, NULL, 'm' }, - { "msgstr-suffix", optional_argument, NULL, 'M' }, - { "no-escape", no_argument, NULL, 'e' }, - { "no-git", no_argument, NULL, CHAR_MAX + 23 }, - { "no-location", no_argument, NULL, CHAR_MAX + 16 }, - { "no-wrap", no_argument, NULL, CHAR_MAX + 4 }, - { "omit-header", no_argument, &xgettext_omit_header, 1 }, - { "output", required_argument, NULL, 'o' }, - { "output-dir", required_argument, NULL, 'p' }, - { "package-name", required_argument, NULL, CHAR_MAX + 12 }, - { "package-version", required_argument, NULL, CHAR_MAX + 13 }, - { "properties-output", no_argument, NULL, CHAR_MAX + 6 }, - { "qt", no_argument, NULL, CHAR_MAX + 9 }, - { "reference", required_argument, NULL, CHAR_MAX + 22 }, - { "sentence-end", required_argument, NULL, CHAR_MAX + 18 }, - { "sort-by-file", no_argument, NULL, 'F' }, - { "sort-output", no_argument, NULL, 's' }, - { "strict", no_argument, NULL, 'S' }, - { "string-limit", required_argument, NULL, 'l' }, - { "stringtable-output", no_argument, NULL, CHAR_MAX + 7 }, - { "style", required_argument, NULL, CHAR_MAX + 15 }, - { "tag", required_argument, NULL, CHAR_MAX + 21 }, - { "trigraphs", no_argument, NULL, 'T' }, - { "verbose", no_argument, NULL, 'v' }, - { "version", no_argument, NULL, 'V' }, - { "width", required_argument, NULL, 'w' }, - { NULL, 0, NULL, 0 } -}; - /* The extractors must all be functions returning void and taking as arguments - the file name or file stream, @@ -364,7 +304,6 @@ static const char *extension_to_language (const char *extension); int main (int argc, char *argv[]) { - int optchar; bool do_help = false; bool do_version = false; msgdomain_list_ty *mdlp; @@ -437,12 +376,74 @@ main (int argc, char *argv[]) init_flag_table_gcc_internal (); init_flag_table_ycp (); - while ((optchar = getopt_long (argc, argv, - "ac::Cd:D:eEf:Fhijk::l:L:m::M::no:p:sTvVw:x:", - long_options, NULL)) != EOF) + /* Parse command line options. */ + BEGIN_ALLOW_OMITTING_FIELD_INITIALIZERS + static const struct program_option options[] = + { + { "add-comments", 'c', optional_argument }, + { "add-location", CHAR_MAX + 'n', optional_argument }, + { NULL, 'n', no_argument }, + { "boost", CHAR_MAX + 11, no_argument }, + { "c++", 'C', no_argument }, + { "check", CHAR_MAX + 17, required_argument }, + { "color", CHAR_MAX + 14, optional_argument }, + { "copyright-holder", CHAR_MAX + 1, required_argument }, + { "debug", 0, no_argument, &do_debug, 1 }, + { "default-domain", 'd', required_argument }, + { "directory", 'D', required_argument }, + { "escape", 'E', no_argument }, + { "exclude-file", 'x', required_argument }, + { "extract-all", 'a', no_argument }, + { "files-from", 'f', required_argument }, + { "flag", CHAR_MAX + 8, required_argument }, + { "force-po", 0, no_argument, &force_po, 1 }, + { "foreign-user", CHAR_MAX + 2, no_argument }, + { "from-code", CHAR_MAX + 3, required_argument }, + { "generated", CHAR_MAX + 24, required_argument }, + { "help", 'h', no_argument }, + { "indent", 'i', no_argument }, + { "its", CHAR_MAX + 20, required_argument }, + { "itstool", CHAR_MAX + 19, no_argument }, + { "join-existing", 'j', no_argument }, + { "kde", CHAR_MAX + 10, no_argument }, + { "keyword", 'k', optional_argument }, + { "language", 'L', required_argument }, + { "msgid-bugs-address", CHAR_MAX + 5, required_argument }, + { "msgstr-prefix", 'm', optional_argument }, + { "msgstr-suffix", 'M', optional_argument }, + { "no-escape", 'e', no_argument }, + { "no-git", CHAR_MAX + 23, no_argument }, + { "no-location", CHAR_MAX + 16, no_argument }, + { "no-wrap", CHAR_MAX + 4, no_argument }, + { "omit-header", 0, no_argument, &xgettext_omit_header, 1 }, + { "output", 'o', required_argument }, + { "output-dir", 'p', required_argument }, + { "package-name", CHAR_MAX + 12, required_argument }, + { "package-version", CHAR_MAX + 13, required_argument }, + { "properties-output", CHAR_MAX + 6, no_argument }, + { "qt", CHAR_MAX + 9, no_argument }, + { "reference", CHAR_MAX + 22, required_argument }, + { "sentence-end", CHAR_MAX + 18, required_argument }, + { "sort-by-file", 'F', no_argument }, + { "sort-output", 's', no_argument }, + { "strict", CHAR_MAX + 25, no_argument }, + { "string-limit", 'l', required_argument }, + { "stringtable-output", CHAR_MAX + 7, no_argument }, + { "style", CHAR_MAX + 15, required_argument }, + { "tag", CHAR_MAX + 21, required_argument }, + { "trigraphs", 'T', no_argument }, + { "verbose", 'v', no_argument }, + { "version", 'V', no_argument }, + { "width", 'w', required_argument }, + { NULL, 'W', required_argument }, + }; + END_ALLOW_OMITTING_FIELD_INITIALIZERS + start_options (argc, argv, options, MOVE_OPTIONS_FIRST, 0); + int optchar; + while ((optchar = get_next_option ()) != -1) switch (optchar) { - case '\0': /* Long option. */ + case '\0': /* Long option with key == 0. */ break; case 'a': @@ -580,7 +581,8 @@ main (int argc, char *argv[]) msgstr_suffix = optarg == NULL ? "" : optarg; break; - case 'n': + case 'n': /* -n */ + case CHAR_MAX + 'n': /* --add-location[={full|yes|file|never|no}] */ if (handle_filepos_comment_option (optarg)) usage (EXIT_FAILURE); break; @@ -607,7 +609,7 @@ main (int argc, char *argv[]) sort_by_msgid = true; break; - case 'S': + case CHAR_MAX + 25: /* --strict */ message_print_style_uniforum (); break; diff --git a/gettext-tools/tests/tstgettext.c b/gettext-tools/tests/tstgettext.c index a0989dd2a..cd97be1d5 100644 --- a/gettext-tools/tests/tstgettext.c +++ b/gettext-tools/tests/tstgettext.c @@ -1,5 +1,5 @@ /* gettext - retrieve text string from message catalog and print it. - Copyright (C) 1995-2024 Free Software Foundation, Inc. + Copyright (C) 1995-2025 Free Software Foundation, Inc. Written by Ulrich Drepper , May 1995. This program is free software: you can redistribute it and/or modify @@ -19,7 +19,7 @@ # include #endif -#include +#include #include #include #include @@ -27,6 +27,7 @@ #include #include +#include "options.h" #include "attribute.h" #include "noreturn.h" #include "closeout.h" @@ -51,18 +52,6 @@ extern char *setlocale (int category, const char *locale); #define _(str) gettext (str) -/* Long options. */ -static const struct option long_options[] = -{ - { "domain", required_argument, NULL, 'd' }, - { "env", required_argument, NULL, '=' }, - { "help", no_argument, NULL, 'h' }, - { "shell-script", no_argument, NULL, 's' }, - { "thread", no_argument, NULL, 't' }, - { "version", no_argument, NULL, 'V' }, - { NULL, 0, NULL, 0 } -}; - /* Forward declaration of local functions. */ _GL_NORETURN_FUNC static void *worker_thread (void *arg); _GL_NORETURN_FUNC static void usage (int status); @@ -86,8 +75,6 @@ struct worker_context int main (int argc, char *argv[]) { - int optchar; - /* Default values for command line options. */ bool do_help = false; bool do_thread = false; @@ -116,11 +103,26 @@ main (int argc, char *argv[]) atexit (close_stdout); /* Parse command line options. */ - while ((optchar = getopt_long (argc, argv, "+d:eEhnstV", long_options, NULL)) - != EOF) + BEGIN_ALLOW_OMITTING_FIELD_INITIALIZERS + static const struct program_option options[] = + { + { "domain", 'd', required_argument }, + { "env", CHAR_MAX + 1, required_argument }, + { "help", 'h', no_argument }, + { "shell-script", 's', no_argument }, + { "thread", 't', no_argument }, + { "version", 'V', no_argument }, + { NULL, 'e', no_argument }, + { NULL, 'E', no_argument }, + { NULL, 'n', no_argument }, + }; + END_ALLOW_OMITTING_FIELD_INITIALIZERS + start_options (argc, argv, options, NON_OPTION_TERMINATES_OPTIONS, 0); + int optchar; + while ((optchar = get_next_option ()) != -1) switch (optchar) { - case '\0': /* Long option. */ + case '\0': /* Long option with key == 0. */ break; case 'd': context.domain = optarg; @@ -146,7 +148,7 @@ main (int argc, char *argv[]) case 'V': do_version = true; break; - case '=': + case CHAR_MAX + 1: /* --env */ { /* Undocumented option --env sets an environment variable. */ char *separator = strchr (optarg, '='); diff --git a/gettext-tools/tests/tstngettext.c b/gettext-tools/tests/tstngettext.c index 7af6c38cd..5efe49d54 100644 --- a/gettext-tools/tests/tstngettext.c +++ b/gettext-tools/tests/tstngettext.c @@ -18,7 +18,7 @@ # include #endif -#include +#include #include #include #include @@ -28,6 +28,7 @@ #include #include +#include "options.h" #include "attribute.h" #include "noreturn.h" #include "closeout.h" @@ -50,17 +51,6 @@ extern char *setlocale (int category, const char *locale); #define _(str) gettext (str) -/* Long options. */ -static const struct option long_options[] = -{ - { "domain", required_argument, NULL, 'd' }, - { "env", required_argument, NULL, '=' }, - { "help", no_argument, NULL, 'h' }, - { "thread", no_argument, NULL, 't' }, - { "version", no_argument, NULL, 'V' }, - { NULL, 0, NULL, 0 } -}; - /* Forward declaration of local functions. */ _GL_NORETURN_FUNC static void *worker_thread (void *arg); _GL_NORETURN_FUNC static void usage (int __status); @@ -77,8 +67,6 @@ struct worker_context int main (int argc, char *argv[]) { - int optchar; - /* Default values for command line options. */ bool do_help = false; bool do_thread = false; @@ -104,11 +92,22 @@ main (int argc, char *argv[]) atexit (close_stdout); /* Parse command line options. */ - while ((optchar = getopt_long (argc, argv, "+d:htV", long_options, NULL)) - != EOF) + BEGIN_ALLOW_OMITTING_FIELD_INITIALIZERS + static const struct program_option options[] = + { + { "domain", 'd', required_argument }, + { "env", CHAR_MAX + 1, required_argument }, + { "help", 'h', no_argument }, + { "thread", 't', no_argument }, + { "version", 'V', no_argument }, + }; + END_ALLOW_OMITTING_FIELD_INITIALIZERS + start_options (argc, argv, options, NON_OPTION_TERMINATES_OPTIONS, 0); + int optchar; + while ((optchar = get_next_option ()) != -1) switch (optchar) { - case '\0': /* Long option. */ + case '\0': /* Long option with key == 0. */ break; case 'd': context.domain = optarg; @@ -122,7 +121,7 @@ main (int argc, char *argv[]) case 'V': do_version = true; break; - case '=': + case CHAR_MAX + 1: /* --env */ { /* Undocumented option --env sets an environment variable. */ char *separator = strchr (optarg, '='); -- 2.47.3