comp.lang.ada
 help / color / mirror / Atom feed
From: "David C. Hoos, Sr." <david.c.hoos.sr@ada95.com>
Subject: Re: GETARG in Ada?
Date: 1999/02/27
Date: 1999-02-27T00:00:00+00:00	[thread overview]
Message-ID: <0PjuprqY#GA.177@pet.hiwaay.net> (raw)
In-Reply-To: 7b9r2g$lha$1@netra.msu.montana.edu


Lou Glassy wrote in message <7b9r2g$lha$1@netra.msu.montana.edu>...
>Question:  Can anyone help me clean up this program?
>           No, this is not a homework assignment.
>           I'm an old Fortran programmer, learning Ada...
>
>Here is a little Fortran program (compiles, runs with g77 under Linux)
>that prints out the arguments on the Unix command line:
>
>        PROGRAM TEST_GETARG
>        IMPLICIT NONE
>
>        INTEGER ARG_COUNT, I
>        CHARACTER(LEN=20) CURRENT_ARG
>
>        ARG_COUNT = IARGC()
>        PRINT *, 'number of args = ', ARG_COUNT
>        DO I = 1, ARG_COUNT
>                CALL GETARG(I, CURRENT_ARG)
>                PRINT *, 'ARG ', I, '=', CURRENT_ARG
>        END DO
>
>        END
>
>I'd like to do the same thing in Ada-95.  For concreteness, I'm
>using GNAT (3.10p), but I'm guessing any A95 implementation on a
>Unix box would eat something similar to the following:
>
>with TEXT_IO;           use TEXT_IO;
>with ADA.COMMAND_LINE;  use ADA.COMMAND_LINE;
>with ADA.STRINGS;       use ADA.STRINGS;
>with ADA.STRINGS.FIXED; use ADA.STRINGS.FIXED;
>
>procedure TEST_GETARG is
>
>    package INT_IO is new INTEGER_IO(INTEGER);
>    use INT_IO;
>
>    ARG_COUNT: INTEGER;
>
>    MAX_ARG_LENGTH: constant INTEGER := 80;
>
>    ARG: STRING(1..MAX_ARG_LENGTH) := (others => ' ');
>
>begin
>
>    ARG_COUNT := ADA.COMMAND_LINE.ARGUMENT_COUNT;
>    PUT("number of command-line args = ");
>    PUT(ARG_COUNT);
>    NEW_LINE;
>
>    if ARG_COUNT > 0 then
>
>        for CURRENT_ARG in 1..ARG_COUNT loop
>            PUT("arg ");
>            PUT(CURRENT_ARG);
>            PUT("=");
>            ARG(1..ADA.COMMAND_lINE.ARGUMENT(CURRENT_ARG)'LENGTH) :=
>                ADA.COMMAND_lINE.ARGUMENT(CURRENT_ARG);
>            PUT(TRIM(ARG,RIGHT));
>            NEW_LINE;
>        end loop;
>
>    end if;
>
>end TEST_GETARG;
>
>------------------------------------------
>
>My A95 "TEST_GETARG" program works, I'm just trying to figure
>out how to write it more cleanly.
>
This is the code from one of the examples supplied with GNAT.
It uses gnat-specific IO, but you could easily replace the Gnat.IO calls
with calls to Text_IO.

You've made much more work for yourself than necessary; there's no need to
"trim" or anything like that, because the put procedure will put only as
many characters as there are in the string.

By the way, 3.10p is going on two years old.  Why don't you get 3.11p?

Hope this helps.

with Ada.Command_Line;
with Gnat.Io; use Gnat.Io;

procedure Test_CL is
begin
   --  Writes out the command name (argv[0])
   Put ("      The command name : ");
   Put (Ada.Command_Line.Command_Name);
   New_Line;

   --  Writes out the number of arguments passed to the program (argc)
   Put ("The number of arguments: ");
   Put (Ada.Command_Line.Argument_Count);
   New_Line;

   --  Writes out all the arguments using the Argument function.
   --  (BE CAREFUL because if the number you pass to Argument is not
   --   in the range 1 .. Argument_Count you will get Constraint_Error!)
   for I in 1 .. Ada.Command_Line.Argument_Count loop
      Put ("             Argument ");
      Put (I);
      Put (": ");
      Put (Ada.Command_Line.Argument (I));
      New_Line;
   end loop;
end Test_CL;







  reply	other threads:[~1999-02-27  0:00 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1999-02-27  0:00 GETARG in Ada? Lou Glassy
1999-02-27  0:00 ` David C. Hoos, Sr. [this message]
1999-02-27  0:00   ` bill
1999-02-28  0:00     ` James S. Rogers
1999-03-02  0:00     ` Stephen Leake
1999-03-02  0:00       ` nabbasi
1999-02-27  0:00 ` bob
1999-02-28  0:00 ` Matthew Heaney
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox