Command Line, Environment Variables & Directories
Read program arguments with Ada.Command_Line (Argument_Count, Argument, Command_Name, Set_Exit_Status), get and set environment variables with Ada.Environment_Variables, and manage files and folders with Ada.Directories (Create_Path, Compose, Extension, Base_Name, searches, Kind, Size, Delete_Tree), plus GNAT.OS_Lib and GNAT.Command_Line.
Command-line tools written in Ada rely on three standard packages: Ada.Command_Line for arguments and the exit status, Ada.Environment_Variables for the process environment, and Ada.Directories for portable file-system operations. Together they make Ada a practical language for build tools, converters and system utilities.
Ada.Command_Line
Argument_Count returns the number of arguments; Argument (N) returns the Nth as a String (1-based); Command_Name returns the program name as invoked (possibly with a path — Ada.Directories.Simple_Name strips it). Set_Exit_Status (Code) sets the status reported when the main program ends (Success and Failure are predefined constants). For option parsing, GNAT provides GNAT.Command_Line (getopt-style switches) — or use a library from Alire.
• Argument_Count, Argument (I), Command_Name. • Set_Exit_Status (Success | Failure | N). • GNAT.Command_Line for switch parsing.
Environment Variables
Ada.Environment_Variables: Value (Name) (raises Constraint_Error if undefined; Ada 2022 adds Value (Name, Default)), Exists (Name), Set, Clear (Name), and Iterate to visit all variables. Changes affect the current process and child processes started afterwards.
• Value / Exists / Set / Clear / Iterate. • Value of a missing variable raises Constraint_Error (use Exists or the Default overload).
Ada.Directories
Path manipulation: Compose (Dir, Name, Ext) builds paths with the platform separator (\ on Windows), Simple_Name, Containing_Directory, Base_Name, Extension, Full_Name, Current_Directory, Set_Directory. File system: Exists, Kind (Ordinary_File, Directory, Special_File), Size, Modification_Time, Create_Directory, Create_Path (like mkdir -p), Delete_File, Delete_Directory, Delete_Tree, Rename, Copy_File. Searching: Start_Search (Search, Dir, Pattern), More_Entries, Get_Next_Entry, End_Search, or the Search procedure with a callback. Entry order is not specified, so sort names if you need stable output. Ada 2022 adds Ada.Directories.Hierarchical_File_Names.
• Compose / Simple_Name / Base_Name / Extension / Containing_Directory. • Exists / Kind / Size / Create_Path / Delete_Tree / Copy_File / Rename. • Start_Search → Get_Next_Entry loop; order unspecified.
Code Examples
Run on Windows as 'l51_cmdline 10 abc 32'. Compose uses a backslash, and the 5- and 3-character files are 7 and 5 bytes because Text_IO wrote a CR LF line terminator on Close. The last line is from the test harness reporting the exit status set by Set_Exit_Status (3). Output: Program: l51_cmdline.exe Argument_Count = 3 Argument ( 1 ) = 10 Argument ( 2 ) = abc (not a number) Argument ( 3 ) = 32 Sum of numeric arguments = 42 ADA_DEMO = hello Exists ("NO_SUCH_VAR_123") = FALSE Compose -> demo_dir\a.txt Extension ("report.final.pdf") = pdf, Base_Name = report.final entry: a.txt (ORDINARY_FILE, 7 bytes) entry: b.log (ORDINARY_FILE, 5 bytes) entry: sub (DIRECTORY) demo_dir exists after Delete_Tree? FALSE [process exited with status 3]
Shells expose the status set with Set_Exit_Status.
Path := Dir & "/" & Name;
Path := Ada.Directories.Compose (Dir, Name);
Compose uses the correct separator for the platform.
Home := Ada.Environment_Variables.Value ("HOME"); -- may not exist
Home := (if Exists ("HOME") then Value ("HOME") else "");
Value raises Constraint_Error for undefined variables.
-- relying on directory search order -- collect names into an ordered set, then print
Search order is implementation- and file-system-dependent.
Which function returns the Nth command-line argument? A. Argv (N) B. Ada.Command_Line.Argument (N) C. Get_Arg (N) D. Args (N)
How do you create a directory and all missing parents? A. Create_Directory B. Create_Path C. Make_Dirs D. Mkdir_P
What does Extension ("report.final.pdf") return? A. final.pdf B. pdf C..pdf D. report B. pdf (verified).
How does a program report exit code 3? A. return 3; B. Set_Exit_Status (3); C. exit (3); D. raise Exit_3;
Trace the command-line example (run with arguments 10 abc 32).
Real output: Program: l51_cmdline.exe Argument_Count = 3 Argument ( 1 ) = 10 Argument ( 2 ) = abc (not a number) Argument ( 3 ) = 32 Sum of numeric arguments = 42 ADA_DEMO = hello Exists ("NO_SUCH_VAR_123") = FALSE Compose -> demo_dir\a.txt Extension ("report.final.pdf") = pdf, Base_Name = report.final entry: a.txt (ORDINARY_FILE, 7 bytes) entry: b.log (ORDINARY_FILE, 5 bytes) entry: sub (DIRECTORY) demo_dir exists after Delete_Tree? FALSE [process exited with status 3]
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Command_Line; use Ada.Command_Line;
with Ada.Directories; use Ada.Directories;
with Ada.Environment_Variables;
with Ada.Containers.Indefinite_Ordered_Sets;
procedure L51_Cmdline is
package Name_Sets is new Ada.Containers.Indefinite_Ordered_Sets (String);
Found : Name_Sets.Set;
Sum : Integer := 0;
procedure Touch (Name, Content : String) is
F : File_Type;
begin
Create (F, Out_File, Name);
Put (F, Content);
Close (F);
end Touch;
begin
-- Command-line arguments
Put_Line ("Program: " & Simple_Name (Command_Name));
Put_Line ("Argument_Count =" & Natural'Image (Argument_Count));
for I in 1 .. Argument_Count loop
Put_Line (" Argument (" & Integer'Image (I) & " ) = " & Argument (I));
begin
Sum := Sum + Integer'Value (Argument (I));
exception
when Constraint_Error => Put_Line (" (not a number)");
end;
end loop;
Put_Line ("Sum of numeric arguments =" & Integer'Image (Sum));
-- Environment variables
Ada.Environment_Variables.Set ("ADA_DEMO", "hello");
Put_Line ("ADA_DEMO = " & Ada.Environment_Vari
…./l51_cmdline 10 abc 32
echo $? # prints 3 (the exit status)