Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Command line arguments for Ada

Tags:

ada

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:

  1. A single character specifying whether uppercase conversion or lowercase conversion is to be applied to the input. 'U' or 'u' signifies uppercase conversion; 'L' or 'l' specifies lowercase conversion. This parameter is required for the program to run.
  2. (optional) The name of the file to be used for input to the uppercase/lowercase conversion. If this parameter is not specified, the program must read from standard input.
  3. (optional and only used if the third command line parameter is also provided) The name of the file to be used for output from the encryption or decryption process. If this parameter is not specified, the program must write to standard output.

Any help?

like image 556
Derpboom Avatar asked Jan 24 '13 00:01

Derpboom


3 Answers

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;
like image 108
clx Avatar answered Oct 20 '22 11:10

clx


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.

like image 31
qunying Avatar answered Oct 20 '22 13:10

qunying


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.

like image 39
user_1818839 Avatar answered Oct 20 '22 12:10

user_1818839