getopt_long(3) — Linux manual page

NAME | LIBRARY | SYNOPSIS | DESCRIPTION | RETURN VALUE | ENVIRONMENT | ATTRIBUTES | STANDARDS | EXAMPLES | SEE ALSO | COLOPHON

getopt_long(3)           Library Functions Manual          getopt_long(3)

NAME         top

       getopt_long - parse command-line options

LIBRARY         top

       Standard C library (libc, -lc)

SYNOPSIS         top

       #define _GNU_SOURCE
       #include <getopt.h>

       int getopt_long(int argc, char *argv[],
                  const char *optstring,
                  const struct option *longopts, int *longindex);

DESCRIPTION         top

       The getopt_long() function works like getopt(3) except that it
       also accepts long options, started with two dashes.  (If the
       program accepts only long options, then optstring should be
       specified as an empty string (""), not NULL.)  Long option names
       may be abbreviated if the abbreviation is unique or is an exact
       match for some defined option.  A long option may take a
       parameter, of the form --arg=param or --arg param.

       longopts is a pointer to the first element of an array of struct
       option declared in <getopt.h> as

           struct option {
               const char *name;
               int         has_arg;
               int        *flag;
               int         val;
           };

       The meanings of the different fields are:

       name   is the name of the long option.

       has_arg
              is: no_argument (or 0) if the option does not take an
              argument; required_argument (or 1) if the option requires
              an argument; or optional_argument (or 2) if the option
              takes an optional argument.

       flag   specifies how results are returned for a long option.  If
              flag is NULL, then getopt_long() returns val.  (For
              example, the calling program may set val to the equivalent
              short option character.)  Otherwise, getopt_long() returns
              0, and flag points to a variable which is set to val if the
              option is found, but left unchanged if the option is not
              found.

       val    is the value to return, or to load into the variable
              pointed to by flag.

       The last element of the array has to be filled with zeros.

       If longindex is not NULL, it points to a variable which is set to
       the index of the long option relative to longopts.

       Its analogue to getopt(3)'s optopt is “argv[(optind - 1)]”.

RETURN VALUE         top

       See getopt(3).

       getopt_long() also returns the option character when a short
       option is recognized.  For a long option, it returns val if flag
       is NULL, and 0 otherwise.  Error and -1 returns are the same as
       for getopt(3), plus '?' for an ambiguous match or an extraneous
       parameter.

ENVIRONMENT         top

       See getopt(3).

ATTRIBUTES         top

       For an explanation of the terms used in this section, see
       attributes(7).
       ┌───────────────┬───────────────┬────────────────────────────────┐
       │ Interface     Attribute     Value                          │
       ├───────────────┼───────────────┼────────────────────────────────┤
       │ getopt_long() │ Thread safety │ MT-Unsafe race:getopt env      │
       └───────────────┴───────────────┴────────────────────────────────┘

STANDARDS         top

       GNU.

EXAMPLES         top

       The following example program illustrates the use of getopt_long()
       with most of its features.

       #include <getopt.h>
       #include <stdio.h>
       #include <stdlib.h>
       #include <unistd.h>

       int
       main(int argc, char *argv[])
       {
           int c;
           int digit_optind = 0;

           for (;;) {
               int this_option_optind = optind ? optind : 1;
               int option_index = 0;
               static struct option long_options[] = {
                   {"add",     required_argument, 0,  0 },
                   {"append",  no_argument,       0,  0 },
                   {"delete",  required_argument, 0,  0 },
                   {"verbose", no_argument,       0,  0 },
                   {"create",  required_argument, 0, 'c'},
                   {"file",    required_argument, 0,  0 },
                   {0,         0,                 0,  0 }
               };

               c = getopt_long(argc, argv, "abc:d:012",
                               long_options, &option_index);
               if (c == -1)
                   break;

               switch (c) {
               case 0:
                   printf("option %s", long_options[option_index].name);
                   if (optarg)
                       printf(" with arg %s", optarg);
                   printf("\n");
                   break;

               case '0':
               case '1':
               case '2':
                   if (digit_optind != 0 && digit_optind != this_option_optind)
                     printf("digits occur in two different argv-elements.\n");
                   digit_optind = this_option_optind;
                   printf("option %c\n", c);
                   break;

               case 'a':
                   printf("option a\n");
                   break;

               case 'b':
                   printf("option b\n");
                   break;

               case 'c':
                   printf("option c with value '%s'\n", optarg);
                   break;

               case 'd':
                   printf("option d with value '%s'\n", optarg);
                   break;

               case '?':
                   break;

               default:
                   printf("?? getopt returned character code 0%o ??\n", c);
               }
           }

           if (optind < argc) {
               printf("non-option ARGV-elements: ");
               while (optind < argc)
                   printf("%s ", argv[optind++]);
               printf("\n");
           }

           exit(EXIT_SUCCESS);
       }

SEE ALSO         top

       getopt(1), getopt(3), getopt_long_only(3), getsubopt(3)

COLOPHON         top

       This page is part of the man-pages (Linux kernel and C library
       user-space interface documentation) project.  Information about
       the project can be found at 
       ⟨https://www.kernel.org/doc/man-pages/⟩.  If you have a bug report
       for this manual page, see
       ⟨https://git.kernel.org/pub/scm/docs/man-pages/man-pages.git/tree/CONTRIBUTING⟩.
       This page was obtained from the tarball man-pages-6.18.tar.gz
       fetched from
       ⟨https://mirrors.edge.kernel.org/pub/linux/docs/man-pages/⟩ on
       2026-05-24.  If you discover any rendering problems in this HTML
       version of the page, or you believe there is a better or more up-
       to-date source for the page, or you have corrections or
       improvements to the information in this COLOPHON (which is not
       part of the original manual page), send a mail to
       man-pages@man7.org

Linux man-pages 6.18            2026-02-14                 getopt_long(3)

Pages that refer to this page: tcpdump(1)getopt(3)getopt_long_only(3)pmgetoptions(3)tcpdump(8)