-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathargparse.adb
1501 lines (1265 loc) · 47.9 KB
/
argparse.adb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
------------------------------------------------------------------------------
-- --
-- GNATcoverage --
-- --
-- Copyright (C) 2015-2024, AdaCore --
-- --
-- GNATcoverage is free software; you can redistribute it and/or modify it --
-- under terms of the GNU General Public License as published by the Free --
-- Software Foundation; either version 3, or (at your option) any later --
-- version. This software is distributed in the hope that it will be useful --
-- but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- --
-- TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public --
-- License for more details. You should have received a copy of the GNU --
-- General Public License distributed with this software; see file --
-- COPYING3. If not, go to http://www.gnu.org/licenses for a complete copy --
-- of the license. --
------------------------------------------------------------------------------
with Ada.Containers;
with Ada.Containers.Generic_Array_Sort;
with Ada.Containers.Hashed_Maps;
with Ada.Containers.Vectors;
with Ada.Exceptions;
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Unchecked_Deallocation;
with Outputs;
with Support_Files;
package body Argparse is
type Option_Info_Access is access constant Option_Info'Class;
type Option_Info_Array is array (Natural range <>) of Option_Info_Access;
package Option_Reference_Vectors is new Ada.Containers.Vectors
(Index_Type => Positive,
Element_Type => Option_Reference);
type Option_Reference_Vector is access Option_Reference_Vectors.Vector;
procedure Free is new Ada.Unchecked_Deallocation
(Option_Reference_Vectors.Vector, Option_Reference_Vector);
function Hash (C : Character) return Ada.Containers.Hash_Type is
(Ada.Containers.Hash_Type (Character'Pos (C)));
package Short_Options_Index is new Ada.Containers.Hashed_Maps
(Key_Type => Character,
Hash => Hash,
Element_Type => Option_Reference_Vector,
Equivalent_Keys => "=");
package Long_Options_Index is new Ada.Containers.Hashed_Maps
(Key_Type => Unbounded_String,
Hash => Strings.Hash,
Element_Type => Option_Reference_Vector,
Equivalent_Keys => "=");
type Parse_Table is record
Short_Index : Short_Options_Index.Map;
-- Options sorted by short name letter. Multiple options can have
-- similar short names, so the map contains vectors of options.
Long_Index : Long_Options_Index.Map;
-- Options sorted by long name. Multiple options can have similar long
-- names, so the map contains vectors of options.
end record;
-- Helper data structure for parsing
type Parser_Record is record
Command_Infos : Command_Info_Array;
Bool_Info : Bool_Option_Info_Array;
String_Info : String_Option_Info_Array;
String_List_Info : String_List_Option_Info_Array;
Bool_Callback : Bool_Callback_Type;
String_Callback : String_Callback_Type;
String_List_Callback : String_List_Callback_Type;
Arg_Callback : Arg_Callback_Type;
Table : Parse_Table;
end record;
procedure Free is new Ada.Unchecked_Deallocation
(Parser_Record, Parser_Access);
function Option_Name (Option : Option_Info'Class) return String;
-- Shortcut for Option_Name (Ref_For_Option)
function Get_Option
(Parser : Parser_Access;
Ref : Option_Reference)
return Option_Info_Access;
-- Return an access to the Option_Info record corresponding to Ref in
-- Parser.
function Split_Option_Text (S : String) return String_Vectors.Vector;
-- Split |-separated lists of option names, return a vector holding option
-- names.
procedure Build_Parse_Table (Parser : Parser_Access);
-- Initialize Parser.Table from Parser.*_Info. Raise a Program_Error if
-- option names are invalid/conflicting.
function Sorted_Options
(Parser : Parser_Access;
With_Internal : Boolean;
For_Command : Command_Type := No_Command)
return Option_Info_Array;
-- Return a list of options, sorted by long and then short names.
-- With_Internal and For_Command are used to filter the set of options
-- to return: they have the same meaning as in Print_Usage.
function Wrap
(Text : String;
First_Line_Indent : Natural;
Next_Lines_Indent : Natural;
Width : Natural)
return String;
-- Wrap each line in Text on Width columns (0). Each line will be prefixed
-- with First_Line_Indent spaces (1), plus Next_Line_Indent ones (2) for
-- all but the first line and plus any existing spaces prefix for each
-- line (3). All other contiguous spaces are merged otherwise (4).
--
-- To clarify, here are a few examples:
--
-- (0) Wrap ("Text text", 0, 0, 7) -> "Text" & ASCII.LF & "text"
-- (1) Wrap ("Text", 2, 0, 80) -> " Text"
-- (2) Wrap ("Text text", 2, 3, 7) -> " Text" & ASCII.LF & " text"
-- (3) Wrap (" Text", 1, 0, 80) -> " Text"
-- (4) Wrap (" Text text", 0, 0, 80) -> " Text text"
------------------
-- Command_Name --
------------------
function Command_Name
(Parser : Parser_Type;
Command : Valid_Commands)
return String
is (+Parser.Data.Command_Infos (Command).Name);
-----------------
-- Option_Name --
-----------------
function Option_Name
(Parser : Parser_Type;
Ref : Option_Reference)
return String
is
(Option_Name (Get_Option (Parser.Data, Ref).all));
-----------------
-- Option_Name --
-----------------
function Option_Name (Option : Option_Info'Class) return String is
Result : Unbounded_String;
begin
if Option.Long_Name /= "" then
Append (Result, Option.Long_Name);
end if;
if Option.Short_Name /= "" then
if Result /= "" then
Append (Result, '|');
end if;
Append (Result, Option.Short_Name);
end if;
return +Result;
end Option_Name;
-------------------
-- Value_Or_Null --
-------------------
function Value_Or_Null (Opt : String_Option) return Unbounded_String is
begin
if Opt.Present then
return Opt.Value;
else
return Null_Unbounded_String;
end if;
end Value_Or_Null;
function Value_Or_Null (Opt : String_Option) return String is
begin
return +Value_Or_Null (Opt);
end Value_Or_Null;
----------------
-- Is_Present --
----------------
function Is_Present
(Args : Parsed_Arguments;
Ref : Option_Reference) return Boolean is
begin
case Ref.Kind is
when Bool_Opt =>
return Args.Bool_Args (Ref.Bool_Option);
when String_Opt =>
return Args.String_Args (Ref.String_Option).Present;
when String_List_Opt =>
return not Args.String_List_Args (Ref.String_List_Option).Is_Empty;
end case;
end Is_Present;
-----------
-- Value --
-----------
function Value
(Args : Parsed_Arguments;
Option : String_Options;
Default : String := "") return String is
begin
return (if Args.String_Args (Option).Present
then +Args.String_Args (Option).Value
else Default);
end Value;
--------------
-- Supports --
--------------
function Supports
(Parser : Parser_Type;
Cmd : Command_Type;
Option : Option_Reference) return Boolean is
begin
case Option.Kind is
when Bool_Opt =>
return Parser.Data.Bool_Info (Option.Bool_Option).Commands (Cmd);
when String_Opt =>
return Parser.Data.String_Info
(Option.String_Option).Commands (Cmd);
when String_List_Opt =>
return Parser.Data.String_List_Info
(Option.String_List_Option).Commands (Cmd);
end case;
end Supports;
-------------
-- Unparse --
-------------
function Unparse
(Parser : Parser_Type;
Args : Parsed_Arguments;
Option : Option_Reference) return String_Vectors.Vector
is
Result : String_Vectors.Vector;
function Name (Opt : Option_Info'Class) return Unbounded_String is
(if Opt.Long_Name = ""
then Opt.Short_Name
else Opt.Long_Name);
-- Some options such as -P do not defined a long name. In this case,
-- return the short name.
begin
case Option.Kind is
when Bool_Opt =>
Result.Append
(Name (Parser.Data.Bool_Info (Option.Bool_Option)));
when String_Opt =>
Result.Append
(Name (Parser.Data.String_Info (Option.String_Option)));
Result.Append (Args.String_Args (Option.String_Option).Value);
when String_List_Opt =>
declare
Opt_Name : constant Unbounded_String :=
Name
(Parser.Data.String_List_Info (Option.String_List_Option));
begin
for Arg of Args.String_List_Args (Option.String_List_Option)
loop
Result.Append (Opt_Name);
Result.Append (Arg);
end loop;
end;
end case;
return Result;
end Unparse;
----------------
-- Get_Option --
----------------
function Get_Option
(Parser : Parser_Access;
Ref : Option_Reference)
return Option_Info_Access
is
(case Ref.Kind is
when Bool_Opt => Parser.Bool_Info (Ref.Bool_Option)'Access,
when String_Opt => Parser.String_Info (Ref.String_Option)'Access,
when String_List_Opt =>
Parser.String_List_Info (Ref.String_List_Option)'Access);
------------
-- Create --
------------
function Create
(Name : String;
Pattern : String := "";
Description : String;
Internal : Boolean)
return Command_Info
is
(+Name, +Pattern, +Description, Internal);
------------
-- Create --
------------
function Create
(Long_Name, Short_Name, Help : String := "";
Commands : Command_Set := All_Commands;
Internal : Boolean)
return Bool_Option_Info
is
((+Long_Name, +Short_Name, +Help, Commands, Internal));
------------
-- Create --
------------
function Create
(Long_Name, Short_Name, Help : String := "";
Commands : Command_Set := All_Commands;
Internal, At_Most_Once : Boolean;
Pattern : String := "")
return String_Option_Info
is
(+Long_Name, +Short_Name, +Help, Commands, Internal, At_Most_Once,
+Pattern);
------------
-- Create --
------------
function Create
(Long_Name, Short_Name, Help : String := "";
Commands : Command_Set := All_Commands;
Internal : Boolean;
Greedy : Boolean := False;
Pattern : String := "";
Accepts_Comma_Separator : Boolean := False)
return String_List_Option_Info
is
(+Long_Name, +Short_Name, +Help, Commands, Internal, Greedy, +Pattern,
Accepts_Comma_Separator);
------------
-- Create --
------------
function Create
(Command_Infos : Command_Info_Array;
Bool_Infos : Bool_Option_Info_Array;
String_Infos : String_Option_Info_Array;
String_List_Infos : String_List_Option_Info_Array;
Bool_Callback : Bool_Callback_Type := null;
String_Callback : String_Callback_Type := null;
String_List_Callback : String_List_Callback_Type := null;
Arg_Callback : Arg_Callback_Type := null)
return Parser_Type
is
Result : constant Parser_Access := new Parser_Record'
(Command_Infos,
Bool_Infos, String_Infos, String_List_Infos,
Bool_Callback, String_Callback, String_List_Callback, Arg_Callback,
Table => <>);
begin
Build_Parse_Table (Result);
return (Ada.Finalization.Limited_Controlled with Data => Result);
end Create;
--------------
-- Finalize --
--------------
overriding procedure Finalize (Self : in out Parser_Type) is
begin
if Self.Data /= null then
for Ref of Self.Data.Table.Short_Index loop
Free (Ref);
end loop;
for Ref of Self.Data.Table.Long_Index loop
Free (Ref);
end loop;
Free (Self.Data);
end if;
end Finalize;
-----------------
-- Print_Usage --
-----------------
procedure Print_Usage
(Parser : Parser_Type;
With_Internal : Boolean;
Short : Boolean;
For_Command : Command_Type := No_Command)
is
-- If we are printing the usage of an internal command, we obviously
-- want to display internal options.
With_Internal_Options : constant Boolean :=
(For_Command /= No_Command
and then Parser.Data.Command_Infos (For_Command).Internal)
or else With_Internal;
Option_Infos : constant Option_Info_Array :=
Sorted_Options (Parser.Data, With_Internal_Options, For_Command);
function Get_Pattern (Info : Option_Info'Class) return String;
-- Return a pattern to be used in the help for the option corresponding
-- to Info.
--
-- * For boolean options, return an empty string.
-- * For single string options and multiple strings ones, return its
-- provided pattern or " [...]" if the option has none.
procedure Put_Wrapped
(Text : String;
First_Line_Indent : Natural;
Next_Lines_Indent : Natural);
-- Put on standard output Text wrapped on 80 columns according to
-- First_Line_Indent and Next_Lines_Indent (see Wrap).
-----------------
-- Get_Pattern --
-----------------
function Get_Pattern (Info : Option_Info'Class) return String is
Candidate : Unbounded_String;
begin
if Info in String_Option_Info'Class then
Candidate := String_Option_Info (Info).Pattern;
elsif Info in String_List_Option_Info'Class then
Candidate := String_List_Option_Info (Info).Pattern;
else
return "";
end if;
return (if Candidate = ""
then " [...]"
else " " & (+Candidate));
end Get_Pattern;
-----------------
-- Put_Wrapped --
-----------------
procedure Put_Wrapped
(Text : String;
First_Line_Indent : Natural;
Next_Lines_Indent : Natural)
is
begin
Put (Wrap (Text, First_Line_Indent, Next_Lines_Indent, 80));
end Put_Wrapped;
First : Boolean;
begin
if For_Command = No_Command then
-- Describe commands first, or one command if asked so
Put_Line
("Usage: " & Support_Files.Gnatcov_Command_Name
& " ACTION [OPTIONS ...]");
if Short then
Put_Line
("Run '" & Support_Files.Gnatcov_Command_Name
& " --help' for more information.");
return;
end if;
New_Line;
Put_Line ("ACTION is one of:");
First := True;
for Info of Parser.Data.Command_Infos loop
if not Info.Internal or else With_Internal_Options then
if First then
New_Line;
First := False;
end if;
Put_Wrapped (+Info.Name & " " & (+Info.Pattern), 2, 6);
Put_Wrapped (+Info.Description, 4, 4);
New_Line;
end if;
end loop;
else
declare
Info : Command_Info renames
Parser.Data.Command_Infos (For_Command);
begin
Put_Wrapped ("Usage: " & Support_Files.Gnatcov_Command_Name & " "
& (+Info.Name) & " " & (+Info.Pattern), 0, 4);
if Short then
Put_Line
("Run '" & Support_Files.Gnatcov_Command_Name & " "
& (+Info.Name) & " --help' for more information.");
return;
end if;
New_Line;
Put_Wrapped (+Info.Description, 2, 2);
New_Line;
end;
end if;
-- And then all options or only the ones that apply to For_Command
Put_Line ("The following options are supported:");
First := True;
for Info of Option_Infos loop
if First then
New_Line;
First := False;
end if;
declare
Pattern : constant String := Get_Pattern (Info.all);
begin
-- Display all names for this option
for Name of Split_Option_Text (+Info.Long_Name) loop
Put_Wrapped (+Name & Pattern, 2, 6);
end loop;
for Name of Split_Option_Text (+Info.Short_Name) loop
Put_Wrapped (+Name & Pattern, 2, 6);
end loop;
-- If this option is not available for all commands, display
-- which commands accept it.
if Info.Commands /= All_Commands then
declare
Set_Image : Unbounded_String;
First : Boolean := True;
begin
Append (Set_Image, '[');
for Cmd in Valid_Commands'Range loop
if Info.Commands (Cmd)
and then
(not Parser.Data.Command_Infos (Cmd).Internal
or else
With_Internal_Options)
then
if not First then
Append (Set_Image, ", ");
end if;
Append
(Set_Image, Parser.Data.Command_Infos (Cmd).Name);
First := False;
end if;
end loop;
Append (Set_Image, ']');
Put_Wrapped (+Set_Image, 4, 5);
end;
end if;
Put_Wrapped (+Info.Help, 4, 4);
New_Line;
end;
end loop;
end Print_Usage;
-----------
-- Parse --
-----------
function Parse
(Parser : Parser_Type;
Args : String_Vectors.Vector;
With_Command : Command_Type := No_Command;
Callback : access procedure (Result : in out Parsed_Arguments;
Ref : Option_Reference) := null)
return Parsed_Arguments
is
I : Natural := Args.First_Index;
Result : Parsed_Arguments;
function Error (S : String) return Parsed_Arguments is
((Error => +S, Command => Result.Command, others => <>));
Arg_Error : exception;
-- Exception to raise in order to abort the parsing process. The
-- associated text is used to yield the error message in the result.
type Slice is record
First, Last : Natural;
end record;
-- Slice of the current argument (i.e. Args (I))
procedure Split_Arg
(Arg : String;
Option, Value : out Slice;
Has_Value : out Boolean);
-- Try to split Arg into Option and Value. For instance, if Arg is
-- "--foo=bar", Option will refer to "--foo" while Value will refer
-- to "bar". Set Has_Value to whether there is a "=" substring.
function Consume_Value
(For_Arg : Slice;
I : in out Natural;
J : Natural)
return String;
-- Considering the For_Arg slice (which is the option part of the
-- currently parsed argument Args(I)), try to fetch a value for the
-- option wthat was just parsed. Look for any value starting at Args
-- (I).all (J): if there is no value there, fetch the next argument and
-- update I accordingly. In the latter case, raise an Arg_Error if there
-- is no argument left.
function Consume_Next_Arg
(For_Arg : Slice; I : in out Natural) return Unbounded_String;
-- Fetch the next argument and return an access to it. Update I
-- accordingly. Raise an Arg_Error if there is no argument left.
function Select_Option
(Opt_Name : String;
Opt_Vec : Option_Reference_Vector)
return Option_Reference;
-- Given Opt_Name (the currently parsed option) and Opt_Vec (a set of
-- candidates for a match), return the option that is accepted by the
-- matched subcommand (Result.Command).
function Invoke_Callback (Ref : Option_Reference) return Boolean;
-- If callback is provided, call it for Ref. Return whether it yielded
-- an error (in which case, parsing must be stopped).
procedure Handle_Bool (Opt : Bool_Options);
-- Set the boolean for Opt to True in Result. Previously invoke the
-- boolean option callback if provided.
function Handle_String_List
(Opt : String_List_Options;
Str : Unbounded_String)
return Boolean;
-- Invoke the multiple strings option callback if provided. Then, if Opt
-- is greedy, consume all the arguments, store them in Result and return
-- True. Consume only one Str and return False otherwise.
---------------
-- Split_Arg --
---------------
procedure Split_Arg
(Arg : String;
Option, Value : out Slice;
Has_Value : out Boolean)
is
begin
for I in Arg'Range loop
if Arg (I) = '=' then
Option := (Arg'First, I - 1);
Value := (I + 1, Arg'Last);
Has_Value := True;
return;
end if;
end loop;
Option := (Arg'First, Arg'Last);
Value := (1, 0);
Has_Value := False;
end Split_Arg;
-------------------
-- Consume_Value --
-------------------
function Consume_Value
(For_Arg : Slice;
I : in out Natural;
J : Natural)
return String
is
Arg : constant String := +Args (I);
begin
-- The current short option requires a value:
if J > Arg'Last then
-- If this is the last option in this argument, fetch the next
-- argument.
return +Consume_Next_Arg (For_Arg, I);
else
-- Otherwise, take the rest of the current argument (stripping the
-- heading '=' if present).
return Arg ((if Arg (J) = '=' then J + 1 else J) .. Arg'Last);
end if;
end Consume_Value;
----------------------
-- Consume_Next_Arg --
----------------------
function Consume_Next_Arg
(For_Arg : Slice; I : in out Natural) return Unbounded_String is
begin
if I < Args.Last_Index then
I := I + 1;
return Args (I);
else
raise Arg_Error with
(US.Slice (Args (I), For_Arg.First, For_Arg.Last)
& " requires a value");
end if;
end Consume_Next_Arg;
-------------------
-- Select_Option --
-------------------
function Select_Option
(Opt_Name : String;
Opt_Vec : Option_Reference_Vector)
return Option_Reference
is
begin
for Ref of Opt_Vec.all loop
if Get_Option (Parser.Data, Ref).Commands (Result.Command) then
return Ref;
end if;
end loop;
raise Arg_Error with
(Opt_Name & " is not valid with the """
& Command_Name (Parser, Result.Command) & """ command.");
end Select_Option;
---------------------
-- Invoke_Callback --
---------------------
function Invoke_Callback (Ref : Option_Reference) return Boolean is
begin
if Callback /= null then
Callback (Result, Ref);
return Result.Error /= "";
end if;
return False;
end Invoke_Callback;
-----------------
-- Handle_Bool --
-----------------
procedure Handle_Bool (Opt : Bool_Options)
is
begin
if Parser.Data.Bool_Callback /= null then
Parser.Data.Bool_Callback (Result, Opt);
end if;
Result.Bool_Args (Opt) := True;
end Handle_Bool;
------------------------
-- Handle_String_List --
------------------------
function Handle_String_List
(Opt : String_List_Options;
Str : Unbounded_String)
return Boolean
is
Str_Vec : String_Vectors.Vector renames
Result.String_List_Args (Opt);
Option : String_List_Option_Info renames
Parser.Data.String_List_Info (Opt);
begin
if Parser.Data.String_List_Callback /= null then
Parser.Data.String_List_Callback (Result, Opt, +Str);
end if;
if Option.Greedy then
-- Greedy options consume all remaining arguments
for J in I .. Args.Last_Index loop
Str_Vec.Append (Args (J));
end loop;
return True;
elsif Option.Accepts_Comma_Separator then
-- Split values for comma-separated string list options
Append_From_String (Str_Vec, Str);
return False;
else
-- For regular options, just add the passed value
Str_Vec.Append (Str);
return False;
end if;
end Handle_String_List;
begin
-- Get the command from With_Command or from Args
if With_Command /= No_Command then
Result.Command := With_Command;
else
if Args.Is_Empty then
return Error ("No command specified.");
end if;
declare
Command : constant String := +Args.First_Element;
begin
for Cmd in Parser.Data.Command_Infos'Range loop
if +Parser.Data.Command_Infos (Cmd).Name = Command then
Result.Command := Cmd;
exit;
elsif Cmd = Command_Type'Last then
return Error
("Bad command: " & Command & ". Try option --help");
end if;
end loop;
end;
I := I + 1;
end if;
-- And the parse the arguments that follow
while I <= Args.Last_Index loop
declare
Arg : constant String := +Args (I);
Option, Value : Slice;
J : Natural;
Has_Value : Boolean;
Long_Cur : Long_Options_Index.Cursor;
Short_Cur : Short_Options_Index.Cursor;
Opt : Option_Reference;
begin
-- The following allow users to query help messages for a specific
-- subcommand.
if Arg = "--help" then
Print_Usage
(Parser => Parser,
With_Internal => False,
Short => False,
For_Command => Result.Command);
raise Outputs.Xcov_Exit_Exc;
elsif Arg = "--help-internal" then
Print_Usage
(Parser => Parser,
With_Internal => True,
Short => False,
For_Command => Result.Command);
raise Outputs.Xcov_Exit_Exc;
end if;
if Arg'Length >= 2 and then Arg (Arg'First) = '-' then
-- We have an option! Determine if this is a short or a long
-- option first. Since we allow long options to start with a
-- single dash ('-'), let's first match a long option.
Split_Arg (Arg, Option, Value, Has_Value);
Long_Cur := Parser.Data.Table.Long_Index.Find
(+Arg (Option.First .. Option.Last));
if Long_Options_Index.Has_Element (Long_Cur) then
-- We found a match! Interpret it, now.
Opt := Select_Option
(Arg (Option.First .. Option.Last),
Long_Options_Index.Element (Long_Cur));
if Invoke_Callback (Opt) then
return Result;
end if;
case Opt.Kind is
when Bool_Opt =>
if Has_Value then
return Error
(Option_Name (Parser, Opt)
& " does not accept a value.");
end if;
Handle_Bool (Opt.Bool_Option);
when String_Opt | String_List_Opt =>
declare
Str : constant Unbounded_String :=
(if Has_Value
then +Arg (Value.First .. Value.Last)
else Consume_Next_Arg (Option, I));
begin
if Opt.Kind = String_Opt then
if Parser.Data.String_Info
(Opt.String_Option).At_Most_Once
and then Is_Present (Result, Opt)
then
return Error
(Option_Name (Parser, Opt)
& " cannot appear multiple times.");
end if;
if Parser.Data.String_Callback /= null then
Parser.Data.String_Callback
(Result, Opt.String_Option, +Str);
end if;
Result.String_Args (Opt.String_Option) :=
(Present => True,
Value => Str);
elsif Handle_String_List
(Opt.String_List_Option, Str)
then
-- If this is a greedy option, it consumes the
-- rest of the arguments, so we only have to
-- return afterwards.
return Result;
end if;
end;
end case;
elsif Arg (Arg'First + 1) = '-' then
return Error
("Unknown option: " & Arg (Option.First .. Option.Last));
-- Past this point, this is either a short option or an unknown
-- one.
else
J := Arg'First + 1;
while J <= Arg'Last loop
Short_Cur :=
Parser.Data.Table.Short_Index.Find (Arg (J));
if not Short_Options_Index.Has_Element (Short_Cur) then
return Error
("Unknown option: -" & Arg (J) & " (from "
& Arg (Option.First .. Option.Last) & ").");
end if;
Opt := Select_Option
(Arg (Option.First .. Option.Last),
Short_Options_Index.Element (Short_Cur));
if Invoke_Callback (Opt) then
return Result;
end if;
case Opt.Kind is
when Bool_Opt =>
Handle_Bool (Opt.Bool_Option);
J := J + 1;
when String_Opt | String_List_Opt =>
-- This options requires a value:
-- * if this is the last option in this argument,
-- get the rest of this argument as a value (if
-- any) or fetch the next argument;
-- * otherwise take the rest of the current
-- argument.
declare
Str : constant Unbounded_String :=
+Consume_Value (Option, I, J + 1);
begin
if Opt.Kind = String_Opt then
if Parser.Data.String_Callback /= null then
Parser.Data.String_Callback
(Result, Opt.String_Option, +Str);
end if;
Result.String_Args (Opt.String_Option) :=
(Present => True,
Value => Str);
elsif Handle_String_List
(Opt.String_List_Option, Str)
then
-- If this is a greedy option, it consumes
-- the rest of the arguments, so we only
-- have to return afterwards.
return Result;
end if;
end;