home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 18 REXX / 18-REXX.zip / rxhll.zip / PARSEOPT.REX < prev    next >
OS/2 REXX Batch file  |  1993-11-21  |  5KB  |  116 lines

  1.  
  2. /* #include <parseopt.rex> */
  3.  
  4. ParseOptions: procedure expose Opt.
  5.    /**
  6.    ***  This will parse the command line options.  Those parameters that
  7.    ***  begin with a minus (-) or forward slash (/) are considered flags
  8.    ***  and are placed in Opt.Flag.   The remaining options are placed
  9.    ***  into Opt.parm.<x>.
  10.    ***
  11.    ***  NOTE:  This code does not clear out the 'Opt.' stem variable since
  12.    ***         the caller may want to establish defaults prior to calling
  13.    ***         this code.
  14.    ***
  15.    ***  LIMITATIONS:  The code currently only looks for the double quote
  16.    ***         character (").  The apostrophe is treated like any other
  17.    ***         character.  The way this is currently coded, multiple blanks
  18.    ***         in a quoted string are compressed to a single blanks and
  19.    ***         probably should not be.
  20.    ***
  21.    **/
  22.  
  23.    parse arg arguments
  24.  
  25.    Opt.Flag.List = ''
  26.    Opt.State = 'Normal'
  27.    j = 0
  28.    do i = 1 to words(arguments)
  29.       argument = word(arguments, i)
  30.  
  31.       select
  32.          when Opt.State = 'Quoted Positional' then
  33.             do
  34.             /* Keep appending the words to this parm until an ending quote */
  35.             /* is found.                                                   */
  36.  
  37.             Opt.Parm.j = Opt.Parm.j argument
  38.             if right(argument,1) = '"' then
  39.                do
  40.                Opt.Parm.j = strip(Opt.Parm.j, 'Both', '"')
  41.                Opt.State = 'Normal'
  42.                end
  43.             end
  44.          when Opt.State = 'Quoted Flag' then
  45.             do
  46.             /* Keep appending until the terminating quote is found */
  47.  
  48.             Opt.Flag.Flagname = Opt.Flag.FlagName argument
  49.             if right(argument,1) = '"' then
  50.                do
  51.                Opt.Flag.Flagname = strip(Opt.Flag.Flagname, 'Both', '"')
  52.                Opt.State = 'Normal'
  53.                end
  54.             end
  55.          when Opt.State = 'Normal' then
  56.             do
  57.             FirstChar = left(argument, 1)
  58.             if ((FirstChar = '-') | (FirstChar = '/')) then
  59.                do
  60.                /*  This is a flag.  The value of the flag is the remainder of  */
  61.                /*  the string.  If the remainder is the null string, then it   */
  62.                /*  has an implicit value of '+' implying "on" or "true"        */
  63.  
  64.                FlagName = substr(argument, 2, 1)   /* Second character     */
  65.                FlagName = translate(FlagName)      /* Convert to uppercase */
  66.  
  67.                /* See if this flag parm is quoted */
  68.  
  69.                if substr(argument, 3, 1) = '"' then
  70.                   Opt.State = 'Quoted Flag'
  71.  
  72.                /* If any of the flag names are not a valid character for a REXX */
  73.                /* variable, we have to translate into a mnemonic.               */
  74.  
  75.                if ((FlagName < 'A') | (FlagName > 'Z')) then
  76.                   do
  77.                   select
  78.                      when FlagName = '?' then FlagName = 'SYNTAX'
  79.                      when FlagName = '!' then FlagName = 'BANG'
  80.                      when FlagName = '*' then FlagName = 'STAR'
  81.                      when FlagName = '#' then FlagName = 'POUND'
  82.                      when FlagName = '$' then FlagName = 'DOLLAR'
  83.                      when FlagName = '%' then FlagName = 'PERCENT'
  84.                      when FlagName = '^' then FlagName = 'HAT'
  85.                      when FlagName = '&' then FlagName = 'AMP'
  86.                      when FlagName = '(' then FlagName = 'LPAR'
  87.                      when FlagName = ')' then FlagName = 'RPAR'
  88.                      when FlagName = '-' then FlagName = 'DASH'
  89.                      when FlagName = '=' then FlagName = 'EQUAL'
  90.                      otherwise /* Force a syntax message */
  91.                         FlagName = 'SYNTAX'
  92.                   end /* select */
  93.                   end /* if */
  94.  
  95.                FlagValue = substr(argument, 3)     /* Remainder of string */
  96.                if FlagValue = '' then
  97.                   FlagValue = '+'
  98.  
  99.                Opt.Flag.FlagName = FlagValue
  100.                Opt.Flag.List = FlagName Opt.Flag.List
  101.                end
  102.             else /* it is a positional parameter */
  103.                do
  104.                j = j + 1
  105.                Opt.Parm.j = argument
  106.                if left(argument,1) = '"' then
  107.                   Opt.State = 'Quoted Positional'
  108.                end
  109.          end /* 'Normal' */
  110.       otherwise
  111.          nop
  112.       end /* select */
  113.    end /* do i... */
  114.    Opt.Parm.0 = j
  115.    return
  116.