-- A simple program that uses command line arguments 
-- and input from a named file.

-- If the program is invoked with the command:
--    cline f1 f2 
-- Then the following files will be read and printed: My_Data, f1, f2

-- We assume that each file contains 0 or more integers.

with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;

with Ada.Command_Line; use Ada.Command_Line;

procedure cline is

    -- The name Input_File will be used to access the file from within the
    -- program

    Input_File: File_Type;

    number : Integer;

    -- Example of opening, doing input from, and closing the file My_Data

    -- Open connects the name program's name (Input_File) for the file
    -- with the operating system's name (My_Data).

    -- In_File is the mode.  It means that we do input from the file.

    -- Open will raise an ADA.IO_EXCEPTIONS.NAME_ERROR expection
    -- if the file does not exist.

    open (Input_File, In_File, "My_Data");

        exit when End_Of_File (Input_File);

        get (Input_File, number);

        put (number);
    end loop;

    close (Input_File);

    -- Now do the same thing with each file named as a command line argument
    -- Argument_Count is the number of command line arguments

    for I in 1 .. argument_count loop

        -- Output each command line argument
        put (argument(i));
        put_line (":");

        -- Open each file named by the command line argument

        open (Input_File, In_File, argument(i));

            exit when End_Of_File (Input_File);

            get (Input_File, number);

            put (number);
        end loop;

        close (File => Input_File);

    end loop;

    -- Command_Name is the name of the command that is executing.
    -- It will be cline, unless the executable has been renamed.

    put ("This program was invoked by using the command '");
    put (Command_Name);
    put_line ("'.");

end cline;