markww wrote: > Hi, > > Can someone give me an example of a command line app where I can read > in command line arguments? > > In C++ we have: > > int main(int argc, char **argv) > { > printf("There are [%i] arguments.\n", argc); > return 0; > } > > all I have so far in ada is: > > > with Ada.Text_IO; use Ada.Text_IO; > procedure DynamicArray is > begin > > -- Tell the user what we're doing. > Put_Line ("This application will allocate a dynamic array."); > > -- Now allocate a dynamic array. > Name : array (0 .. 20) of INTEGER > > -- That's it. > > end DynamicArray; > > eventually I'd like to allocate that array based on the number of > elements passed as an argument by the user. > > Thanks, > Mark > -- start code with Ada.Text_IO; with Ada.Command_Line; procedure Main is Parm_Max : constant := 1024; subtype Arg_String_Chars is String(1 .. Parm_Max); type Arg_String is record Len : Integer; Chars : Arg_String_Chars; end record; Argc : Integer := Ada.Command_Line.Argument_Count; Argv : array(1 .. Argc) of Arg_String; begin for I in 1 .. Argc loop Argv(I).Len := Ada.Command_Line.Argument(I)'Length; Argv(I).chars(1..Argv(I).Len) := Ada.Command_Line.Argument(I); end loop; for I in 1 .. Argc loop Ada.Text_IO.Put_Line(Argv(I).chars(1 .. Argv(I).Len)); end loop; end Main; -- end code Some comments: 1. I created an array to hold the command string so I could include the length. The 'length attribute gives the length of the array with no consideration of the contents of the array. So without some external reference to the length you will print out 1024 characters for each parameter, no matter how long the parameter really is. 2. There does not seem to be an argument(0). It appears to be the case you cannot reference the name used to call the program. 3. The array is not actually dynamically allocated, but it is sized to the number of parameters. 4. All parameters are stored in arrays of 1024 characters, which is innefficient. It would be possible to declare a string access type and allocate a string on the heap for each parameter. (Mostly I've used Ada in safety critical environments where that is considered bad form.) But if you do this the need for the record goes away because you can then use the 'length attribute to write out the argument. I don't think it is possible to have different length strings holding the arrays without using accesses and dynamic allocations (someone correct me if I'm wrong). James Alan Farrell