Using Rexx PARSE for Clean Command‑Line Argument Handling
Learn how Rexx’s PARSE instruction can split strings, extract typed fields, and simplify command‑line or data parsing without external libraries.
26 May 2026, 21:46 UTC

The problem: brittle argument parsing in Rexx scripts
When a Rexx script needs to read command‑line options or a delimited data line, newcomers often resort to a chain of SUBSTR, POS, and STRIP calls. The resulting code is hard to read, easy to mis‑align, and repeats the same conversion logic for each field.
Thesis: PARSE lets you extract and type‑convert fields in a single, readable statement
The built‑in PARSE instruction can split a string using whitespace, a delimiter, or a pattern, assigning the pieces to variables while applying optional type placeholders (VAR, LITERAL, NUMERIC). Because it lives in the language runtime, it works the same on IBM mainframe Rexx, Regina, and ooRexx without any external library.
How PARSE works
At its simplest, PARSE ARG template takes the special ARG source (the command‑line arguments) and matches it against a template. Each placeholder in the template consumes characters from the source and stores the result in a variable.
VAR– captures a blank‑delimited word (default).LITERAL– captures everything up to a specified literal string.NUMERIC– captures a numeric token and converts it to a number.
You can also parse a regular variable: PARSE VAR source template.
Worked example: parsing a comma‑separated line
Imagine a script that receives a line like "Alice,42,Engineer" and needs the name, age, and job title.
/* parse_example.rexx */
/* Run with: rexx parse_example.rexx "Alice,42,Engineer" */
/* No special permissions required; just read‑execute on the script file. */
/* Grab the whole argument string */
arg_line = arg(1) /* arg(1) returns the first argument passed to the script */
/* Use PARSE to split on commas and convert the age to a number */
parse var arg_line name ',' age ',' job
/* age is still a string; convert it explicitly */
age = age + 0 /* forces numeric conversion */
say 'Name:' name
say 'Age:' age
say 'Job:' job
When you run the script:
$ rexx parse_example.rexx "Alice,42,Engineer" Name: Alice Age: 42 Job: Engineer
The PARSE VAR line does three things at once:
- Reads characters up to the first comma into
name. - Reads up to the second comma into
age. - Stores the remainder in
job.
If the line had missing fields, the corresponding variables would be set to an empty string, which you can test with if name = '' then ....
Trade‑offs and limitations
While PARSE is powerful, long templates with many placeholders can become dense and hard to debug. A common mitigation is to split the parsing into two steps: first extract a chunk, then parse that chunk further.
Classic Rexx treats the source as a byte string; it does not understand Unicode code points. If you need Unicode‑aware tokenization, consider using ooRexx (which adds limited Unicode support) or pre‑convert the string to a known encoding.
Finally, PARSE does not report parsing errors; mismatched templates simply leave variables empty or truncated. Always verify the results, especially when processing external data.
Actionable closing
Next time you write a Rexx script that needs to pull apart arguments or a delimited line, reach for PARSE first. Start with a simple template, run the script with a known input, and check each variable’s value. If the output matches expectations, you have a clean, dependency‑free solution that works across all major Rexx implementations.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.