MainframeMaster

COBOL COMMAND-LINE

The COMMAND-LINE feature in COBOL enables programs to process command line arguments and parameters, making applications more flexible and configurable. This capability is essential for creating utility programs, batch processors, and interactive applications that need runtime configuration.

Overview and Purpose

Command line processing in COBOL allows programs to receive and process arguments passed when the program is invoked. This feature makes programs more versatile by enabling users to specify options, input files, output destinations, and other configuration parameters without modifying the source code. Whether you're creating data processing utilities, report generators, or administrative tools, command line processing provides the flexibility needed for professional applications.

Basic Command Line Retrieval

cobol
1
2
3
4
01 COMMAND-LINE-DATA PIC X(256). ACCEPT COMMAND-LINE-DATA FROM COMMAND-LINE DISPLAY "Command line: " COMMAND-LINE-DATA

This basic example shows how to retrieve the entire command line using the ACCEPT statement. The COMMAND-LINE special register contains all arguments passed to the program as a single string. The field should be large enough to accommodate the expected command line length. This approach gives you complete control over parsing but requires you to handle the string manipulation yourself.

Simple Parameter Parsing

cobol
1
2
3
4
5
6
7
01 WS-COMMAND-LINE PIC X(200). 01 WS-FILENAME PIC X(50). 01 WS-MODE PIC X(10). ACCEPT WS-COMMAND-LINE FROM COMMAND-LINE UNSTRING WS-COMMAND-LINE DELIMITED BY SPACE INTO WS-FILENAME, WS-MODE

This example demonstrates basic parameter parsing using the UNSTRING statement. The command line is split into individual components based on space delimiters. This simple approach works well when you have a fixed number of positional parameters. The first parameter becomes the filename, and the second becomes the mode setting. This pattern is common for simple utility programs.

Advanced Parameter Processing

cobol
1
2
3
4
5
6
7
8
01 WS-PARAMETERS. 05 WS-PARAM-COUNT PIC 99 VALUE ZERO. 05 WS-PARAM-TABLE OCCURS 20 TIMES. 10 WS-PARAM-NAME PIC X(20). 10 WS-PARAM-VALUE PIC X(50). 01 WS-CURRENT-PARAM PIC X(80). 01 WS-PARAM-INDEX PIC 99.

For more complex parameter processing, define a parameter table structure that can hold multiple name-value pairs. This approach supports named parameters (like --input=file.txt or -o output.dat) and provides a flexible foundation for sophisticated command line interfaces. The table structure allows you to store and access parameters by name rather than position.

Tutorial: Building a Configurable File Processor

Step-by-Step Tutorial

Step 1: Define Parameter Structure

cobol
1
2
3
4
5
01 WS-CONFIG. 05 WS-INPUT-FILE PIC X(100) VALUE SPACES. 05 WS-OUTPUT-FILE PIC X(100) VALUE SPACES. 05 WS-REPORT-TYPE PIC X(20) VALUE "SUMMARY". 05 WS-VERBOSE-MODE PIC X VALUE "N".

Start by defining a configuration structure with default values for all parameters. This ensures your program works even with minimal command line input.

Step 2: Parse Command Line Arguments

cobol
1
2
3
4
5
6
PERFORM PARSE-COMMAND-LINE IF WS-INPUT-FILE = SPACES DISPLAY "Error: Input file required" DISPLAY "Usage: PROGRAM inputfile [options]" STOP RUN END-IF

Implement parsing logic that extracts parameters and validates required arguments. Provide helpful error messages and usage information when parameters are missing or invalid.

Step 3: Implement Parameter Validation

cobol
1
2
3
4
5
6
7
PARSE-COMMAND-LINE. ACCEPT WS-COMMAND-LINE FROM COMMAND-LINE PERFORM VARYING WS-INDEX FROM 1 BY 1 UNTIL WS-INDEX > 10 OR WS-COMMAND-LINE = SPACES PERFORM EXTRACT-NEXT-PARAMETER PERFORM PROCESS-PARAMETER END-PERFORM.

Create a systematic approach to parameter processing that handles both positional and named parameters, validates input, and sets appropriate configuration values.

Practical Exercises

Practice Exercises

Exercise 1: File Copy Utility

Create a program that accepts source and destination filenames from the command line, with optional parameters for backup and verbose modes.

Show Solution
cobol
1
2
3
4
5
6
7
8
9
01 WS-COPY-CONFIG. 05 WS-SOURCE-FILE PIC X(80). 05 WS-DEST-FILE PIC X(80). 05 WS-BACKUP-FLAG PIC X VALUE "N". 05 WS-VERBOSE-FLAG PIC X VALUE "N". ACCEPT WS-COMMAND-LINE FROM COMMAND-LINE UNSTRING WS-COMMAND-LINE DELIMITED BY SPACE INTO WS-SOURCE-FILE, WS-DEST-FILE, WS-OPTIONS

Exercise 2: Report Generator

Design a report generator that accepts input file, report type, date range, and output format as command line parameters.

Show Solution
cobol
1
2
3
4
5
6
01 WS-REPORT-CONFIG. 05 WS-DATA-FILE PIC X(80). 05 WS-REPORT-TYPE PIC X(20) VALUE "STANDARD". 05 WS-START-DATE PIC X(10). 05 WS-END-DATE PIC X(10). 05 WS-OUTPUT-FORMAT PIC X(10) VALUE "TEXT".

Exercise 3: Data Validation Tool

Build a data validation tool that processes command line options for validation rules, error reporting level, and output destinations.

Show Solution
cobol
1
2
3
4
5
01 WS-VALIDATION-CONFIG. 05 WS-INPUT-FILE PIC X(80). 05 WS-RULES-FILE PIC X(80). 05 WS-ERROR-LEVEL PIC 9 VALUE 1. 05 WS-ERROR-FILE PIC X(80) VALUE "ERRORS.TXT".

Advanced Command Line Techniques

Named Parameter Processing

cobol
1
2
3
4
5
6
7
8
PROCESS-NAMED-PARAMETER. IF WS-CURRENT-PARAM(1:2) = "--" PERFORM PROCESS-LONG-OPTION ELSE IF WS-CURRENT-PARAM(1:1) = "-" PERFORM PROCESS-SHORT-OPTION ELSE PERFORM PROCESS-POSITIONAL-PARAM END-IF.

Implement support for both short (-v) and long (--verbose) parameter formats. This provides users with flexible options and follows standard command line conventions. The processing logic can distinguish between different parameter types and handle them appropriately.

Parameter Validation and Error Handling

cobol
1
2
3
4
5
6
7
8
9
10
11
12
VALIDATE-PARAMETERS. IF WS-INPUT-FILE = SPACES MOVE "Input file is required" TO WS-ERROR-MSG PERFORM DISPLAY-ERROR-AND-EXIT END-IF IF WS-REPORT-TYPE NOT = "SUMMARY" AND WS-REPORT-TYPE NOT = "DETAIL" AND WS-REPORT-TYPE NOT = "FULL" MOVE "Invalid report type" TO WS-ERROR-MSG PERFORM DISPLAY-ERROR-AND-EXIT END-IF.

Implement comprehensive parameter validation that checks for required parameters, validates parameter values against acceptable ranges or lists, and provides meaningful error messages. Good validation prevents runtime errors and improves user experience.

Help and Usage Information

cobol
1
2
3
4
5
6
7
DISPLAY-USAGE. DISPLAY "Usage: PROGRAM inputfile [options]" DISPLAY "Options:" DISPLAY " -o, --output FILE Output file name" DISPLAY " -t, --type TYPE Report type (SUMMARY|DETAIL|FULL)" DISPLAY " -v, --verbose Verbose output mode" DISPLAY " -h, --help Show this help message".

Always provide comprehensive help and usage information. Users should be able to understand how to use your program without consulting external documentation. Include examples of common usage patterns and explain all available options clearly.

Environment Integration

Configuration File Support

For complex applications, consider supporting configuration files in addition to command line parameters. This allows users to maintain standard configurations while still overriding specific settings through command line arguments. Configuration files are particularly useful for applications with many parameters or when the same settings are used repeatedly.

Environment Variable Integration

Integrate command line processing with environment variables to provide multiple ways for users to configure your application. Environment variables can provide default values that command line arguments can override, creating a flexible hierarchy of configuration options.

Test Your Knowledge

Question 1: Command Line Access

Which statement is used to retrieve command line arguments in COBOL?

A) READ COMMAND-LINE
B) ACCEPT FROM COMMAND-LINE
C) GET COMMAND-LINE
D) INPUT COMMAND-LINE
Show Answer

B) ACCEPT FROM COMMAND-LINE - This is the standard COBOL statement for retrieving command line arguments into a data item.

Question 2: Parameter Parsing

What's the most flexible approach for processing command line parameters?

A) Fixed positional parameters only
B) Named parameters with validation
C) Single parameter string
D) Hard-coded parameter values
Show Answer

B) Named parameters with validation - This approach provides the most flexibility and user-friendliness while ensuring data integrity.

Question 3: Error Handling

What should a program do when required command line parameters are missing?

A) Continue with default values
B) Display error message and usage information
C) Ignore the missing parameters
D) Crash with runtime error
Show Answer

B) Display error message and usage information - This provides the best user experience by clearly indicating what's wrong and how to fix it.

Frequently Asked Questions