home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 6 File / 06-File.zip / comnt110.zip / rxGetOpt.Cmd < prev   
OS/2 REXX Batch file  |  1996-02-14  |  8KB  |  182 lines

  1. /***************************************************************************
  2.  * rxGetOpt.Cmd 2.01 (14-Feb-1996 21:07:35)
  3.  * Copyright 1995 Christopher J. Madsen
  4.  *
  5.  * Parse options specified on command line.
  6.  ***************************************************************************/
  7. PARSE SOURCE . called_as .
  8.  
  9. IF called_as = "COMMAND" THEN DO
  10.   SAY 'RxGetOpt version 2.01 (14-Feb-1996)'
  11.   SAY 'Copyright 1995 Christopher J. Madsen'
  12.   SAY
  13.   SAY 'RxGetOpt is free software; you can redistribute it and/or modify it'
  14.   SAY 'under the terms of the GNU General Public License as published by'
  15.   SAY 'the Free Software Foundation; either version 2 of the License, or'
  16.   SAY '(at your option) any later version.'
  17.   SAY
  18.   SAY 'RxGetOpt is distributed in the hope that it will be useful, but'
  19.   SAY 'WITHOUT ANY WARRANTY; without even the implied warranty of'
  20.   SAY 'MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU'
  21.   SAY 'General Public License for more details.'
  22.   SAY
  23.   SAY 'You should have received a copy of the GNU General Public License'
  24.   SAY 'along with this program; if not, write to the Free Software'
  25.   SAY 'Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.'
  26.   SAY
  27.   SAY 'My Addresses:'
  28.   SAY 'Internet: ac608@yfn.ysu.edu     USnail: Christopher J. Madsen'
  29.   SAY '                                        3222 Darby Ln.'
  30.   SAY '                                        Denton, TX  76207-1305'
  31.   SAY
  32.   SAY 'Error:  RxGetOpt must be called as a function from a REXX program.'
  33.   EXIT
  34. END /* if not called as function */
  35.  
  36. DROP called_as
  37.  
  38. /***************************************************************************
  39.  * Global variables:
  40.  ***************************************************************************/
  41. optionChars = '-/'              /* Characters which signify options */
  42. optionChar  = '-'               /* Preferred character to signify option */
  43. quoteChars  = '"'''             /* Characters which quote arguments */
  44.  
  45. globalVars = 'argOptions commandLine forceUpper longOptions optionChar',
  46.   'optionChars quoteChars'
  47.  
  48. /***************************************************************************
  49.  * RxGetOpt options (case is significant):
  50.  *
  51.  * A  Allow options to appear anywhere in command line
  52.  *    (default is options must come before any other arguments)
  53.  * M  Preserve case of options (default is to force upper case)
  54.  * U  Use only '-' for options (default is to let '-' or '/' start options)
  55.  ***************************************************************************/
  56. PARSE ARG commandLine, parseOptions, longOptions, argOptions
  57.  
  58. trailingOptions = (POS('A',parseOptions) > 0)
  59.  
  60. forceUpper = (POS('M',parseOptions) = 0)
  61.  
  62. IF POS('U',parseOptions) > 0 THEN
  63.   optionChars = '-'
  64.  
  65. DROP parseOptions
  66.  
  67. /***************************************************************************
  68.  * Main Loop:
  69.  ***************************************************************************/
  70. commandLine = STRIP(commandLine,'L') /* Strip leading blanks */
  71. parsedOptions = ''
  72. DO WHILE POS(LEFT(commandLine,1),optionChars) > 0
  73.   PARSE VAR commandLine argument commandLine
  74.   CALL handleOption
  75.   commandLine = STRIP(commandLine,'L') /* Strip leading blanks */
  76. END
  77.  
  78. IF trailingOptions THEN DO
  79.   otherArguments = ''
  80.   DO WHILE commandLine <> ''
  81.     argument = nextArg()
  82.     IF POS(LEFT(argument,1),optionChars) > 0 THEN
  83.       CALL handleOption
  84.     ELSE
  85.       otherArguments = otherArguments argument
  86.   END /* while commandLine is not empty */
  87.   commandLine = STRIP(otherArguments,'L')
  88. END /* if trailingOptions */
  89.  
  90. RETURN 0 STRIP(parsedOptions optionChar||optionChar commandLine)
  91.  
  92. /***************************************************************************
  93.  * Possible return codes:
  94.  *   0 No errors
  95.  *   1 Missing argument (option that requires an argument did not find one)
  96.  ***************************************************************************/
  97.  
  98. /***************************************************************************
  99.  * Move an option onto the new command line:
  100.  *
  101.  * Input:
  102.  *   argument must hold the option to be processed
  103.  *   parsedOptions must hold any previously processed options
  104.  * Output:
  105.  *   parsedOptions holds the processed options
  106.  ***************************************************************************/
  107. handleOption: PROCEDURE EXPOSE argument parsedOptions (globalVars)
  108.   IF LEFT(argument,1) = SUBSTR(argument,2,1) THEN DO
  109.     /* Handle long option */
  110.     optionName = SUBSTR(argument,3)
  111.     IF optionName = '' THEN DO
  112.       optionChars = ''          /* '--' marks end of options */
  113.       RETURN
  114.     END /* if option is '--' */
  115.     IF POS('=',argument) > 0 THEN DO
  116.       PARSE VAR optionName optionName '=' optArgument
  117.       IF optArgument = '' THEN optArgument = '""'
  118.       ELSE DO
  119.         commandLine = optArgument' 'commandLine
  120.         optArgument = nextArg()
  121.       END /* else */
  122.     END /* if argument contains '=' */
  123.     ELSE optArgument = ''
  124.     IF forceUpper THEN optionName = TRANSLATE(optionName)
  125.     i = POS(':'optionName':',longOptions)
  126.     IF i > 1 THEN DO
  127.       optionName = SUBSTR(longOptions,i-1,1)
  128.       parsedOptions = parsedOptions optionChar||optionName
  129.     END /* if long option translates to a short option */
  130.     ELSE DO
  131.       parsedOptions = parsedOptions optionChar||optionChar||optionName
  132.     END /* else long option cannot be translated to a short option */
  133.     IF POS(':'optionName':',argOptions) > 0 THEN DO
  134.       IF optArgument = '' THEN
  135.         optArgument = nextArg()
  136.       IF optArgument = '' THEN
  137.         EXIT 1 'Option `'argument"' requires argument"
  138.       parsedOptions = parsedOptions optArgument
  139.     END /* if option takes an argument */
  140.     ELSE IF optArgument <> '' THEN
  141.       EXIT 1 'Option `'argument"' does not take an argument"
  142.   END /* if it's a long option */
  143.   ELSE /* Handle short option */
  144.     DO i = 2 TO LENGTH(argument)
  145.       optionName = SUBSTR(argument,i,1)
  146.       IF forceUpper THEN optionName = TRANSLATE(optionName)
  147.       parsedOptions = parsedOptions optionChar||optionName
  148.       IF POS(':'optionName':',argOptions) > 0 THEN DO
  149.         IF i = LENGTH(argument) THEN
  150.           optArgument = nextArg()
  151.         ELSE
  152.           optArgument = SUBSTR(argument,i+1)
  153.         IF optArgument = '' THEN
  154.           EXIT 1 'Option `'optionChar||optionName"' requires argument"
  155.         parsedOptions = parsedOptions optArgument
  156.         LEAVE /* Option that takes argument must come last */
  157.       END /* if option takes an argument */
  158.     END /* for each option char */
  159. RETURN /* handleOption */
  160.  
  161. /***************************************************************************
  162.  * Read an argument from the command line:
  163.  *
  164.  * Input:
  165.  *   The command line must be stored in commandLine
  166.  * Output:
  167.  *   Returns the argument.  If it was enclosed in quotation marks, the
  168.  *     quotes are retained.
  169.  *   The retrieved argument is removed from commandLine.
  170.  ***************************************************************************/
  171. nextArg: PROCEDURE EXPOSE (globalVars)
  172.   IF POS(LEFT(commandLine,1),quoteChars) > 0 THEN DO
  173.     /* Get a quoted argument */
  174.     quoteChar = LEFT(commandLine,1)
  175.     PARSE VAR commandLine (quoteChar) argument (quoteChar) commandLine
  176.     argument = quoteChar||argument||quoteChar
  177.   END /* if this is a quoted argument */
  178.   ELSE
  179.     PARSE VAR commandLine argument commandLine
  180.   RETURN argument
  181. /* end nextArg */
  182.