Parameters for a REXX program

[Autolink] Menu

There's a difference in the way REXX programs handle parameters when they're called from the command line (e.g. you enter erexxtry in an OS/2 window) and when they're called from another REXX program (e.g. you write myRC = rexxtry() in your REXX program):

In the first case (when called from the command line), the REXX program always retrieves the parameters given on the command line as single string. Thus, the programmer must include code in the REXX program to parse the string into individual parameters. In the second case (when called from within a REXX program), the REXX interpreter itself splits the parameter string into its parts, much as if it has been called by a REXX routine (see also How to use parse arg).

To get around the differences in these behaviors, you can check the "call type", returned by PARSE SOURCE (see also Simple parameter parsing routine). Then, having identified the type of call that was used, your program can branch to code based on the identified call type:

 
/* simple example code to handle parameters to a REXX program         */
/* in a manner equivalent to the handling of parameters for a         */
/* REXX routine                                                       */
/*                                                                    */

                    /* get the call type of this routine              */
                    /* DO NOT turn NOVALUE on at this time!           */
  parse source . callType .
  if callType <> "COMMAND" then
    signal Main     /* called as function or procedure                */
  else
  do
                    /* called from the command line                   */
    args = ""
    if pos( ",", arg(1)) = 0 then
    do
                    /* no comma found in the parameters -- use blanks */
                    /* as argument delimiter                          */

                    /* split argument in multiple arguments using     */
                    /* blanks as argument separator                   */
      do i = 1 to words( arg(1) )
        args = args "'" || word( arg(1),i ) || "'"
        args = args ","
      end /* do i = 1 to words( arg(1) ) */
    end /* if pos( ... */
    else
    do
                    /* at least one comma found in the parameters --  */
                    /* assume commas are used to separate the         */
                    /* parameters                                     */

                    /* split argument in multiple arguments using     */
                    /* commas as argument separator                   */
      argT = strip( arg(1) )
      do while argT <> ""
        parse var argT argC "," argT
        argC = strip( argC )
        args = args || "'" || argC || "'"
        args = args ","
      end /* while argT <> "" */
    end /* else */

    drop argT argC
    interpret "call Main " || args

    if symbol( "RESULT" ) = "VAR" then
    do
                    /* return the return code to the caller           */
      return result
    end /* if symbol( "RESULT" ) = "VAR" then */
    else
      return
  end /* else */

/* main function entry point                                          */
/* Note: Do not use PROCEDURE for this routine                        */

Main:
  say "Main called with " || arg() || " arguments:"
  do i = 1 to arg()
    say "  Argument " || i || " is <" || arg(i) || ">"
  end  /* do i = 1 to arg() */
return arg(1)


[Back: Using %0 ... %9 with OS/2 commands]
[Next: Parameters eaten by the CMD.EXE]