I'm having an issue parsing input arguments in Ada using the GNAT.Command_Line
package. I've modeled my approach based off resources such as this Ada Gem as well as this answer, but can't seem to get the argument-accepting switches working correctly.
main.adb
:
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
with Command_Line; use Command_Line;
procedure Main is
CLS : Command_Line_State;
begin
Parse_Command_Line (State => CLS);
end Main;
Command_Line.ads
:
with Ada.Containers.Vectors; use Ada.Containers;
with Ada.Directories; use Ada.Directories;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
with Ada.Text_IO; use Ada.Text_IO;
with Gnat.Command_Line; use Gnat.Command_Line;
with Gnat.OS_Lib; use Gnat.OS_Lib;
with Gnat.Strings; use Gnat.Strings;
package Command_Line is
type Output_Type is (CSV, TXT);
type Filename_Record is record
Was_Given : Boolean;
Filename : Unbounded_String;
Path : Unbounded_String;
end record;
package Filename_Vectors is new Vectors (Positive, Filename_Record);
type Command_Line_State is record
Filenames : Filename_Vectors.Vector;
Interactive_Mode : Boolean := False;
Output_Format : Output_Type;
end record;
procedure Parse_Command_Line (State : in out Command_Line_State);
end Command_Line;
Command_Line.adb
:
package body Command_Line is
procedure Parse_Command_Line(State : in out Command_Line_State) is
Configuration : Command_Line_Configuration;
In_Path : aliased Gnat.Strings.String_Access;
Out_Path : aliased Gnat.Strings.String_Access;
Interactive : aliased Boolean := False;
CSV_Format : aliased Boolean := False;
begin
Define_Switch (Config => Configuration,
Output => In_Path'Access,
Switch => "-i",
Long_Switch => "--inpath",
Help => "Supplies an external path for case files not colocated with the program",
Argument => "PATH");
Define_Switch (Config => Configuration,
Output => Out_Path'Access,
Switch => "-o",
Long_Switch => "--outpath",
Help => "Identifies an external path or directory to write output data to",
Argument => "PATH");
Define_Switch (Config => Configuration,
Output => Interactive'Access,
Switch => "-n",
Long_Switch => "--interactive",
Help => "Places program into interactive mode");
Define_Switch (Config => Configuration,
Output => CSV_Format'Access,
Switch => "-c",
Long_Switch => "--csv",
Help => "Output data in CSV, rather than TXT format");
Getopt (Configuration);
declare
In_Path_Unbounded : Unbounded_String := To_Unbounded_String (In_Path.all);
Out_Path_Unbounded : Unbounded_String := To_Unbounded_String (Out_Path.all);
begin
Put_Line ("Input file path: '" & To_String (In_Path_Unbounded) & "'.");
Put_Line ("Output file path: '" & To_String (Out_Path_Unbounded) & "'.");
end;
if Interactive then
Put_Line ("Interactive mode (-n, --interactive) was set to: True.");
else
Put_Line ("Interactive mode (-n, --interactive) was set to: False.");
end if;
if CSV_Format then
Put_Line ("CSV formatting mode (-c, --csv) was set to: True.");
else
Put_Line ("CSV formatting mode (-c, --csv) was set to: False.");
end if;
end Parse_Command_Line;
end Command_Line;
Note that some structure is present for the final end use application. When I run the program from a PowerShell window with the following options:
.\main.exe -i INPATH -o OUTPATH -n -c
I get the following result:
Input file path: ''.
Output file path: ''.
Interactive mode (-n, --interactive) was set to: True.
CSV formatting mode (-c, --csv) was set to: True.
How do I modify my existing code so that the Getopt
parser and GNAT.Command_Line
correctly capture the arguments (i.e. INPATH
and OUTPATH
) for string-accepting options?