首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >GNAT.Command_Line中的选项参数问题

GNAT.Command_Line中的选项参数问题
EN

Stack Overflow用户
提问于 2022-02-01 00:14:02
回答 1查看 107关注 0票数 2

我在使用GNAT.Command_Line包解析Ada中的输入参数时遇到了问题。我以这个阿达宝石这个答案等资源为基础对我的方法进行了建模,但似乎无法使接受参数的开关正常工作。

main.adb

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
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;

请注意,对于最终的最终使用应用程序,存在一些结构。当我使用以下选项从PowerShell窗口运行程序时:

代码语言:javascript
复制
.\main.exe -i INPATH -o OUTPATH -n -c

我得到以下结果:

代码语言:javascript
复制
Input file path:  ''.
Output file path: ''.
Interactive mode (-n, --interactive) was set to: True.
CSV formatting mode (-c, --csv) was set to:      True.

如何修改现有代码,以便Getopt解析器和GNAT.Command_Line正确捕获字符串接受选项的参数(即INPATHOUTPATH)?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-02-01 09:57:25

诀窍是在SwitchLong_Switch参数中添加一个适当的字符。

g-comlin.ads中,对Define_Switch的描述如下(另请参阅这里):

代码语言:javascript
复制
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).

getopt的描述读取时(在同一个文件中,也请参见这里):

代码语言:javascript
复制
--  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.

因此,将冒号(:)添加到SwitchLong_Switch的参数中就可以了。例如:

代码语言:javascript
复制
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");

现在,将SwitchLong_Switch参数调整为-i-o,将得到所需的结果:

代码语言:javascript
复制
$ ./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.
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70934311

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档