Command Line Flags in Ada

Our first program demonstrates how to use command-line flags. Here’s the full source code:

with Ada.Text_IO;
with Ada.Command_Line;
with GNAT.Command_Line;

procedure Command_Line_Flags is
   use Ada.Text_IO;
   use Ada.Command_Line;
   use GNAT.Command_Line;

   Word : String_Access := new String'("foo");
   Numb : Integer := 42;
   Fork : Boolean := False;
   Svar : String_Access := new String'("bar");

begin
   -- Define command-line options
   Define_Switch (Switch => "-w=", Long_Switch => "--word=",
                  Help => "a string", Variable => Word);
   Define_Switch (Switch => "-n=", Long_Switch => "--numb=",
                  Help => "an int", Variable => Numb);
   Define_Switch (Switch => "-f", Long_Switch => "--fork",
                  Help => "a bool", Variable => Fork);
   Define_Switch (Switch => "-s=", Long_Switch => "--svar=",
                  Help => "a string var", Variable => Svar);

   -- Parse the command line
   begin
      Getopt;
   exception
      when Invalid_Switch =>
         Put_Line ("Invalid Switch");
         return;
      when Invalid_Parameter =>
         Put_Line ("Invalid Parameter");
         return;
   end;

   -- Print out the parsed options
   Put_Line ("word: " & Word.all);
   Put_Line ("numb:" & Integer'Image (Numb));
   Put_Line ("fork:" & Boolean'Image (Fork));
   Put_Line ("svar: " & Svar.all);

   -- Print any remaining arguments
   Put ("tail:");
   for I in 1 .. Argument_Count loop
      Put (" " & Argument (I));
   end loop;
   New_Line;

   -- Free allocated memory
   Free (Word);
   Free (Svar);
end Command_Line_Flags;

This Ada program uses the GNAT.Command_Line package to handle command-line flags, which is similar to the flag package in the original example.

To experiment with the command-line flags program, first compile it and then run the resulting executable directly.

$ gnatmake command_line_flags.adb
$ ./command_line_flags -w opt -n 7 -f -s flag
word: opt
numb: 7
fork: TRUE
svar: flag
tail:

Note that if you omit flags, they automatically take their default values:

$ ./command_line_flags -w opt
word: opt
numb: 42
fork: FALSE
svar: bar
tail:

Trailing positional arguments can be provided after any flags:

$ ./command_line_flags -w opt a1 a2 a3
word: opt
numb: 42
fork: FALSE
svar: bar
tail: a1 a2 a3

Use -h or --help flags to get automatically generated help text for the command-line program:

$ ./command_line_flags --help
usage: command_line_flags [-h] [-w STRING] [-n INTEGER] [-f] [-s STRING]

  -h, --help        Display this help text
  -w, --word=STRING a string
  -n, --numb=INTEGER
                    an int
  -f, --fork        a bool
  -s, --svar=STRING a string var

If you provide a flag that wasn’t specified, the program will print an error message:

$ ./command_line_flags -x
Invalid Switch

This Ada implementation provides similar functionality to the original example, allowing users to specify command-line options and parse them in the program.