Previous | Next | Table of Contents | Index | Program List | Copyright

9.6 Problem Solving: Reading Command Parameters

It is common that a program must be "configured" with certain values before it begins to do its work. A popular method for passing such parameters into the program is to use command line parameters (sometimes called command line flags or command line options). Ada provides this capability in the package Ada.Command_Line, which allows a program to retrieve the flags or parameters entered on the operating system command line when the program is invoked.[1] The specification for this package is shown in Figure 9.3.

Figure 9.3
Ada 95 Command Line Package

PACKAGE Ada.Command_Line IS

  FUNCTION Argument_Count RETURN Natural;

  FUNCTION Argument (Number : IN Positive) RETURN String;

  FUNCTION Command_Name RETURN String;

  TYPE Status IS RANGE implementation-defined;

  Success : CONSTANT Status;
  Failure : CONSTANT Status;

  PROCEDURE Set_Status (Code : IN Status);

END Ada.Command_Line;

Program 9.5 illustrates the use of the first two functions.

Program 9.5
Illustration of Ada.Command_Line

WITH Ada.Text_IO;
WITH Ada.Command_Line;
PROCEDURE Command_Arguments IS
------------------------------------------------------------------------
--| demonstrate Ada 95 command line parameters
--| Author: Michael B. Feldman, The George Washington University 
--| Last Modified: September 1995                                     
------------------------------------------------------------------------
 
  HowMany: Natural;  -- how many command-line arguments were there?

BEGIN -- Command_Arguments

  Ada.Text_IO.Put(Item => Ada.Command_Line.Command_Name);

  HowMany := Ada.Command_Line.Argument_Count;

  IF HowMany = 0 THEN
    Ada.Text_IO.Put_Line(Item => ": No command line arguments today.");
  ELSE  
    Ada.Text_IO.Put_Line(Item => ": The command line arguments are: ");
  
    FOR Count IN 1..HowMany LOOP
      Ada.Text_IO.Put
        (Item => Ada.Command_Line.Argument(Number => Count));
      Ada.Text_IO.New_Line;
    END LOOP;
  END IF;
  
END Command_Arguments;
Sample Run
command_arguments: The command line arguments are: 
ABC
123
pqr

Note that Argument always returns a string; if the program requires an integer, float, or enumeration value, the appropriate string-input Get procedure (see Section 9.5) can be used to convert the string.

The sample run illustrates the result of typing, on the Unix command line (for example),

    command_arguments ABC 123 pqr
Some operating systems allow a program, invoked by the command line, to return a value, usually a small nonnegative integer, to the command shell. The procedure Set_Status can be used to set this value, if the operating system allows it. Furthermore, the function Command_Name allows the program to find out its own name as known to the operating system, that is, the name by which it is invoked on the command line.


Case Study: Lengths of Lines in a Text File

In preparing this book for production at the publisher, the authors were faced with meeting a page-layout requirement that no line of program source code exceed 72 characters in length. This requirement led to the present case study; the author actually used the resulting program to assist in meeting the layout requirement.

PROBLEM SPECIFICATION

Develop a program that, given the name of a text file and a specified limit on line length, reads each line of the file and displays it, together with its line number, if and only if it exceeds the given limit. At the end, display the maximum line length found in the file. The program is to have two input arguments, the maximum desired length and the name of the file being checked.

ANALYSIS

The program can use command line parameters to get its input arguments. The full data requirements are:

Data Requirements

Program Inputs

desired maximum length (MaxLength: Natural)

text file to be checked (InputText: Ada.Text_IO.File_Type)

Program Outputs

line number of each long line (LineNumber: Natural)

each excessively long line (Line: String(1..120))

DESIGN

Algorithm

1. Get command arguments and open the input file.

2. Read each line of the file and display it with its line number, if and only if its length exceeds the desired maximum.

3. Display the length of the longest line found.

We leave the straightforward refinements and test plan to the student as an exercise.

IMPLEMENTATION

Program 9.6 implements the algorithm. The first command argument is an integer, so it is read from that argument (as string) using the string-input Ada.Integer_Text_IO.Get procedure. The second argument is a file name and so can be passed directly from the command argument to the Ada.Text_IO.Open operation.

Program 9.6
Find the Lengths of the Lines in a Text File

WITH Ada.Text_IO;
WITH Ada.Integer_Text_IO;
WITH Ada.Command_Line;
PROCEDURE Line_Length IS
------------------------------------------------------------------------
--| Compute maximum line length in a file
--| First command line parameter gives the desired max number
--| of characters; second parameter gives input file.
--| Program displays lines longer than the desired max,
--| and finds the length of the longest line in the file.
--| Author: Michael B. Feldman, The George Washington University 
--| Last Modified: September 1995 
------------------------------------------------------------------------
 
  DesiredMax: Natural;        -- input  - desired maximum length
  MaxLength : Natural;        -- output - maximum line length

  LineNumber: Natural;        -- counts the lines in the file
  Line      : String(1..120); -- holder for input line read from file
  Length    : Natural;        -- holds length of current line

  InputText : Ada.Text_IO.File_Type;

BEGIN -- Line_Length;

  -- Get command parameters and open input file
  Ada.Integer_Text_IO.Get (From => Ada.Command_Line.Argument(1),
                           Item => DesiredMax,
                           Last => Length);

  Ada.Text_IO.Put 
    (Item => "Reading from " & Ada.Command_Line.Argument(2));    
  Ada.Text_IO.New_Line;

  Ada.Text_IO.Open (File => InputText, Mode => Ada.Text_IO.In_File,
                    Name => Ada.Command_Line.Argument(2));

  -- loop through file reading lines
  MaxLength := 0;
  LineNumber := 0;
  WHILE NOT Ada.Text_IO.End_of_File (File => InputText) LOOP

    Ada.Text_IO.Get_Line
      (File => InputText, Item => Line, Last => Length);

    -- keep track of number of lines in the file
    LineNumber := LineNumber + 1;

    -- is line too long?
    IF Length > DesiredMax THEN
      Ada.Integer_Text_IO.Put(Item => LineNumber, Width => 3);
      Ada.Text_IO.Put(Item => " " & Line(1..Length));
      Ada.Text_IO.New_Line;
    END IF;

    -- is line longer than longest so far?
    IF Length > MaxLength THEN
      MaxLength := Length;
    END IF;

  END LOOP;

  -- display results
  Ada.Text_IO.Put(Item => "The longest line length in " &
                          Ada.Command_Line.Argument(2) & " is ");
  Ada.Integer_Text_IO.Put(Item => MaxLength, Width => 1);
  Ada.Text_IO.New_Line;

END Line_Length;

TESTING

The sample run shows the results of running the program on its own source file, that is, of typing, on the command line,

    line_length 65 line_length.adb
Sample Run
Reading from line_length.adb
 5 ---------------------------------------------------------------------
 17 --------------------------------------------------------------------
 23   Line     : String(1..120); -- holder for input line read from file
 39   Ada.Text_IO.Open (File => InputText, Mode => Ada.Text_IO.In_File,
The longest line length in line_length.adb is 69

[1] This is another new Ada 95 feature. It is missing from Ada 83 and is often provided by Ada 83 compiler suppliers in a nonstandard way.


Previous | Next | Table of Contents | Index | Program List | Copyright

Copyright © 1996 by Addison-Wesley Publishing Company, Inc.