home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 35 Internet
/
35-Internet.zip
/
rftp.zip
/
getopt.3r
< prev
next >
Wrap
Text File
|
1995-08-15
|
7KB
|
182 lines
getopt(3r) getopt(3r)
───────────────────────────────────────────────────────────────────────────────
NAME
getopt - parse options from REXX program command line
SYNOPSIS
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
init_getopt subroutine. 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 program.
getopt.1 through getopt.n
Specifies the list of parameters passed to the program.
getopt.!errmsg
Contains the error message printed to the default output
stream when getopt encounters an error. Useful if
getopt.!opterr has been set to 0 (zero).
getopt.!optarg
Stores the value of the option argument found by getopt.
getopt.!opterr
If the application has not set this variable to 0 (zero),
getopt prints diagnostic messages to the default output
stream. Initialized to 1 (zero) by init_getopt.
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
init_getopt.
getopt.!sp
Used internally by getopt. Initialized by init_getopt.
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 argv
call init_getopt argv
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
init_getopt(3r)
PROGRAM AUTHOR
Lawrence R. "Rod" Buchanan
COPYRIGHT
Copyright (c) 1994-95 Lawrence R Buchanan. ALL RIGHTS RESERVED.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.