Skip to content

Commit

Permalink
Merge branch 'topic/vadim/cmlline' into 'master'
Browse files Browse the repository at this point in the history
Multivalue positional option support

Closes #223 and #231

See merge request eng/ide/VSS!310
  • Loading branch information
godunko committed Jan 22, 2024
2 parents fbe24ce + 1bda7c0 commit b2df4a4
Show file tree
Hide file tree
Showing 7 changed files with 290 additions and 63 deletions.
72 changes: 67 additions & 5 deletions source/os/implementation/vss-command_line-parsers.adb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
--
-- Copyright (C) 2022-2023, AdaCore
-- Copyright (C) 2022-2024, AdaCore
--
-- SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
--
Expand Down Expand Up @@ -74,6 +74,10 @@ package body VSS.Command_Line.Parsers is
Named_Option'Class (Option));
end if;

elsif Option in Multivalue_Positional_Option'Class then
Self.Defined_Multivalue_Option.Replace_Element
(Multivalue_Positional_Option (Option));

else
Self.Defined_Positional_Options.Append
(Positional_Option'Class (Option));
Expand Down Expand Up @@ -250,6 +254,12 @@ package body VSS.Command_Line.Parsers is
Usage.Append (Option.Name);
end loop;

if not Self.Defined_Multivalue_Option.Is_Empty then
Usage.Append (' ');
Usage.Append (Self.Defined_Multivalue_Option.Element.Name);
Usage.Append ('');
end if;

Result.Append (Usage);
end;

Expand Down Expand Up @@ -285,10 +295,14 @@ package body VSS.Command_Line.Parsers is
end loop;
end if;

if not Self.Defined_Positional_Options.Is_Empty then
if not Self.Defined_Positional_Options.Is_Empty
or else not Self.Defined_Multivalue_Option.Is_Empty
then
Result.Append (VSS.Strings.Empty_Virtual_String);
Result.Append ("Arguments:");
end if;

if not Self.Defined_Positional_Options.Is_Empty then
for Option of Self.Defined_Positional_Options loop
Option_Text.Clear;
Option_Text.Append (" ");
Expand All @@ -298,6 +312,15 @@ package body VSS.Command_Line.Parsers is
end loop;
end if;

if not Self.Defined_Multivalue_Option.Is_Empty then
Option_Text.Clear;
Option_Text.Append (" ");
Option_Text.Append (Self.Defined_Multivalue_Option.Element.Name);

Append_Option_Description
(Option_Text, Self.Defined_Multivalue_Option.Element.Description);
end if;

return Result;
end Help_Text;

Expand All @@ -314,6 +337,14 @@ package body VSS.Command_Line.Parsers is
Self.Known_Named_Options_Values.Contains
(Named_Option'Class (Option).Unique_Name);

elsif Option in Multivalue_Positional_Option'Class then
return
not Self.Defined_Multivalue_Option.Is_Empty
and then Self.Defined_Multivalue_Option.Element
= Multivalue_Positional_Option (Option)
and then Natural (Self.Defined_Positional_Options.Length)
< Self.Positional_Options_Values.Length;

else
return
Self.Defined_Positional_Options.Find_Index
Expand Down Expand Up @@ -616,10 +647,24 @@ package body VSS.Command_Line.Parsers is

else
if Self.Defined_Positional_Options.Is_Empty then
Self.Error_Message := "unexpected positional argument";
Success := False;
if Self.Defined_Multivalue_Option.Is_Empty then
Self.Error_Message := "unexpected positional argument";
Success := False;

return;
return;
end if;

else
if Self.Positional_Options_Values.Length
= Natural (Self.Defined_Positional_Options.Length)
then
if Self.Defined_Multivalue_Option.Is_Empty then
Self.Error_Message := "unexpected positional argument";
Success := False;

return;
end if;
end if;
end if;

Self.Positional_Options_Values.Append (Argument);
Expand Down Expand Up @@ -776,4 +821,21 @@ package body VSS.Command_Line.Parsers is
end return;
end Values;

------------
-- Values --
------------

function Values
(Self : Command_Line_Parser'Class;
Option : Multivalue_Positional_Option'Class)
return VSS.String_Vectors.Virtual_String_Vector is
begin
return
Self.Positional_Options_Values.Slice
(Natural (Self.Defined_Positional_Options.Length) + 1,
Self.Positional_Options_Values.Length);

return VSS.String_Vectors.Empty_Virtual_String_Vector;
end Values;

end VSS.Command_Line.Parsers;
11 changes: 11 additions & 0 deletions source/os/implementation/vss-command_line.adb
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,17 @@ package body VSS.Command_Line is
-- Values --
------------

function Values
(Option : Multivalue_Positional_Option'Class)
return VSS.String_Vectors.Virtual_String_Vector is
begin
return Parser.Values (Option);
end Values;

------------
-- Values --
------------

function Values
(Option : Name_Value_Option'Class) return Name_Value_Vectors.Vector is
begin
Expand Down
14 changes: 13 additions & 1 deletion source/os/vss-command_line-parsers.ads
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
--
-- Copyright (C) 2022-2023, AdaCore
-- Copyright (C) 2022-2024, AdaCore
--
-- SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
--
Expand All @@ -8,6 +8,7 @@

private with Ada.Containers.Hashed_Maps;
private with Ada.Containers.Indefinite_Hashed_Maps;
private with Ada.Containers.Indefinite_Holders;
private with Ada.Containers.Indefinite_Vectors;
private with Ada.Containers.Hashed_Sets;

Expand Down Expand Up @@ -66,6 +67,12 @@ package VSS.Command_Line.Parsers is
-- Return all name=value pairs of the given option specified in the
-- command line.

function Values
(Self : Command_Line_Parser'Class;
Option : Multivalue_Positional_Option'Class)
return VSS.String_Vectors.Virtual_String_Vector;
-- Return all values provided for given multivalued positional option.

function Positional_Arguments
(Self : Command_Line_Parser'Class)
return VSS.String_Vectors.Virtual_String_Vector;
Expand Down Expand Up @@ -111,12 +118,17 @@ private
(Index_Type => Positive,
Element_Type => Named_Option'Class);

package Multivalue_Positional_Option_Holders is
new Ada.Containers.Indefinite_Holders (Multivalue_Positional_Option);

type Command_Line_Parser is tagged limited record
Defined_Named_Options_List : Named_Option_Vectors.Vector;
Defined_Short_Options : Name_Sets.Set;
Defined_Long_Options : Name_Sets.Set;
Defined_Named_Options : Named_Option_Maps.Map;
Defined_Positional_Options : Positional_Option_Vectors.Vector;
Defined_Multivalue_Option :
Multivalue_Positional_Option_Holders.Holder;

Error_Message : VSS.Strings.Virtual_String;
Only_Positional : Boolean := False;
Expand Down
9 changes: 9 additions & 0 deletions source/os/vss-command_line.ads
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ package VSS.Command_Line is
Name : VSS.Strings.Virtual_String;
end record;

type Multivalue_Positional_Option is new Abstract_Option with record
Name : VSS.Strings.Virtual_String;
end record;

type Named_Option is abstract new Abstract_Option with record
Short_Name : VSS.Strings.Virtual_String;
Long_Name : VSS.Strings.Virtual_String;
Expand Down Expand Up @@ -71,6 +75,11 @@ package VSS.Command_Line is
return VSS.String_Vectors.Virtual_String_Vector;
-- Return all values of the given option specified in the command line.

function Values
(Option : Multivalue_Positional_Option'Class)
return VSS.String_Vectors.Virtual_String_Vector;
-- Return all values of the given option specified in the command line.

function Values
(Option : Name_Value_Option'Class) return Name_Value_Vectors.Vector;
-- Return all name=value pairs of the given option specified in the
Expand Down
20 changes: 19 additions & 1 deletion source/text/implementation/vss-string_vectors.adb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
--
-- Copyright (C) 2020-2023, AdaCore
-- Copyright (C) 2020-2024, AdaCore
--
-- SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
--
Expand Down Expand Up @@ -403,6 +403,24 @@ package body VSS.String_Vectors is
end if;
end Replace;

-----------
-- Slice --
-----------

function Slice
(Self : Virtual_String_Vector'Class;
From : Positive;
To : Natural) return Virtual_String_Vector is
begin
return Result : Virtual_String_Vector do
for J in From .. To loop
exit when J > Self.Length;

Result.Append (Self (J));
end loop;
end return;
end Slice;

-----------
-- Write --
-----------
Expand Down
8 changes: 7 additions & 1 deletion source/text/vss-string_vectors.ads
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
--
-- Copyright (C) 2020-2023, AdaCore
-- Copyright (C) 2020-2024, AdaCore
--
-- SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
--
Expand Down Expand Up @@ -49,6 +49,12 @@ package VSS.String_Vectors is
Index : Positive) return VSS.Strings.Virtual_String;
-- Return given element. Return "null" string when index is out of bound.

function Slice
(Self : Virtual_String_Vector'Class;
From : Positive;
To : Natural) return Virtual_String_Vector;
-- Return elements inside given range.

function First_Element
(Self : Virtual_String_Vector'Class) return VSS.Strings.Virtual_String;
-- Return first element of the vector.
Expand Down
Loading

0 comments on commit b2df4a4

Please sign in to comment.