Command Line Arguments in COBOL
Command-line arguments are a common way to parameterize execution of programs. For example, CALL "PROGRAM" USING ARG1 ARG2
uses ARG1
and ARG2
as arguments to the PROGRAM
.
In COBOL, we don’t have direct access to command-line arguments like in some other languages. Instead, we use the ACCEPT FROM ARGUMENT-NUMBER
and ACCEPT FROM ARGUMENT-VALUE
statements to retrieve the number of arguments and their values.
The DISPLAY-ARGS
paragraph shows how to access all arguments:
- We use
ACCEPT WS-ARGS-COUNT FROM ARGUMENT-NUMBER
to get the total number of arguments. - We then loop through all arguments using a
PERFORM VARYING
loop. - For each iteration, we use
ACCEPT WS-ARG FROM ARGUMENT-VALUE
to get the value of the current argument.
The DISPLAY-SPECIFIC-ARG
paragraph demonstrates how to access a specific argument (in this case, the third one):
- We set
WS-ARG-INDEX
to 3 to get the third argument. - We then use
ACCEPT WS-ARG FROM ARGUMENT-VALUE
to retrieve its value.
To experiment with command-line arguments, you would compile this program and then run it with arguments. The exact command might vary depending on your COBOL compiler and environment, but it could look something like this:
Note that in COBOL, unlike some other languages, the program name itself is typically not included in the argument list.
Next, we’ll look at more advanced command-line processing techniques in COBOL.