home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 18 REXX
/
18-REXX.zip
/
rxhll.zip
/
PARSEOPT.REX
< prev
next >
Wrap
OS/2 REXX Batch file
|
1993-11-21
|
5KB
|
116 lines
/* #include <parseopt.rex> */
ParseOptions: procedure expose Opt.
/**
*** This will parse the command line options. Those parameters that
*** begin with a minus (-) or forward slash (/) are considered flags
*** and are placed in Opt.Flag. The remaining options are placed
*** into Opt.parm.<x>.
***
*** NOTE: This code does not clear out the 'Opt.' stem variable since
*** the caller may want to establish defaults prior to calling
*** this code.
***
*** LIMITATIONS: The code currently only looks for the double quote
*** character ("). The apostrophe is treated like any other
*** character. The way this is currently coded, multiple blanks
*** in a quoted string are compressed to a single blanks and
*** probably should not be.
***
**/
parse arg arguments
Opt.Flag.List = ''
Opt.State = 'Normal'
j = 0
do i = 1 to words(arguments)
argument = word(arguments, i)
select
when Opt.State = 'Quoted Positional' then
do
/* Keep appending the words to this parm until an ending quote */
/* is found. */
Opt.Parm.j = Opt.Parm.j argument
if right(argument,1) = '"' then
do
Opt.Parm.j = strip(Opt.Parm.j, 'Both', '"')
Opt.State = 'Normal'
end
end
when Opt.State = 'Quoted Flag' then
do
/* Keep appending until the terminating quote is found */
Opt.Flag.Flagname = Opt.Flag.FlagName argument
if right(argument,1) = '"' then
do
Opt.Flag.Flagname = strip(Opt.Flag.Flagname, 'Both', '"')
Opt.State = 'Normal'
end
end
when Opt.State = 'Normal' then
do
FirstChar = left(argument, 1)
if ((FirstChar = '-') | (FirstChar = '/')) then
do
/* This is a flag. The value of the flag is the remainder of */
/* the string. If the remainder is the null string, then it */
/* has an implicit value of '+' implying "on" or "true" */
FlagName = substr(argument, 2, 1) /* Second character */
FlagName = translate(FlagName) /* Convert to uppercase */
/* See if this flag parm is quoted */
if substr(argument, 3, 1) = '"' then
Opt.State = 'Quoted Flag'
/* If any of the flag names are not a valid character for a REXX */
/* variable, we have to translate into a mnemonic. */
if ((FlagName < 'A') | (FlagName > 'Z')) then
do
select
when FlagName = '?' then FlagName = 'SYNTAX'
when FlagName = '!' then FlagName = 'BANG'
when FlagName = '*' then FlagName = 'STAR'
when FlagName = '#' then FlagName = 'POUND'
when FlagName = '$' then FlagName = 'DOLLAR'
when FlagName = '%' then FlagName = 'PERCENT'
when FlagName = '^' then FlagName = 'HAT'
when FlagName = '&' then FlagName = 'AMP'
when FlagName = '(' then FlagName = 'LPAR'
when FlagName = ')' then FlagName = 'RPAR'
when FlagName = '-' then FlagName = 'DASH'
when FlagName = '=' then FlagName = 'EQUAL'
otherwise /* Force a syntax message */
FlagName = 'SYNTAX'
end /* select */
end /* if */
FlagValue = substr(argument, 3) /* Remainder of string */
if FlagValue = '' then
FlagValue = '+'
Opt.Flag.FlagName = FlagValue
Opt.Flag.List = FlagName Opt.Flag.List
end
else /* it is a positional parameter */
do
j = j + 1
Opt.Parm.j = argument
if left(argument,1) = '"' then
Opt.State = 'Quoted Positional'
end
end /* 'Normal' */
otherwise
nop
end /* select */
end /* do i... */
Opt.Parm.0 = j
return