home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_progs / prog_c / edlib.lzh / EDLIB / MAN / GETOPT < prev    next >
Encoding:
Text File  |  1991-08-16  |  3.6 KB  |  119 lines

  1. GETOPT(3)                  Library Functions                 GETOPT(3)
  2.  
  3.  
  4.  
  5. NAME
  6.      getopt - get option letter from argv
  7.  
  8. SYNOPSIS
  9.      #include <edlib.h>
  10.  
  11.      int getopt(argc, argv, optstring)
  12.      inta argc;
  13.  
  14.      char **optstring;
  15.  
  16.      extern char *optarg;
  17.      extern int optind;
  18.  
  19. DESCRIPTION
  20.      Getopt returns the next option letter in argv that matches a
  21.      letter in optstring.  Optstring is a string of recognized
  22.      option letters; if a letter is followed by a colon, the
  23.      option is expected to have an argument that may or may not
  24.      be separated from it by white space.  Optarg is set to point
  25.      to the start of the option argument on return from getopt.
  26.  
  27.      Getopt places in optind the argv index of the next argument
  28.      to be processed.  Because optind is external, it is normally
  29.      initialized to zero automatically before the first call to
  30.      getopt.
  31.  
  32.      When an option that is not in the list occurs, a NULL is
  33.      returned and the optarg pointer is set to point to the
  34.      first character of the null terminated string. This is done
  35.      so that options may be specified with other parameters
  36.      interspersed between them.
  37.  
  38. DIAGNOSTICS
  39.      Getopt prints an error message on stderr and returns a ques-
  40.      tion mark (?) when it encounters an option letter not
  41.      included in optstring.
  42.  
  43. EXAMPLE
  44.      The following code fragment shows how one might process the
  45.      arguments for a command that can take the mutually exclusive
  46.      options a and b, and the options f and o, both of which
  47.      require arguments:
  48.  
  49.           main(argc, argv)
  50.           int argc;
  51.           char **argv;
  52.           {
  53.                int c;
  54.                extern int optind;
  55.                extern char *optarg;
  56.                .
  57.                .
  58.                .
  59.                 while ((c = getopt(argc, argv, "abf:o:")) != EOF)
  60.                     switch (c) {
  61.                     case `a':
  62.                          if (bflg)
  63.                               errflg++;
  64.                          else
  65.                               aflg++;
  66.                          break;
  67.                     case `b':
  68.                          if (aflg)
  69.                               errflg++;
  70.                          else
  71.                               bproc();
  72.                          break;
  73.                     case `f':
  74.                          ifile = optarg;
  75.                          break;
  76.                     case `o':
  77.                          ofile = optarg;
  78.                          break;
  79.                     case `?':
  80.                     default:
  81.                          errflg++;
  82.                          break;
  83.                     }
  84.                if (errflg) {
  85.                     fprintf(stderr, "Usage: ...");
  86.                     exit(2);
  87.                }
  88.                for (; optind < argc; optind++) {
  89.                     .
  90.                     .
  91.                     .
  92.                }
  93.                .
  94.                .
  95.                .
  96.           }
  97.  
  98. HISTORY
  99.      Written by Henry Spencer, working from a Bell Labs manual
  100.      page.  Modified by Keith Bostic to behave more like the Sys-
  101.      tem V version. Ported to the Amiga and modified to take
  102.      options anywhere by Edwin (Deepthot) Hoogerbeets.
  103.  
  104. BUGS
  105.      It is not obvious how `-' standing alone should be treated;
  106.      this version treats it as a non-option argument, which is
  107.      not always right.
  108.  
  109.      Option arguments are allowed to begin with `-'; this is rea-
  110.      sonable but reduces the amount of error checking possible.
  111.      Getopt is quite flexible but the obvious price must be paid:
  112.      there is much it could do that it doesn't, like checking
  113.      mutually exclusive options, checking type of option argu-
  114.      ments, etc.
  115.  
  116.  
  117.  
  118.  
  119.