Sample to parse command-line options
Command line option syntax: "ab:c:"
- a: a switch option
- b/c: has a option value
For example:
PROG -a
PROG -b b_value
PROG -a -b b_value -c v_value.
Program Codes:
IDENTIFICATION DIVISION.
PROGRAM-ID. COMMANDLINE.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 ARG-OPTION PIC X(20) VALUE SPACES.
88 ARG-A VALUES "-a".
88 ARG-B VALUES "-b", "--bbbb".
88 ARG-C VALUES "-c", "--cccc".
01 ARG-VALUE.
05 VALUE-A PIC X(1) VALUE "N".
05 VALUE-B PIC X(10) VALUE SPACES.
05 VALUE-C PIC X(10) VALUE SPACES.
PROCEDURE DIVISION.
* * PARSE ARGS ONE-BY-ONE
ACCEPT ARG-OPTION FROM ARGUMENT-VALUE
PERFORM UNTIL ARG-OPTION = SPACES
EVALUATE TRUE
WHEN ARG-A
MOVE "Y" TO VALUE-A
WHEN ARG-B
ACCEPT VALUE-B FROM ARGUMENT-VALUE
WHEN ARG-C
ACCEPT VALUE-C FROM ARGUMENT-VALUE
WHEN OTHER
DISPLAY "INVALID SWITCH: " ARG-OPTION
END-EVALUATE
MOVE SPACES TO ARG-OPTION
ACCEPT ARG-OPTION FROM ARGUMENT-VALUE
END-PERFORM
* * PRINT ARGS ONE-BY-ONE
display "ARG:a=[" VALUE-A "]".
display "ARG:b=[" VALUE-B "]".
display "ARG:c=[" VALUE-C "]".
* * PARSE ARGS LINE
ACCEPT ARG-OPTION FROM COMMAND-LINE
DISPLAY "ARGS:=[" ARG-OPTION "]".
GOBACK.
Executing Result:
$ COMMANDLINE -a -b bb -c cc
ARG:a=[Y]
ARG:b=[bb ]
ARG:c=[cc ]
ARGS:=[-a -b bb -c cc ]