Museum

Home

Lab Overview

Retrotechnology Articles

Online Manuals

⇒ getopt(S) — OpenDesktop Software Development System 1.0.0d

Media Vault

Software Library

Restoration Projects

Artifacts Sought



     GETOPT(S)                 UNIX System V                 GETOPT(S)



     Name
          getopt - get option letter from argument vector

     Syntax
          int getopt (argc, argv, optstring)
          int argc;
          char **argv, *opstring;

          extern char *optarg;
          extern int optind, opterr;

     Description
          The getopt function returns the next option letter in argv
          that matches a letter in optstring.  It supports all the
          rules of the command syntax standard (see intro(C)).  So all
          new commands will adhere to the command syntax standard,
          they should use getopts(C) or getopt(S) to parse positional
          parameters and check for options that are legal for that
          command.

          optstring must contain the option letters the command using
          getopt will recognize; if a letter is followed by a colon,
          the option is expected to have an argument, or group of
          arguments, which must be separated from it by white space.

          optarg is set to point to the start of the option-argument
          on return from getopt.

          getopt places in optind the argv index of the next argument
          to be processed.  optind is external and is initialized to 1
          before the first call to getopt.

          When all options have been processed (that is, up to the
          first non-option argument), getopt returns -1.  The special
          option ``--'' may be used to delimit the end of the options;
          when it is encountered, -1 will be returned, and ``--'' will
          be skipped.

          The following rules comprise the System V standard for
          command-line syntax:

          RULE 1         Command names must be between two and nine
                         characters.

          RULE 2         Command names must include lowercase letters
                         and digits only.

          RULE 3         Option names must be a single character in
                         length.

          RULE 4         All options must be delimited by the -
                         character.
          RULE 5         Options with no arguments may be grouped
                         behind one delimiter.

          RULE 6         The first option-argument following an option
                         must be preceded by white space.

          RULE 7         Option arguments cannot be optional.

          RULE 8         Groups of option arguments following an
                         option must be separated by commas or
                         separated by white space and quoted.

          RULE 9         All options must precede operands on the
                         command line.

          RULE 10        The characters -- may be used to delimit the
                         end of the options.

          RULE 11        The order of options relative to one another
                         should not matter.

          RULE 12        The order of operands may matter and
                         position-related interpretations should be
                         determined on a command-specific basis.

          RULE 13        The - character preceded and followed by
                         white space should be used only to mean
                         standard input.

          The function getopt is the command-line parser that will
          enforce the rules of this command syntax standard.

     Example
          The following code fragment shows how one might process the
          arguments for a command that can take the mutually exclusive
          options a and b, and the option o, which requires an
          option-argument:

          main (argc, argv)
          int argc;
          char **argv;
          {
               int c;
               extern char *optarg;
               extern int optind;
               .
               .
               .
               while ((c = getopt(argc, argv, "abo:")) != -1)
                    switch (c) {
                    case 'a':
                         if (bflg)
                              errflg++;
                         else
                              aflg++;
                         break;
                    case 'b':
                         if (aflg)
                              errflg++;
                         else
                              bproc( );
                         break;
                    case 'o':
                         ofile = optarg;
                         break;
                    case '?':
                         errflg++;
                    }
               if (errflg) {
                    (void)fprintf(stderr, "usage: . . . ");
                    exit (1);
               }
               for ( ; optind < argc; optind++) {
                    if (access(argv[optind], 4)) {
               .
               .
               .
          }

     See Also
          getopts(C), intro(C)

     Diagnostics
          getopt prints an error message on standard error and returns
          a question mark (?) when it encounters an option letter not
          included in optstring or no option-argument after an option
          that expects one.  This error message may be disabled by
          setting opterr to 0.

     Warning
          Although the following command syntax rule (see intro(C))
          relaxations are permitted under the current implementation,
          they should not be used because they may not be supported in
          future releases of the system.  As in the Example section
          above, a and b are options, and the option o requires an
          option-argument:

          cmd -aboxxx file    (Rule 5 violation: options with
                               option-arguments must not be
                               grouped with other options)
          cmd -ab -oxxx file  (Rule 6 violation: there must be
                               white space after an option that
                               takes an option-argument)

          Changing the value of the variable optind or calling getopt
          with different values of argv may lead to unexpected
          results.

     Standards Conformance
          getopt is conformant with:
          AT&T SVID Issue 2, Select Code 307-127;
          and The X/Open Portability Guide II of January 1987.

                                             (printed 6/20/89)



Typewritten Software • bear@typewritten.org • Edmonds, WA 98026