Command_Line provides a few subprograms for input from the operating system. This package defines a function named Argument_Count that returns the number of arguments given to it (it will be zero or higher). It also provides a function called Argument that takes an argument number (an index) and returns that argument's value as a String. It also provides function Command_Name, which returns the name of the program as a String; some operating systems don't support this.
Command_Line provides subprograms for returning a Status_Code to the operating system, which is some sort of integer type. There are two predefined Status_Codes: Success and Failure. You can set the Status_Code using the procedure Set_Status; when the program ends, the last value set in Set_Status will be returned to the operating system.
Here's the definition of Command_Line (from the LRM section A.15):
package Ada.Command_Line is function Argument_Count return Natural; function Argument (Number : in Positive) return String; function Command_Name return String; type Exit_Status is implementation-defined integer type; Success : constant Exit_Status; Failure : constant Exit_Status; procedure Set_Exit_Status (Code : in Exit_Status); -- ... end Ada.Command_Line;
Here's an example of Command_Line; program Show takes as arguments a list of files and prints them out, indented with one space:
with Ada.Command_Line, Text_IO, Ada.Strings.Unbounded, Ustrings; use Ada.Command_Line, Text_IO, Ada.Strings.Unbounded, Ustrings; procedure Show is -- Take each command line argument as a filename and display -- each file, indented. procedure Show_File(Filename : String) is -- Open "Filename" and display it, indented. File : File_Type; Input : Unbounded_String; begin Put("Printing file "); Put_Line(Filename); Open(File, In_File, Filename); while (not End_Of_File(File)) loop Get_Line(File, Input); Put(' '); -- indent. Put_Line(Input); end loop; Close(File); end Show_File; begin -- procedure Show if Argument_Count = 0 then Put_Line(Current_Error, "Error - No file names given."); Set_Exit_Status(Failure); else -- Open each file and show it. for Arg in 1 .. Argument_Count loop Show_File(Argument(Arg)); end loop; end if; end Show;
Package Command_Line may not be useful if there's no operating system, since in that case there's nothing to communicate with.
There is no quiz question for this section.
You may go to the next section.
![]() |
![]() |
---|
David A. Wheeler (dwheeler@ida.org)