Museum

Home

Lab Overview

Retrotechnology Articles

Online Manuals

⇒ getopt(3) — AIX PS/2 1.2.1

Media Vault

Software Library

Restoration Projects

Artifacts Sought

Related Articles

getopt



GETOPT(3,L)                 AIX Technical Reference                 GETOPT(3,L)



-------------------------------------------------------------------------------
getopt



PURPOSE

Gets flag letters from the argument vector.

LIBRARY

Standard C Library (libc.a)

SYNTAX



   int getopt (argc, argv, optstring)     extern char *optarg;
   int argc;
   char **argv;                           extern int optind, opterr;
   char *optstring;



DESCRIPTION

The getopt subroutine returns the next flag letter in the argv parameter list
that matches a letter in the optstring parameter.  The getopt subroutine is an
aid to help programs interpret shell command-line flags that are passed to
them.

The optstring parameter is a string of recognized flag letters.  If a letter is
followed by a colon, the flag is expected to take a parameter that may or may
not be separated from it by white space.  The optarg external variable is set
to point to the start of the flag's parameter on return from the getopt
subroutine.

The getopt subroutine places the argv index of the next argument to be
processed in optind.  optind is externally initialized to 1 so that argv[0] is
not processed.

When all flags have been processed (that is, up to the first nonflag argument),
the getopt subroutine returns EOF.  The special flag "--" (dash dash) can be
used to delimit the end of the flags; EOF is returned, and "--" is skipped.

The getopt subroutine prints an error message on stderr and returns "(int) '?'"
(question mark) when it encounters a flag letter that is not included in the
optstring parameter.  You can disable this error message by setting opterr to
0.

EXAMPLES




Processed November 7, 1990        GETOPT(3,L)                                 1





GETOPT(3,L)                 AIX Technical Reference                 GETOPT(3,L)



The following code fragment processes the flags for a command that can take the
mutually exclusive flags a and b, and the flags f and o, both of which require
parameters.

  #include <unistd.h>      /* Needed for access system call constants */
  #include <stdio.h>

  main (argc, argv)
  int argc;
  char **argv;
  {
     int c;
     int aflg=0;
     int bflg=0;
     extern int optind;
     extern char *optarg;
     .
     .
     .
     while ((c = getopt(argc, argv, "abf:o:")) != EOF)
     {
        switch (c)
        {
           case 'a':
              if (bflg)
                 errflg++;
              else
                 aflg++;
              break;
           case 'b':
              if (aflg)
                 errflg++;
              else
                 bflg++;
              break;
           case 'f':
              ifile = optarg;
              break;
           case 'o':
              ofile = optarg;
              break;
           case '?':
              errflg++;
        } /* case */

        if (errflg)
        {
           fprintf(stderr, "usage:...");
           exit(2);
        }
     } /* while */
        if (aflg) printf ("-a flag seen\n");



Processed November 7, 1990        GETOPT(3,L)                                 2





GETOPT(3,L)                 AIX Technical Reference                 GETOPT(3,L)



        if (bflg) printf ("-b flag seen\n");

     for ( ; optind < argc; optind++)
     {
        if (access(argv[optind], R_OK))
        {
        .
        .
        .
        }
     } /* for  */
  }    /* main */

RELATED INFORMATION

The getopt command in AIX Operating System Commands Reference.







































Processed November 7, 1990        GETOPT(3,L)                                 3



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