10

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?

NWS
  • 3,080
  • 1
  • 19
  • 34
Derpboom
  • 211
  • 2
  • 3
  • 6

3 Answers3

9

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.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
qunying
  • 428
  • 3
  • 4
7

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.

  • 1
    When using GNAT, it's much easier using the high-lever api in GNAT.Command_Line. Source: http://www.adacore.com/adaanswers/gems/gem-138-gnatcollcommand-line/ – flotto Oct 26 '15 at 11:55
3

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;
clx
  • 794
  • 2
  • 8
  • 19
  • This program doesn't meet the specification: you've invented 'i' and 'o', and the first parameter is mandatory and must be l|L|u|U. And what happens if a user leaves out the filename after 'i' or 'o'? – Simon Wright Jan 24 '13 at 20:16
  • well true, but I haven't found something like [optArg](https://en.wikipedia.org/wiki/Getopt). I think a complete check, about the file paths (which fails if paths are missing) is a different topic. – clx Jan 24 '13 at 21:33