Skip to content

Commit 9da57b1

Browse files
committed
Made Long and Short Optional Opt_Parse
Fixed an issue in Parse_Args for Option_List_Parser which was causing constraint errors to be raised. Added a number of test cases to test both the expected and failure cases Removed a use clause that had no effect, clearing compiler warning. [V131-031] Change-Id: I13e7eb8b4ee184fb259353348cac8ec4b69c1aba
1 parent 90b6cff commit 9da57b1

File tree

102 files changed

+1229
-65
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

102 files changed

+1229
-65
lines changed

.gitattributes

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ testsuite/*/* no-precommit-check
33
testsuite/*/*/* no-precommit-check
44
testsuite/*/*/*/* no-precommit-check
55
testsuite/*/*/*/*/* no-precommit-check
6+
testsuite/*/*/*/*/*/* no-precommit-check
7+
testsuite/*/*/*/*/*/*/* no-precommit-check
8+
69

710
# Third-party package
811
src/getRSS.c no-precommit-check

src/gnatcoll-opt_parse.adb

Lines changed: 136 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222
------------------------------------------------------------------------------
2323

2424
with Ada.Command_Line;
25-
with Ada.Text_IO; use Ada.Text_IO;
2625
with Ada.Characters.Handling; use Ada.Characters.Handling;
2726
with Ada.Exceptions;
27+
with Ada.Text_IO; use Ada.Text_IO;
2828
with Ada.Unchecked_Deallocation;
2929

3030
with GNAT.OS_Lib;
@@ -128,14 +128,10 @@ package body GNATCOLL.Opt_Parse is
128128
end record;
129129

130130
overriding function Usage
131-
(Self : Flag_Parser) return String
132-
is ("[" & To_String (Self.Long) &
133-
(if Self.Short = "" then "" else "|" & To_String (Self.Short)) & "]");
131+
(Self : Flag_Parser) return String;
134132

135133
overriding function Help_Name
136-
(Self : Flag_Parser) return String
137-
is
138-
(To_String (Self.Long) & ", " & To_String (Self.Short));
134+
(Self : Flag_Parser) return String;
139135

140136
overriding function Parse_Args
141137
(Self : in out Flag_Parser;
@@ -148,6 +144,39 @@ package body GNATCOLL.Opt_Parse is
148144
type Help_Flag_Parser is new Flag_Parser with null record;
149145
-- Specific subtype of Flag_Parser to designate the help flag parser.
150146

147+
-----------
148+
-- Usage --
149+
-----------
150+
151+
overriding function Usage
152+
(Self : Flag_Parser) return String
153+
is
154+
begin
155+
if Self.Long /= "" and Self.Short /= "" then
156+
return
157+
"[" & To_String (Self.Long) & "|" & To_String (Self.Short) & "]";
158+
elsif Self.Long /= "" then
159+
return "[" & To_String (Self.Long) & "]";
160+
end if;
161+
return "[" & To_String (Self.Short) & "]";
162+
end Usage;
163+
164+
---------------
165+
-- Help_Name --
166+
---------------
167+
168+
overriding function Help_Name
169+
(Self : Flag_Parser) return String
170+
is
171+
begin
172+
if Self.Long /= "" and Self.Short /= "" then
173+
return To_String (Self.Long) & ", " & To_String (Self.Short);
174+
elsif Self.Long /= "" then
175+
return To_String (Self.Long);
176+
end if;
177+
return To_String (Self.Short);
178+
end Help_Name;
179+
151180
-----------------
152181
-- Append_Line --
153182
-----------------
@@ -730,7 +759,8 @@ package body GNATCOLL.Opt_Parse is
730759
package body Parse_Flag is
731760

732761
Self_Val : aliased Flag_Parser := Flag_Parser'
733-
(Name => +Long (3 .. Long'Last),
762+
(Name => +(if Name /= "" then Name
763+
else Long (3 .. Long'Last)),
734764
Help => +Help,
735765
Long => +Long,
736766
Short => +Short,
@@ -762,7 +792,13 @@ package body GNATCOLL.Opt_Parse is
762792
end Get;
763793

764794
begin
765-
if Enabled then
795+
if Long = "" and Short = "" then
796+
raise Opt_Parse_Error
797+
with "A long or short flag must be provided for Parse_Flag";
798+
elsif Long = "" and Name = "" then
799+
raise Opt_Parse_Error
800+
with "Either Long or Name must be provided for Parse_Flag";
801+
elsif Enabled then
766802
Parser.Data.Opts_Parsers.Append (Self);
767803
Parser.Data.All_Parsers.Append (Self);
768804
Self.Position := Parser.Data.All_Parsers.Last_Index;
@@ -783,9 +819,7 @@ package body GNATCOLL.Opt_Parse is
783819
(Self : Option_Parser) return String;
784820

785821
overriding function Help_Name
786-
(Dummy : Option_Parser) return String
787-
is
788-
(Long & ", " & Short);
822+
(Dummy : Option_Parser) return String;
789823

790824
overriding function Parse_Args
791825
(Self : in out Option_Parser;
@@ -803,7 +837,8 @@ package body GNATCOLL.Opt_Parse is
803837

804838
Self_Val : aliased Option_Parser :=
805839
Option_Parser'
806-
(Name => +Long (3 .. Long'Last),
840+
(Name => +(if Name /= "" then Name
841+
else Long (3 .. Long'Last)),
807842
Help => +Help,
808843
Parser => Parser.Data,
809844
Opt => True,
@@ -820,12 +855,32 @@ package body GNATCOLL.Opt_Parse is
820855
is
821856
begin
822857
if Usage_Text = "" then
823-
return "[" & Long & (if Short = "" then "" else "|" & Short)
824-
& " " & To_Upper (Long (3 .. Long'Last)) & "]";
858+
if Long /= "" and Short /= "" then
859+
return "[" & Long & "|" & Short & " " & (+Self.Name) & "]";
860+
elsif Long /= "" then
861+
return "[" & Long & " " & (+Self.Name) & "]";
862+
end if;
863+
return "[" & Short & " " & (+Self.Name) & "]";
825864
end if;
826865
return Usage_Text;
827866
end Usage;
828867

868+
---------------
869+
-- Help_Name --
870+
---------------
871+
872+
overriding function Help_Name
873+
(Dummy : Option_Parser) return String
874+
is
875+
begin
876+
if Long /= "" and Short /= "" then
877+
return Long & ", " & Short;
878+
elsif Long /= "" then
879+
return Long;
880+
end if;
881+
return Short;
882+
end Help_Name;
883+
829884
---------
830885
-- Get --
831886
---------
@@ -878,7 +933,13 @@ package body GNATCOLL.Opt_Parse is
878933
end Parse_Args;
879934

880935
begin
881-
if Enabled then
936+
if Long = "" and Short = "" then
937+
raise Opt_Parse_Error
938+
with "A long or short flag must be provided for Parse_Option";
939+
elsif Long = "" and Name = "" then
940+
raise Opt_Parse_Error
941+
with "Either Long or Name must be provided for Parse_Option";
942+
elsif Enabled then
882943
Parser.Data.Opts_Parsers.Append (Self);
883944
Parser.Data.All_Parsers.Append (Self);
884945
Self.Position := Parser.Data.All_Parsers.Last_Index;
@@ -930,11 +991,21 @@ package body GNATCOLL.Opt_Parse is
930991
Default_Val => Default_Val,
931992
Convert => Convert,
932993
Enabled => Enabled,
933-
Usage_Text => Usage_Text);
994+
Usage_Text => Usage_Text,
995+
Name => Name);
934996

935997
function Get
936998
(Args : Parsed_Arguments := No_Parsed_Arguments) return Arg_Type
937999
renames Internal_Option.Get;
1000+
1001+
begin
1002+
if Long = "" and Short = "" then
1003+
raise Opt_Parse_Error
1004+
with "A long or short flag must be provided for Parse_Enum_Option";
1005+
elsif Long = "" and Name = "" then
1006+
raise Opt_Parse_Error
1007+
with "Either Long or Name must be provided for Parse_Enum_Option";
1008+
end if;
9381009
end Parse_Enum_Option;
9391010

9401011
-----------------------
@@ -954,9 +1025,7 @@ package body GNATCOLL.Opt_Parse is
9541025
(Self : Option_List_Parser) return String;
9551026

9561027
overriding function Help_Name
957-
(Dummy : Option_List_Parser) return String
958-
is
959-
(Long & ", " & Short);
1028+
(Dummy : Option_List_Parser) return String;
9601029

9611030
overriding function Parse_Args
9621031
(Self : in out Option_List_Parser;
@@ -976,7 +1045,8 @@ package body GNATCOLL.Opt_Parse is
9761045
procedure Release (Self : in out Internal_Result) is null;
9771046

9781047
Self_Val : aliased Option_List_Parser :=
979-
(Name => +Long (3 .. Long'Last),
1048+
(Name => +(if Name /= "" then Name
1049+
else To_Upper (Long (3 .. Long'Last))),
9801050
Help => +Help,
9811051
Parser => Parser.Data,
9821052
Opt => True,
@@ -993,13 +1063,35 @@ package body GNATCOLL.Opt_Parse is
9931063
is
9941064
begin
9951065
if Usage_Text = "" then
996-
return "[" & Long & (if Short = "" then "" else "|" & Short)
997-
& " " & To_Upper (Long (3 .. Long'Last))
998-
& "[" & To_Upper (Long (3 .. Long'Last)) & "...]]";
1066+
if Long /= "" and Short /= "" then
1067+
return "[" & Long & "|" & Short & " " & (+Self.Name) &
1068+
" [" & (+Self.Name) & "...]]";
1069+
elsif Long /= "" then
1070+
return "[" & Long & " " & (+Self.Name) &
1071+
" [" & (+Self.Name) & "...]]";
1072+
end if;
1073+
return "[" & Short & " " & (+Self.Name) &
1074+
" [" & (+Self.Name) & "...]]";
9991075
end if;
10001076
return Usage_Text;
10011077
end Usage;
10021078

1079+
---------------
1080+
-- Help_Name --
1081+
---------------
1082+
1083+
overriding function Help_Name
1084+
(Dummy : Option_List_Parser) return String
1085+
is
1086+
begin
1087+
if Long /= "" and Short /= "" then
1088+
return Long & ", " & Short;
1089+
elsif Long /= "" then
1090+
return Long;
1091+
end if;
1092+
return Short;
1093+
end Help_Name;
1094+
10031095
---------
10041096
-- Get --
10051097
---------
@@ -1039,13 +1131,14 @@ package body GNATCOLL.Opt_Parse is
10391131
Pos : Positive;
10401132
Result : in out Parsed_Arguments) return Parser_Return
10411133
is
1042-
Last : Parser_Return := Error_Return;
1043-
10441134
Res : Parser_Result_Access
10451135
renames Result.Ref.Get.Results (Self.Position);
10461136

10471137
Tmp : Internal_Result_Access := null;
10481138

1139+
Converted_Arg : Arg_Type;
1140+
Arg_Count : Natural := 0;
1141+
10491142
begin
10501143
if Accumulate then
10511144
declare
@@ -1076,33 +1169,37 @@ package body GNATCOLL.Opt_Parse is
10761169
end if;
10771170

10781171
for I in Pos + 1 .. Args'Last loop
1079-
if Args (I).Starts_With ("--") or Args (I).Starts_With ("-") then
1080-
exit;
1081-
end if;
1082-
1083-
Last := I;
1172+
exit when Args (I).Starts_With ("-");
1173+
Arg_Count := Arg_Count + 1;
10841174
end loop;
10851175

1086-
if Last = Error_Return then
1176+
if Arg_Count = 0 then
10871177
return Error_Return;
10881178
end if;
10891179

10901180
Tmp := new Internal_Result'
10911181
(Start_Pos => Pos,
1092-
End_Pos => Last,
1182+
End_Pos => Pos + Arg_Count,
10931183
Results => Result_Vectors.Empty_Vector);
10941184

10951185
Res := Tmp.all'Unchecked_Access;
10961186

1097-
for I in 1 .. Last - Pos + 1 loop
1098-
Internal_Result (Res.all).Results (I) := Convert (+Args (I + Pos));
1187+
for I in 1 .. Arg_Count loop
1188+
Converted_Arg := Convert (+Args (Pos + I));
1189+
Internal_Result (Res.all).Results.Append (Converted_Arg);
10991190
end loop;
11001191

1101-
return Parser_Return (Last + 1);
1192+
return Parser_Return (Pos + Arg_Count + 1);
11021193
end Parse_Args;
11031194

11041195
begin
1105-
if Enabled then
1196+
if Long = "" and Short = "" then
1197+
raise Opt_Parse_Error
1198+
with "A long or short flag must be provided for Parse_Option_List";
1199+
elsif Long = "" and Name = "" then
1200+
raise Opt_Parse_Error
1201+
with "Either Long or Name must be provided for Parse_Option_List";
1202+
elsif Enabled then
11061203
Parser.Data.Opts_Parsers.Append (Self);
11071204
Parser.Data.All_Parsers.Append (Self);
11081205
Self.Position := Parser.Data.All_Parsers.Last_Index;
@@ -1213,21 +1310,15 @@ package body GNATCOLL.Opt_Parse is
12131310
New_Pos : out Parser_Return) return XString
12141311
is
12151312
begin
1216-
1217-
if
1218-
Args (Pos) = Long
1219-
or else (Short /= "" and then Args (Pos) = Short)
1220-
then
1313+
if Args (Pos) = Long or Args (Pos) = Short then
12211314
if Pos + 1 > Args'Last then
12221315
raise Opt_Parse_Error with "Incomplete option";
12231316
end if;
12241317
New_Pos := Pos + 2;
12251318
return Args (Pos + 1);
1226-
1227-
elsif Args (Pos).Starts_With (Long & "=") then
1319+
elsif Long /= "" and then Args (Pos).Starts_With (Long & "=") then
12281320
New_Pos := Pos + 1;
12291321
return Args (Pos).Slice (Long'Last + 2, Args (Pos).Length);
1230-
12311322
elsif Short /= "" and then Args (Pos).Starts_With (Short) then
12321323
New_Pos := Pos + 1;
12331324
return Args (Pos).Slice (Short'Last + 1, Args (Pos).Length);

0 commit comments

Comments
 (0)