Ada Command Line Arguments

I am writing an Ada program that must convert case for alphabetic characters. The program uses 1, 2, or 3 command line arguments. I pretty much have a written thing, but I have no idea how to make arguments. command line arguments:

  • One character indicating whether the conversion is in upper case or whether the conversion in lower case is applied to the input. β€œU” or β€œu” means uppercase conversion; 'L' or 'l' indicates lowercase conversion. This parameter is required to run the program.
  • (optional) The name of the file that will be used to enter the upper or lower registration conversion. If this parameter is not specified, the program should read from standard input.
  • (optional and used only if the third command-line option is also specified). The name of the file to be used to output from the encryption or decryption process. If this parameter is not specified, the program should write to standard output.

Any help?

+6
source share
3 answers

You can use the standard Ada.Command_Line package to access command line arguments.

You have an Argument_Count for the number of arguments. You have Argument(Number : Positive) to get the argument string at the Number position.

+8
source

The Ada.Command_Line package is standard and ideal for your task.

More complex command line parsing becomes hard work using Ada.Command_Line. If you need named and not positional association for your command line, see This Gem from Adacore when using Gnat.Command_Line (less portable if that matters, but) more Unix-like command line parameters and parameters.

There is also a Generic Command Line Parser that I have successfully used in a small project.

+4
source

I would suggest something similar, as already mentioned, 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; 
+1
source

All Articles