The ARG keyword is used to retrieve and parse arguments that the user typed on the command line when he started a script (ie, any text that he typed after the name of the REXX script). It can also be used to retrieve arguments passed to a function/subroutine, or arguments passed to a script that is called from another script. Each argument can be parsed into its own set of tokens.

Tokens are broken off at blank spaces, unless you specify a different search string after a token variable name.

Tokens can be thrown away by specifying a dot instead of a token variable name.

Tokens can be parsed by character position.

The following script parses the first argument passed to it into 4 tokens:

/* Parse the first argument into 4 tokens */
PARSE ARG token.1 token.2 token.3 token.4
DO i=1 TO 4
   SAY "Token" i "is:" token.i
END
Run the above script from the command line, and type "alpha beta gamma delta" after the script name. The script should print out:
Token 1 is: alpha
Token 2 is: beta
Token 3 is: gamma
Token 4 is: delta
The argument "alpha beta gamma delta" has been parsed into 4 tokens. The tokens were split up at the spaces in the input. If you experiment with running the script, you should see that if you do not type 4 words as the first argument, then the last tokens printed out are empty. Alternately, if you type more than four words, then the last token contains all the extra characters. Also, even if multiple spaces appear between the words, only the last token contains spaces.

Replace PARSE with PARSE UPPER in the program. Now, when you supply an argument, it will be tokenized as uppercase. You can simply use ARG as an abbreviation for PARSE UPPER ARG, so for example, the following 2 lines are identical:

PARSE UPPER ARG token.1
ARG token.1

Multiple arguments

If several arguments are to be passed to a script, then each argument must be separated by a comma. For example, assume that 2 args are passed to a script as so:

"alpha beta gamma delta", 1
The first arg has 4 words in it. The second arg has one number. You can parse each argument into its own set of tokens, and you separate the two sets of tokens with a comma as well:
/* Parse the first argument into 4 tokens,
 * named token.1, token.2, token.3 and token.4,
 * and toss away any additional characters of it.
 * Parse the second argument into a variable
 * named number, and toss away any additional
 * characters. Ignore all other arguments.
 */
PARSE ARG token.1 token.2 token.3 token.4 . , number .
Notice that we have two sets of tokens, each separated by a comma.


ARG() built-in
The ARG (or PARSE ARG) keyword is useful if you already know how many arguments you expect to be passed, and know how you wish them parsed.

But, if you're not sure how many args you're going to be passed, then the ARG() built-in function can be useful. This can tell you how many args you have been passed as so:

/* Display how many args were passed */
SAY ARG()
You can also fetch one, particular arg by specifying the arg number you wish (where 1 is the first arg):
/* Display all of the args that were passed */
count = ARG()
DO i = 1 TO count
   SAY ARG(i)
END