MirBSD manpage: getopt_long(3), getopt_long_only(3)

GETOPT_LONG(3)             BSD Programmer's Manual              GETOPT_LONG(3)


     getopt_long, getopt_long_only - get long options from command line argu-
     ment list


     #include <getopt.h>

     extern char *optarg;
     extern int optind;
     extern int optopt;
     extern int opterr;
     extern int optreset;

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

     getopt_long_only(int argc, char * const *argv, const char *optstring,
             const struct option *longopts, int *longindex);


     The getopt_long() function is similar to getopt(3) but it accepts options
     in two forms: words and characters. The getopt_long() function provides a
     superset of the functionality of getopt(3). getopt_long() can be used in
     two ways. In the first way, every long option understood by the program
     has a corresponding short option, and the option structure is only used
     to translate from long options to short options. When used in this
     fashion, getopt_long() behaves identically to getopt(3). This is a good
     way to add long option processing to an existing program with the minimum
     of rewriting.

     In the second mechanism, a long option sets a flag in the option struc-
     ture passed, or will store a pointer to the command line argument in the
     option structure passed to it for options that take arguments. Addition-
     ally, the long option's argument may be specified as a single argument
     with an equal sign, e.g.

           $ myprogram --myoption=somevalue

     When a long option is processed, the call to getopt_long() will return 0.
     For this reason, long option processing without shortcuts is not back-
     wards compatible with getopt(3).

     It is possible to combine these methods, providing for long options pro-
     cessing with short option equivalents for some options. Less frequently
     used options would be processed as long options only.

     Abbreviated long option names are accepted when getopt_long() processes
     long options if the abbreviation is unique. An exact match is always pre-
     ferred for a defined long option.

     The getopt_long() call requires an array to be initialized describing the
     long options. Each element of the array is a structure:

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

     The name field should contain the option name without the leading double

     The has_arg field should be one of:

           no_argument        no argument to the option is expected.
           required_argument  an argument to the option is required.
           optional_argument  an argument to the option may be presented.

     If flag is not NULL, then the integer pointed to by it will be set to the
     value in the val field. If the flag field is NULL, then the val field
     will be returned. Setting flag to NULL and setting val to the correspond-
     ing short option will make this function act just like getopt(3).

     If the longindex field is not NULL, then the integer pointed to by it
     will be set to the index of the long option relative to longopts.

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

     The getopt_long_only() function behaves identically to getopt_long() with
     the exception that long options may start with '-' in addition to '--'.
     If an option starting with '-' does not match a long option but does
     match a single-character option, the single-character option is returned.


     If the flag field in struct option is NULL, getopt_long() and
     getopt_long_only() return the value specified in the val field, which is
     usually just the corresponding short option. If flag is not NULL, these
     functions return 0 and store val in the location pointed to by flag.
     These functions return ':' if there was a missing option argument, '?' if
     the user specified an unknown or ambiguous option, and -1 when the argu-
     ment list has been exhausted.


     This section describes differences to the GNU implementation found in

     •   handling of '-' within the option string (not the first character):

         GNU      treats a '-' on the command line as a non-argument.

         OpenBSD  a '-' within the option string matches a '-' (single dash)
                  on the command line. This functionality is provided for
                  backward compatibility with programs, such as su(1), that
                  use '-' as an option flag. This practice is wrong, and
                  should not be used in any current development.

     •   handling of '::' in the option string in the presence of

         Both     GNU and OpenBSD ignore POSIXLY_CORRECT here and take '::' to
                  mean the preceding option takes an optional argument.

     •   return value in case of missing argument if first character (after
         '+' or '-') in the option string is not ':':

         GNU      returns '?'

         OpenBSD  returns ':' (since OpenBSD's getopt(3) does).

     •   handling of '--a' in getopt(3):

         GNU      parses this as option '-', option 'a'.

         OpenBSD  parses this as '--', and returns -1 (ignoring the 'a') (be-
                  cause the original getopt() did.)

     •   setting of optopt for long options with flag non-NULL:

         GNU      sets optopt to val.

         OpenBSD  sets optopt to 0 (since val would never be returned).

     •   handling of '-W' with 'W;' in the option string in getopt(3) (not

         GNU      causes a segmentation fault.

         OpenBSD  no special handling is done; 'W;' is interpreted as two
                  separate options, neither of which take an argument.

     •   setting of optarg for long options without an argument that are in-
         voked via '-W' (with 'W;' in the option string):

         GNU      sets optarg to the option name (the argument of '-W').

         OpenBSD  sets optarg to NULL (the argument of the long option).

     •   handling of '-W' with an argument that is not (a prefix to) a known
         long option (with 'W;' in the option string):

         GNU      returns '-W' with optarg set to the unknown option.

         OpenBSD  treats this as an error (unknown option) and returns '?'
                  with optopt set to 0 and optarg set to NULL (as GNU's man
                  page documents).

     •   The error messages are different.

     •   OpenBSD does not permute the argument vector at the same points in
         the calling sequence as GNU does. The aspects normally used by the
         caller (ordering after -1 is returned, value of optind relative to
         current positions) are the same, though. (We do fewer variable


     POSIXLY_CORRECT  If set, option processing stops when the first non-
                      option is found and a leading '+' in the optstring is


     int bflag, ch, fd;
     int daggerset;

     /* options descriptor */
     static struct option longopts[] = {
             { "buffy",      no_argument,            NULL,           'b' },
             { "fluoride",   required_argument,      NULL,           'f' },
             { "daggerset",  no_argument,            &daggerset,     1 },
             { NULL,         0,                      NULL,           0 }

     bflag = 0;
     while ((ch = getopt_long(argc, argv, "bf:", longopts, NULL)) != -1)
             switch (ch) {
             case 'b':
                     bflag = 1;
             case 'f':
                     if ((fd = open(optarg, O_RDONLY, 0)) == -1)
                             err(1, "unable to open %s", optarg);
             case 0:
                     if (daggerset)
                             fprintf(stderr, "Buffy will use her dagger to "
                                 "apply fluoride to dracula's teeth\n");
                     /* NOTREACHED */
     argc -= optind;
     argv += optind;




     The getopt_long() and getopt_long_only() functions first appeared in GNU
     libiberty. This implementation first appeared in OpenBSD 3.3.


     The argv argument is not really const as its elements may be permuted
     (unless POSIXLY_CORRECT is set).

MirBSD #10-current             December 4, 2011                              3

Generated on 2022-12-24 01:00:14 by $MirOS: src/scripts/roff2htm,v 1.113 2022/12/21 23:14:31 tg Exp $ — This product includes material provided by mirabilos.

These manual pages and other documentation are copyrighted by their respective writers; their sources are available at the project’s CVSweb, AnonCVS and other mirrors. The rest is Copyright © 2002–2022 MirBSD.

This manual page’s HTML representation is supposed to be valid XHTML/1.1; if not, please send a bug report — diffs preferred.

Kontakt / Impressum & Datenschutzerklärung