Reputation: 316
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?
Upvotes: 2
Views: 223
Reputation: 5941
The trick is to add an appropriate character to both the Switch
and Long_Switch
parameters.
In g-comlin.ads
, the description of Define_Switch
reads (see also here):
procedure Define_Switch
(Config : in out Command_Line_Configuration;
Switch : String := "";
Long_Switch : String := "";
Help : String := "";
Section : String := "";
Argument : String := "ARG");
-- Indicates a new switch. The format of this switch follows the getopt
-- format (trailing ':', '?', etc for defining a switch with parameters).
While the description of getopt
reads (in the same file, see also here):
-- Switches is a string of all the possible switches, separated by
-- spaces. A switch can be followed by one of the following characters:
--
-- ':' The switch requires a parameter. There can optionally be a space
-- on the command line between the switch and its parameter.
--
-- '=' The switch requires a parameter. There can either be a '=' or a
-- space on the command line between the switch and its parameter.
--
-- '!' The switch requires a parameter, but there can be no space on the
-- command line between the switch and its parameter.
--
-- '?' The switch may have an optional parameter. There can be no space
-- between the switch and its argument.
So, adding a colon (:
) to the arguments of both Switch
and Long_Switch
will do the trick. For example:
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");
Adapting the Switch
and Long_Switch
parameters for both -i
and -o
will now given the desired result:
$ ./obj/main -i "Foo" -o "Bar" -n -c
Input file path: 'Foo'.
Output file path: 'Bar'.
Interactive mode (-n, --interactive) was set to: True.
CSV formatting mode (-c, --csv) was set to: True.
Upvotes: 3