ZetCode

Ada command-line arguments

last modified November 23, 2026

A program started from a shell receives its arguments as text. The shell splits the command line into words, removes the quotes it used for grouping, and passes the remaining words to the program. Ada exposes them through Ada.Command_Line: the name of the program, the number of arguments, and each argument as a String. This article reads those values, converts numeric arguments, parses options, and sets the exit status that the shell reports.

The program name and the count

Ada.Command_Line is a predefined library package, so a program only needs a with clause. Command_Name returns the name that started the program, and Argument_Count returns the number of words after it. Each word is available through Argument (I), where I runs from 1 to the count.

args_demo.adb
with Ada.Text_IO;
with Ada.Command_Line;
use Ada.Text_IO;

procedure Args_Demo is
begin
   Put_Line ("program:" & Ada.Command_Line.Command_Name);
   Put_Line ("count:" & Natural'Image (Ada.Command_Line.Argument_Count));
end Args_Demo;

The name is reported exactly as the shell passed it, so a program started as ./args_demo prints that relative path rather than an absolute one. The count does not include the program name, and the arguments are numbered from 1. The parameter of Argument has the subtype Positive, so there is no argument 0: a program that needs the command name uses Command_Name. Quoting on the shell side decides how many words arrive: alpha "two words" is two arguments, not three.

$ gnatmake -gnat2022 -gnatwa args_demo.adb
$ ./args_demo alpha "two words"
program:./args_demo
count: 2

Reading the values

A loop over the range 1 .. Argument_Count visits every argument. Each one is still a String, even when it looks like a number, so a program that needs a number converts it with Integer'Value. The loop below prints the arguments and adds their values to a running total.

args_values.adb
with Ada.Text_IO;
with Ada.Command_Line;
use Ada.Text_IO;

procedure Args_Values is
   Total : Integer := 0;
begin
   Put_Line ("program:" & Ada.Command_Line.Command_Name);
   Put_Line ("count:" & Natural'Image (Ada.Command_Line.Argument_Count));
   for I in 1 .. Ada.Command_Line.Argument_Count loop
      Put_Line (Natural'Image (I) & ": " & Ada.Command_Line.Argument (I));
      Total := Total + Integer'Value (Ada.Command_Line.Argument (I));
   end loop;
   Put_Line ("sum =" & Integer'Image (Total));
end Args_Values;

The first argument is printed as 1: 10, because Natural'Image begins with a blank for a non-negative value. The conversion accepts the usual decimal notation, including a leading sign, and raises Constraint_Error when the text is not a number. The reading input article shows how to catch that exception and ask again; here the arguments are expected to be valid.

$ gnatmake -gnat2022 -gnatwa args_values.adb
$ ./args_values 10 20 12
program:./args_values
count: 3
 1: 10
 2: 20
 3: 12
sum = 42

A usage line and an exit status

A program that cannot work without arguments tests Argument_Count before it reads any of them. The usage message belongs on standard error, which is where a terminal shows errors, and the program should tell the shell that it failed. Set_Exit_Status (Failure) arranges that, and return leaves the procedure before the loop.

arg_sum.adb
with Ada.Text_IO;
with Ada.Command_Line;
use Ada.Text_IO;

procedure Arg_Sum is
   Total : Integer := 0;
begin
   if Ada.Command_Line.Argument_Count = 0 then
      Put_Line (Standard_Error, "usage: arg_sum NUMBER [NUMBER ...]");
      Ada.Command_Line.Set_Exit_Status (Ada.Command_Line.Failure);
      return;
   end if;

   for I in 1 .. Ada.Command_Line.Argument_Count loop
      Total := Total + Integer'Value (Ada.Command_Line.Argument (I));
   end loop;
   Put_Line ("sum =" & Integer'Image (Total));
end Arg_Sum;

Success is zero and Failure is one, and a program that does not call Set_Exit_Status ends with success. The shell keeps the status in $?, which is why the output below reads it after the second run. The usage line is written to Standard_Error, but the terminal shows both streams together, so it appears in the same place as the normal output.

$ gnatmake -gnat2022 -gnatwa arg_sum.adb
$ ./arg_sum 10 20 12
sum = 42
$ ./arg_sum
usage: arg_sum NUMBER [NUMBER ...]
$ echo $?
1

Parsing options

Options are ordinary arguments. A parser compares each one with the names it knows and, for an option that takes a value, also reads the next argument. The loop below uses a variable index instead of a for loop, because -n has to consume the word that follows it and then skip it as well.

options.adb
with Ada.Text_IO;
with Ada.Command_Line;
use Ada.Text_IO;

procedure Options is
   use Ada.Command_Line;

   Verbose : Boolean := False;
   Count   : Positive := 1;
   I       : Integer := 1;
begin
   while I <= Argument_Count loop
      if Argument (I) = "--verbose" then
         Verbose := True;
      elsif Argument (I) = "-n" then
         I := I + 1;
         Count := Positive'Value (Argument (I));
      else
         Put_Line ("unknown option:" & Argument (I));
         Set_Exit_Status (Failure);
         return;
      end if;
      I := I + 1;
   end loop;

   for N in 1 .. Count loop
      Put_Line (Natural'Image (N) & ": working");
   end loop;
   Put_Line ("verbose =" & Boolean'Image (Verbose));
end Options;

--verbose sets a flag and moves past that word. -n reads the following word as a Positive, so -n 0 would raise Constraint_Error rather than produce an empty loop. Every other word is reported as an unknown option and ends the program with failure. The parser assumes that a value follows -n; a word at the end of the command line would make Argument (I) read past the count. A more careful parser tests I = Argument_Count before it advances.

$ gnatmake -gnat2022 -gnatwa options.adb
$ ./options -n 3 --verbose
 1: working
 2: working
 3: working
verbose =TRUE
$ ./options --bogus
unknown option:--bogus
$ echo $?
1

An applied example: a greeting tool

The last program combines the pieces: two options with values, default values for both, and a loop that repeats a line. The name is kept in an Unbounded_String, because a command-line argument can have any length and a fixed String would have to be given one in advance. The number of repetitions is a Positive, so the loop always runs at least once.

greet.adb
with Ada.Text_IO;
with Ada.Command_Line;
with Ada.Strings.Unbounded;
use Ada.Text_IO;

procedure Greet is
   use Ada.Command_Line;
   use Ada.Strings.Unbounded;

   Name  : Unbounded_String := To_Unbounded_String ("there");
   Times : Positive := 1;
   I     : Integer := 1;
begin
   while I <= Argument_Count loop
      if Argument (I) = "--name" then
         I := I + 1;
         Name := To_Unbounded_String (Argument (I));
      elsif Argument (I) = "--times" then
         I := I + 1;
         Times := Positive'Value (Argument (I));
      else
         Put_Line (Standard_Error, "usage: greet [--name NAME] [--times N]");
         Set_Exit_Status (Failure);
         return;
      end if;
      I := I + 1;
   end loop;

   for N in 1 .. Times loop
      Put_Line ("Hello, " & To_String (Name) & "!");
   end loop;
end Greet;

Without options the program greets there once. With --name Ada --times 3 it prints three lines with the given name, and the order of the two options does not matter. The program reports an unknown option and sets failure, but it does not check that a value follows --name or --times; that check is the next small improvement.

$ gnatmake -gnat2022 -gnatwa greet.adb
$ ./greet --name Ada --times 3
Hello, Ada!
Hello, Ada!
Hello, Ada!
$ ./greet
Hello, there!

Next steps

The next article stays outside the program and looks at its surroundings. It reads and changes environment variables, lists them, starts a child process with GNAT.OS_Lib.Spawn, and passes an exit status back to the shell.

Source

Ada Reference Manual

GNAT User's Guide

AdaCore learning resources

In this article we have read the program name and the argument count, listed and converted arguments, printed a usage line, set the exit status, parsed two options, and built a small greeting tool. The reading input article covers values that arrive from the terminal rather than from the command line.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all Ada tutorials.