One of the first challenges that you will face when writing REXX programs is how to obtain and process initial program arguments. This differs considerably from other languages; such as, C/C++/C# and Java.
When a program is performed as a command, it usually receives a single string of text as an argument. For example,
ex SHOWARGS 'here is some text' |
In the above command:
| ex | is the name of the REXX executor. This varies for different REXX implementation. |
| SHOWARGS | is the name of the REXX program to perform. |
| 'here is some text' | is the program argument text. |
The following SHOWARGS program displays initial arguments.
/* REXX */
/* SHOWARGS program -- shows initial arguments */
say "This program's argument text is:" arg(1)
say 'There are' words( arg(1) ) 'words in the argument text.'
parse arg args
do wd_ix=1 while args <> ''
parse var args wd args
say wd_ix'.' wd
end wd_ix
|
When invoked with the command shown above, the following output appears:
This program's argument text is: 'here is some text' There are 4 words in the argument text. 1. 'here 2. is 3. some 4. text' |
In the above, you can see that 'here is some text' is passed to the program as a single text string. You may have been surprised to see the apostrophes in the argument text.
Here is a modified version of the program that removes the apostrophes.
/* REXX */
/* SHOWARGS program -- shows initial arguments */
parse arg args
args = strip( args, 'B', "'" ) /* remove apostrophes */
say "This program's argument text is:" args
say 'There are' words( args ) 'words in the argument text.'
do wd_ix=1 while args <> ''
parse var args wd args
say wd_ix'.' wd
end wd_ix
|
The following output appears:
This program's argument text is: here is some text There are 4 words in the argument text. 1. here 2. is 3. some 4. text |
You might be mislead by the REXX ARG function's processing of argument text. For example, you might think that an ARG function request would return the number of arguments. In the SHOWARGS program,
say 'There are' ARG() 'arguments.' |
Would show:
There are 1 arguments. |
This is because only one argument is passed to a program that is performed as a command. Multiple arguments are passed to programs when they are invoked as external routines.
Here is another example that converts numeric program arguments to their hexadecimal equivalents.
/* REXX */
/* CVTTOHEX program -- shows hexadecimal equivalents of numeric command arguments */
parse arg args
do n=1 while args <> ''
parse var args wd args
say wd "--> x'" || d2x( wd ) || "'"
end n
|
When the CVTTOHEX program is invoked with the following command:
ex CVTTOHEX 237 413 919 |
The following output appears:
237 --> x'ED' 413 --> x'19D' 919 --> x'397' |
There are many ways that you can process initial program arguments. The associated topics are:
REXX parsing is intricate. If you are beginning to write REXX programs, then you should use the simple argument parsing techniques that are shown above.