13.22.1 getopt_long

LSB specification:
https://refspecs.linuxbase.org/LSB_5.0.0/LSB-Core-generic/LSB-Core-generic/baselib-getopt-long-3.html

Documentation:

Gnulib module: getopt-gnu

Portability problems fixed by Gnulib:

Portability problems not fixed by Gnulib:

Gnulib provides also a module options, that fixes the following shortcomings of the getopt_long API.

These shortcomings are best illustrated with an example:

static struct option const long_options[] =
{
  { "width", required_argument, NULL, 'w' },
  { "help", no_argument, &show_help, 1 },
  { "version", no_argument, &show_version, 1 },
  { NULL, 0, NULL, 0 }
};

while ((optchar = getopt_long (argc, argv, "w:xhV", long_options, NULL))
       != -1)
  switch (optchar)
    {
    case '\0':           /* Long option with flag != NULL.  */
      break;
    case 'w':
      set_width (optarg);
      break;
    case 'x':
      do_x = true;
      break;
    case 'h':
      show_help = 1;     /* Action code duplication!  */
      break;
    case 'V':
      show_version = 1;  /* Action code duplication!  */
      break;
    default:
      usage (EXIT_FAILURE);
    }