Simple parameter parsing routine

[Autolink] Menu

 
/*                                                                    */
/* sample REXX routine to split a parameter string into               */
/* keyword / keyvalue pairs.                                          */
/* This routine also handles parameter in quotes or double quotes     */
/* (see also GetOpt - UNIX like parameter handling                    */
/* and Parameter for a REXX program                                   */
/*                                                                    */

                    /* install an error handler for CTRL-C            */
  signal on halt name UserAbort

  do forever
    say 'Testing the parameter handling routine'
                    /* get the user input                             */
    say 'Enter the separator char (CTRL-C to abort): '
    separator = strip( lineIn() )
    say 'Enter the parameter string for testing (CTRL-C to abort):'
    thisParms = lineIN()

                    /* call the routine                               */
    if separator <> '' then
    do
      say 'Now Calling "SplitParameter 'thisParms','separator''
      call SplitParameter thisParms, Separator
    end /* if */
    else
    do
      say 'Now Calling "call SplitParameter 'thisParms
      call SplitParameter thisParms
    end /* else */

                    /* show the results                               */
    say 'Result:'
    say 'Argv.0 is 'argv.0
    do i = 1 to argv.0
      say 'Argv.i.__Keyword is "'  || argv.i.__keyWord  || '"'
      say 'Argv.i.__KeyValue is "' || argv.i.__keyValue || '"'
      say  Argv.i.__original is "' || argv.n.__original || '"'
    end /* do i = 1 to argv.0 */

  end /* do forever */
exit 0

UserAbort:
  say 'Program aborted by the user!'
exit 1

/* ------------------------------------------------------------------ */
/* function: split a string into separate arguments                   */
/*                                                                    */
/* call:     call SplitParameter Parameter_string {, separator }      */
/*                                                                    */
/* where:    parameter_string - string to split                       */
/*           separator - separator character to split a parameter     */
/*                       into keyword and keyvalue                    */
/*                       (Def.: Don't split the parameter into        */
/*                              keyword and keyvalue)                 */
/*                                                                    */
/* returns:  the number of arguments                                  */
/*           The arguments are returned in the stem argv.:            */
/*                                                                    */
/*             argv.0 = number of arguments                           */
/*                                                                    */
/*             argv.n.__keyword = keyword                             */
/*             argv.n.__keyValue = keyValue                           */
/*             argv.n.__original = original_parameter                 */
/*                                                                    */
/*           The variables 'argv.n.__keyvalue' are only used if       */
/*           the parameter 'separator' is not omitted.                */
/*                                                                    */
/* note:     This routine handles arguments in quotes and double      */
/*           quotes also. You can use either the format               */
/*                                                                    */
/*             keyword:'k e y v a l u e'                              */
/*                                                                    */
/*           or                                                       */
/*                                                                    */
/*             'keyword:k e y v a l u e'                              */
/*                                                                    */
/*           (':' is the separator in this example).                  */
/*                                                                    */
SplitParameter: PROCEDURE EXPOSE (exposeList) argv.

                    /* get the parameter                              */
  parse arg thisArgs, thisSeparator

                    /* init the result stem                           */
  argv. = ''
  argv.0 = 0

  do while thisargs <> ''

    parse value strip( thisArgs, "B" ) with curArg thisArgs

    parse var curArg tc1 +1 .
    if tc1 = '"' | tc1 = "'" then
      parse value curArg thisArgs with (tc1) curArg (tc1) ThisArgs

    if thisSeparator <> '' then
    do
                    /* split the parameter into keyword and keyvalue  */
      parse var curArg argType (thisSeparator) argValue

      parse var argValue tc2 +1 .
      if tc2 = '"' | tc2 = "'" then
        parse value argValue thisArgs with (tc2) argValue (tc2) ThisArgs

      if tc1 <> '"' & tc1 <> "'" & tc2 <> '"' & tc2 <> "'" then
      do
        argtype  = strip( argType  )
        argValue = strip( argValue )
      end /* if */
      else                                                   /* v3.20 */
         if argValue <> '' then                              /* v3.20 */
           curArg = argtype || thisSeparator || argValue     /* v3.20 */

      i = argv.0 + 1
      argv.i.__keyword = translate( argType )
      argv.i.__KeyValue = argValue
      argv.i.__original = strip( curArg )                    /* v3.20 */
      argv.0 = i

   end /* if thisSeparator <> '' then */
   else
   do
     i = argv.0 + 1
     argv.i.__keyword = strip( curArg )
     argv.i.__original = strip( curArg )                     /* v3.20 */
     argv.0 = i
   end /* else */

  end /* do while thisArgs <> '' */

RETURN argv.0


[Back: Soundex routine(s)]
[Next: Using the CLOCK$ device]