# Ada Programming/Libraries/Ada.Command Line

This language feature is available from Ada 95 on.
Ada.Command_Line is a unit of the Predefined Language Environment since Ada 95.

## Introduction

The Ada.Command_Line package enables the Ada program to access the command line arguments set by the command that invoked the program. It is the Ada version of the C argv and argc[n] system.
If the environment supports it, it is also possible to set the Ada program's exit status with Ada.Command_Line.
It is important to note, that all the code in this article is compiled and executed on a Slackware Linux system. The compiler used is GNATMAKE GPL 2008 (20080521) and the shell is BASH. This is important because the reference manual clearly states that:
If the external execution environment supports passing arguments to a program, then Argument_Count returns the number of arguments passed to the program invoking the function. Otherwise, it returns 0. The meaning of “number of arguments” is implementation defined.
Note the closing sentence: The meaning of “number of arguments” is implementation defined. What this means, is that you cannot trust a given argument string to yield the same result for all environments. Argument_Count is highly dependent on factors such as operating system, shell, and compiler.

## Using Ada.Command_Line

The program we'll be using throughout this article doesn't actually do anything, except report the various arguments we've used when invoking the program. The basic program looks like this:
When executing the above program with no arguments given, you should get the following output:
Let's quickly go over the program: On lines 1 and 2 we add the Text_IO and Command_Line packages to the program. On lines 5 and 6 we rename those packages to the shorter IO and CLI and finally on line 8 we output the current argument count. And that is really all that is required to enable access to the command line arguments given to the program.
The full specification of the Command_Line package is about as simple as such specifications can be.
We've already encountered the Argument_Count function in the Args program, but lets see what happens when we invoke the Args program with some arguments.

### Ada.Command_Line.Argument_Count

The specification for Argument_Count looks like this:
The basic Args program already use the Argument_Count function, so to see how it works, all we have to do is supply the program with a variety of arguments:
A pattern should've emerged by now. Of particular interest is the last example: Because the whitespace is escaped, the entire string is treated as one argument. One could easily imagine that another environment might treat such an argument differently and perhaps ignore the backslash. The same holds for the double quotes example ("Thomas Løcke"). Other shells/environments might very well treat "Thomas Løcke" as two separate arguments because of the whitespace. The key thing is to be aware of such details when using Ada.Command_Line.

### Ada.Command_Line.Argument

The specification for Argument looks like this:
Argument returns a String argument corresponding to the relative position Number. Note that Number is of type Positive. This means that the valid range of Number is 1 .. Argument_Count. If Number is outside this range, a Constraint_Error is raised.
To see how it works, we add 3 more lines to our small Args program:
If we run Args with the same arguments as given in the Argument_Count example, we get these results:
There are no big surprises here.
If no arguments are given when running the program, you will end up with a null range loop: (1 .. 0). This is perfectly valid and the result is as expected: The loop is terminated immediately.

### Ada.Command_Line.Command_Name

The specification for Command_Name looks like this:
Command_Name returns the string command used to invoke the program. As is the case with Argument_Count, this is implementation-defined, so Command_Name might return X on system A, Y on system B and null on system C. You cannot rely on this value being the same across all combinations of compilers, shells, and operating systems.
To see how it works, add this line to the Args program:
If we execute the program, without any arguments, from within GNAT Studio (using Shift+F2), the result will be this:
Notice how GNAT Studio uses the full path to the Args program to invoke it.
If I change directory to /home/thomas/wiki_examples/Args/, and execute the Args program from the commandline, I get this:
If I use the common ./ syntax to execute the program, I get this:
As you can see, in an environment that supports argument passing, the Command_Name function returns the exact string used to invoke the program.

### Ada.Command_Line.Set_Exit_Status

The specification for Set_Exit_Status looks like this:
If the environment supports the return of an task completion code, Set_Exit_Status allows you to set an exit status for the program. If you look at the specification for Ada.Command_Line, you will notice the type Exit_Status and the two variables Success and Failure. These two are defined as 0 and 1, respectively, but it is entirely possible to define your own exit status codes.
To see how that works, add this to the specification of the Args program:
And this to the body of the program:
What we do here is set the exit status depending on the number of arguments given to the program. This is what happens when we execute the program with various arguments:
echo $? outputs the exit code of the most recently executed program. On the first execution, we get a failure because no arguments are given. On the second execution, we get a success because exactly one argument is given. On the third execution, we get the special exit code 3 because more than one argument is given.

## See also
### Wikibook

* Ada Programming/Libraries/Ada

### External examples

* Search for examples of Ada.Command_Line in: Rosetta Code, GitHub (gists), any Alire crate or this Wikibook.
* Search for posts related to Ada.Command_Line in: Stack Overflow, comp.lang.ada or any Ada related page.

### Ada Reference Manual
#### Ada 95

* A.15: The Package Command_Line

#### Ada 2005

* A.15: The Package Command_Line

#### Ada 2012

* A.15: The Package Command_Line

### Open-Source Implementations

FSF GNAT
* Specification: a-comlin.ads
* Body: a-comlin.adb
drake
* Specification: environment/a-comlin.ads
* Body: environment/a-comlin.adb