From: "bob" <bklungle@junque.com>
Subject: Re: GETARG in Ada?
Date: 1999/02/27
Date: 1999-02-27T16:58:30-06:00 [thread overview]
Message-ID: <01be62a4$bb870820$0e2915c0@w95> (raw)
In-Reply-To: 7b9r2g$lha$1@netra.msu.montana.edu
--
-- If I were doing this, I would get rid of ALL "use" clauses except for
-- "use type". But this is just my personal preference. Since you knew the
-- numerics were integers, you could have used the "'image" clause and not
-- had to use "integer_io" at all.
-- Here is a quick set of possible mods.
-- I suspect you will get many variations on this, one for everyone who
-- responds.
-- I think the important thing is you are expanding your knowledge.
--
-- cheers...bob
--
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_line("number of command-line args =" & integer'image(arg_count));
-- PUT("number of command-line args = ");
-- PUT(ARG_COUNT, 0);
-- NEW_LINE;
-- if ARG_COUNT > 0 then
for CURRENT_ARG in 1..ARG_COUNT loop
PUT("arg" & integer'image(current_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;
--
-- linux:~/atest> ada getarg.adb
-- gcc -c -g getarg.adb
-- getarg.adb:6:11: warning: file name does not match unit name, should be
"test_getarg.adb"
-- gnatbind -x getarg.ali
-- gnatlink -g getarg.ali
-- linux:~/atest> getarg abc def
-- number of command-line args = 2
-- arg 1 = abc
-- arg 2 = def
-- ***************************************************************
Lou Glassy <glassy@acheta.nervana.montana.edu> wrote in article
<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.
>
> I much appreciate Ehud Lamm's "Ada Idiom Page" and other postings
> kind souls have made to comp.lang.ada. My question is, how would
> an experienced Ada programmer write my little TEST_GETARG program
> in Ada-95? Is there an easier way to handle strings than this?
>
> Thanks in advance...
>
> -lou
>
> Lou Glassy (glassy@cs.montana.edu)
>
next prev parent 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.
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 [this message]
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