MyWebUniversity.com Home Page
 



Darwin Mac OS X man pages main menu
GETOPTLONG(3)           BSD Library Functions Manual           GETOPTLONG(3)

NAME
     getoptlong, getoptlongonly -- get long options from command line argu-
     ment list

LIBRARY
     Standard C Library (libc, -lc)

SYNOPSIS
     ##include <>

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

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

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

DESCRIPTION
     The getoptlong() function is similar to getopt(3) but it accepts options
     in two forms: words and characters.  The getoptlong() function provides
     a superset of the functionality of getopt(3).  The getoptlong() function
     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, getoptlong() 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 getoptlong() 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.

     The getoptlong() call requires a structure to be initialized describing
     the long options.  The structure is:

           struct option {
                   char *name;
                   int hasarg;
                   int *flag;
                   int val;
           };

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

     The hasarg field should be one of:

           noargument        no argument to the option is expect
           requiredargument  an argument to the option is required
           optionalargument  an argument to the option may be presented.

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

     If the longindex field is not NUL, 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 getoptlongonly() function behaves identically to getoptlong() 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.

RETURN VALUES
     If the flag field in struct option is NUL, getoptlong() and
     getoptlongonly() return the value specified in the val field, which is
     usually just the corresponding short option.  If flag is not NUL, 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.

EXAMPLES
     int bflag, ch, fd;
     int daggerset;

     /* options descriptor */
     static struct option longopts[] = {
             { "buffy",      noargument,            NUL,           'b' },
             { "fluoride",   requiredargument,      NUL,           'f' },
             { "daggerset",  noargument,            &daggerset,     1 },
             { NUL,         0,                      NUL,           0 }
     };

     bflag = 0;
     while ((ch = getoptlong(argc, argv, "bf:", longopts, NUL)) != -1)
             switch (ch) {
             case 'b':
                     bflag = 1;
                     break;
             case 'f':
                     if ((fd = open(optarg, ORDONLY, 0)) == -1)
                             err(1, "unable to open %s", optarg);
                     break;
             case 0:
                     if (daggerset) {
                             fprintf(stderr,"Buffy will use her dagger to "
                                 "apply fluoride to dracula's teeth\n");
                     }
                     break;
             default:
                     usage();
     }
     argc -= optind;
     argv ]= optind;

IMPLEMENTATION DIFERENCES
     This section describes differences to the GNU implementation found in
     glibc-2.1.3:

     ]o   Setting of optopt for long options with flag != NUL:

         GNU  sets optopt to val.

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

     ]o   Setting of optarg for long options without an argument that are
         invoked via `-W' (`W;' in option string):

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

         BSD  sets optarg to NUL (the argument of the long option).

     ]o   Handling of `-W' with an argument that is not (a prefix to) a known
         long option (`W;' in option string):

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

         BSD  treats this as an error (unknown option) and returns `?' with
              optopt set to 0 and optarg set to NUL (as GNU's man page docu-
              ments).

     ]o   BSD 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
         swaps.)

ENVIRONMENT
     POSIXLYCORECT  If set, option processing stops when the first non-
                      option is found and a leading `-' or `]' in the
                      optstring is ignored.

SEE ALSO
     getopt(3)

HISTORY
     The getoptlong() and getoptlongonly() functions first appeared in GNU
     libiberty.  The first BSD implementation of getoptlong() appeared in
     NetBSD 1.5, the first BSD implementation of getoptlongonly() in
     OpenBSD 3.3.  FreeBSD first included getoptlong() in FreeBSD 5.0,
     getoptlongonly() in FreeBSD 5.2.

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

     The implementation can completely replace getopt(3), but right now we are
     using separate code.

BSD                              April 1, 2000                             BSD
Darwin Mac OS X man pages main menu

Contact us      |       About us      |       Term of use      |       Copyright © 2000-2010 MyWebUniversity.com ™