I'm writing an Ada program that is supposed to do case conversion for alphabetic characters. The program uses 1, 2 or 3 command line arguments. I pretty much have the thing written up, but I have no clue as to how to how to do arguments. the command line arguments are to:
Any help?
I would suggest something like that, as already said using Ada.Command_Line:
with
Ada.Text_IO,
Ada.Command_Line,
Ada.Strings.Bounded;
use
Ada.Text_IO,
Ada.Command_Line;
procedure Main is
package SB is new Ada.Strings.Bounded.Generic_Bounded_Length (Max => 100);
use SB;
Cur_Argument : SB.Bounded_String;
Input_File_Path : SB.Bounded_String;
Output_File_Path : SB.Bounded_String;
I : Integer := 1;
begin
-- For all the given arguments
while I < Argument_Count loop
Cur_Argument := SB.To_Bounded_String(Argument(I));
if Cur_Argument = "U" or Cur_Argument = "u"
then
-- stuff for uppercase
elsif Cur_Argument = "L" or Cur_Argument = "l"
then
-- stuff for lowercase
elsif Cur_Argument = "i"
then
-- following one is the path of the file
Input_File_Path := SB.To_Bounded_String(Argument(I+1));
i := i + 1;
elsif Cur_Argument = "o"
then
Output_File_Path := SB.To_Bounded_String(Argument(I+1));
i := i + 1;
else
Put_Line("Wrong arguments");
end if;
i := i + 1;
end loop;
end Main;
You could use the standard package Ada.Command_Line
to access command line arguments.
You have Argument_Count
for the number of arguments.
You have Argument(Number : Positive)
to get the argument string at position Number
.
The Ada.Command_Line package is standard and perfectly adequate for the task you have.
More complex command line parsing becomes hard work using Ada.Command_Line. If you need named rather than positional association for your command line, see this Gem from Adacore on using Gnat.Command_Line for (less portable, if that matters, but) more Unix-like command line sequences of parameters and options.
There is also a Generic Command Line Parser which I have used successfully on a small project.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With