home *** CD-ROM | disk | FTP | other *** search
- GetOpt(3) GetOpt(3)
- ─────────────────────────────────────────────────────────────────────────────
-
- NAME
- GetOpt - parse options from REXX program command line
-
- SYNTAX
- ch = GetOpt( optstr )
- .
- .
- GetOpt: procedure expose GetOpt.
- parse arg optstr
-
- DESCRIPTION
- The GetOpt subroutine helps a REXX program interpret command-line flags
- that are passed to it. The GetOpt subroutine processes the GetOpt. stem
- variable, which must have been prepared by the SetupArg subroutine prior
- to calling GetOpt. The optstr parameter is a string of recognized flag
- letters. If a letter is followed by a : (colon), the flag takes an
- argument.
-
- GetOpt._optind indexes the next element of the GetOpt. stem variable to
- be processed. It is initialized to 1 (one) and the GetOpt subroutine
- updates it after calling each element of the GetOpt. parameter.
-
- The GetOpt subroutine returns the next flag letter in the GetOpt. stem
- variable that matches a letter in the optstr parameter. If the flag
- takes an argument, the GetOpt subroutine sets GetOpt._optarg to the
- argument as follows:
-
- * If the flag was the last letter in the string pointed to by an
- element of the GetOpt. stem variable, GetOpt._optarg contains the next
- element of the GetOpt. stem variable and GetOpt._optind is incremented by 2
- (two). If the resulting value of GetOpt._optind is not less than
- GetOpt.0, this indicates a missing flag argument, and the GetOpt
- subroutine returns an error message.
-
- * Otherwise, GetOpt._optarg points to the string following the flag
- letter in that element of the GetOpt. stem variable and GetOpt._optind
- is incremented by 1 (one).
-
- PARAMETERS
- optstr Specifies 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.
-
- GLOBAL VARIABLES
- GetOpt uses a stem variable named GetOpt., which contains the following
- stems:
-
- GetOpt.0
- Specifies the number of parameters passed to the routine.
-
- GetOpt.1 through GetOpt.n
- Specifies the list of parameters passed to the routine.
-
- GetOpt._optarg
- Stores the value of the option argument found by GetOpt.
-
- GetOpt._opterr
- If application has not set this variable to 0 (zero), the GetOpt
- subroutine prints a diagnostic message to the default output
- stream.
-
- GetOpt._optind
- Contains the index of the next argument to be processed.
-
- GetOpt._optopt
- When GetOpt detects an error and returns '?', GetOpt._optopt
- contains the option character that caused the error.
-
- GetOpt._program
- Contains the name of the REXX program. Initialized by SetupArg.
-
- GetOpt._sp
- Used internally by GetOpt. Initialized by SetupArg.
-
- To avoid problems, do not use variables named _optarg, _opterr, _optind,
- _optopt, _program, or _sp in your program.
-
- RETURN VALUES
- The GetOpt subroutine returns the next flag letter specified on the
- command line. A value of -1 (negative one) is returned when all command
- line flags have been parsed. When the value of the GetOpt._optind
- parameter is NULL, is not the - (minus) character, or is the "-" (minus)
- string, the GetOpt subroutine returns a value of -1 (negative one)
- without changing the value. If the value of the GetOpt._optind
- parameter is the "--" (dash) string, the GetOpt subroutine returns a
- value of -1 (negative one) after incrementing the value of the
- GetOpt._optind parameter.
-
- ERROR CODES
- If the GetOpt subroutine encounters an option character that is not
- contained in optstr, a ? (question mark) character is returned. If it
- detects a missing option argument, it returns a ? (question mark). In
- either case, the GetOpt subroutine sets the GetOpt._optopt variable to
- the option character that caused the error. If the application has not
- set the variable GetOpt._opterr to 0 (zero), the GetOpt subroutine also
- prints a diagnostic message to the default output stream.
-
- EXAMPLE
- 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.
-
- /* */
-
- parse arg args
- call SetupArg args
-
- aflg = 0
- eflg = 0
- errflg = 0
-
- optstr = "abf:o:"
- c = GetOpt(optstr)
- do while c \= -1
- select
- when c = 'a' then
- if bflg \= 0 then
- errflg = errflg + 1
- else
- aflg = aflg + 1
- when c = 'b' then
- if aflg \= 0 then
- errflg = errflg + 1
- else
- bflg = bflg + 1
- when c = 'f' then
- ifile = GetOpt._optarg
- when c = 'o' then
- ofile = GetOpt._optarg
- when c = '?' then
- errflg = errflg + 1
- end /* select */
-
- if errflg > 0 then do
- say "usage: . . . "
- exit 2
- end
-
- c = GetOpt(optstr)
- end /* do while */
-
- exit
-
- NOTES
- This GetOpt was patterned after the public domain version that is
- provided by the AT&T UNIX(tm) System Toolchest.
-
- SEE ALSO
- SetupArg(3)
-
- COPYRIGHT
- Copyright (c) 1994 Lawrence R Buchanan.
-