annotate gcc/ada/libgnat/g-comlin.ads @ 131:84e7813d76e9

gcc-8.2
author mir3636
date Thu, 25 Oct 2018 07:37:49 +0900
parents 04ced10e8804
children 1830386684a0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 ------------------------------------------------------------------------------
kono
parents:
diff changeset
2 -- --
kono
parents:
diff changeset
3 -- GNAT COMPILER COMPONENTS --
kono
parents:
diff changeset
4 -- --
kono
parents:
diff changeset
5 -- G N A T . C O M M A N D _ L I N E --
kono
parents:
diff changeset
6 -- --
kono
parents:
diff changeset
7 -- S p e c --
kono
parents:
diff changeset
8 -- --
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
9 -- Copyright (C) 1999-2018, AdaCore --
111
kono
parents:
diff changeset
10 -- --
kono
parents:
diff changeset
11 -- GNAT is free software; you can redistribute it and/or modify it under --
kono
parents:
diff changeset
12 -- terms of the GNU General Public License as published by the Free Soft- --
kono
parents:
diff changeset
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
kono
parents:
diff changeset
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
kono
parents:
diff changeset
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
kono
parents:
diff changeset
16 -- or FITNESS FOR A PARTICULAR PURPOSE. --
kono
parents:
diff changeset
17 -- --
kono
parents:
diff changeset
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
kono
parents:
diff changeset
19 -- additional permissions described in the GCC Runtime Library Exception, --
kono
parents:
diff changeset
20 -- version 3.1, as published by the Free Software Foundation. --
kono
parents:
diff changeset
21 -- --
kono
parents:
diff changeset
22 -- You should have received a copy of the GNU General Public License and --
kono
parents:
diff changeset
23 -- a copy of the GCC Runtime Library Exception along with this program; --
kono
parents:
diff changeset
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
kono
parents:
diff changeset
25 -- <http://www.gnu.org/licenses/>. --
kono
parents:
diff changeset
26 -- --
kono
parents:
diff changeset
27 -- GNAT was originally developed by the GNAT team at New York University. --
kono
parents:
diff changeset
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
kono
parents:
diff changeset
29 -- --
kono
parents:
diff changeset
30 ------------------------------------------------------------------------------
kono
parents:
diff changeset
31
kono
parents:
diff changeset
32 -- High level package for command line parsing and manipulation
kono
parents:
diff changeset
33
kono
parents:
diff changeset
34 ----------------------------------------
kono
parents:
diff changeset
35 -- Simple Parsing of the Command Line --
kono
parents:
diff changeset
36 ----------------------------------------
kono
parents:
diff changeset
37
kono
parents:
diff changeset
38 -- This package provides an interface for parsing command line arguments,
kono
parents:
diff changeset
39 -- when they are either read from Ada.Command_Line or read from a string list.
kono
parents:
diff changeset
40 -- As shown in the example below, one should first retrieve the switches
kono
parents:
diff changeset
41 -- (special command line arguments starting with '-' by default) and their
kono
parents:
diff changeset
42 -- parameters, and then the rest of the command line arguments.
kono
parents:
diff changeset
43 --
kono
parents:
diff changeset
44 -- While it may appear easy to parse the command line arguments with
kono
parents:
diff changeset
45 -- Ada.Command_Line, there are in fact lots of special cases to handle in some
kono
parents:
diff changeset
46 -- applications. Those are fully managed by GNAT.Command_Line. Among these are
kono
parents:
diff changeset
47 -- switches with optional parameters, grouping switches (for instance "-ab"
kono
parents:
diff changeset
48 -- might mean the same as "-a -b"), various characters to separate a switch
kono
parents:
diff changeset
49 -- and its parameter (or none: "-a 1" and "-a1" are generally the same, which
kono
parents:
diff changeset
50 -- can introduce confusion with grouped switches),...
kono
parents:
diff changeset
51 --
kono
parents:
diff changeset
52 -- begin
kono
parents:
diff changeset
53 -- loop
kono
parents:
diff changeset
54 -- case Getopt ("a b: ad") is -- Accepts '-a', '-ad', or '-b argument'
kono
parents:
diff changeset
55 -- when ASCII.NUL => exit;
kono
parents:
diff changeset
56
kono
parents:
diff changeset
57 -- when 'a' =>
kono
parents:
diff changeset
58 -- if Full_Switch = "a" then
kono
parents:
diff changeset
59 -- Put_Line ("Got a");
kono
parents:
diff changeset
60 -- else
kono
parents:
diff changeset
61 -- Put_Line ("Got ad");
kono
parents:
diff changeset
62 -- end if;
kono
parents:
diff changeset
63
kono
parents:
diff changeset
64 -- when 'b' => Put_Line ("Got b + " & Parameter);
kono
parents:
diff changeset
65
kono
parents:
diff changeset
66 -- when others =>
kono
parents:
diff changeset
67 -- raise Program_Error; -- cannot occur
kono
parents:
diff changeset
68 -- end case;
kono
parents:
diff changeset
69 -- end loop;
kono
parents:
diff changeset
70
kono
parents:
diff changeset
71 -- loop
kono
parents:
diff changeset
72 -- declare
kono
parents:
diff changeset
73 -- S : constant String := Get_Argument (Do_Expansion => True);
kono
parents:
diff changeset
74 -- begin
kono
parents:
diff changeset
75 -- exit when S'Length = 0;
kono
parents:
diff changeset
76 -- Put_Line ("Got " & S);
kono
parents:
diff changeset
77 -- end;
kono
parents:
diff changeset
78 -- end loop;
kono
parents:
diff changeset
79
kono
parents:
diff changeset
80 -- exception
kono
parents:
diff changeset
81 -- when Invalid_Switch => Put_Line ("Invalid Switch " & Full_Switch);
kono
parents:
diff changeset
82 -- when Invalid_Parameter => Put_Line ("No parameter for " & Full_Switch);
kono
parents:
diff changeset
83 -- end;
kono
parents:
diff changeset
84
kono
parents:
diff changeset
85 --------------
kono
parents:
diff changeset
86 -- Sections --
kono
parents:
diff changeset
87 --------------
kono
parents:
diff changeset
88
kono
parents:
diff changeset
89 -- A more complicated example would involve the use of sections for the
kono
parents:
diff changeset
90 -- switches, as for instance in gnatmake. The same command line is used to
kono
parents:
diff changeset
91 -- provide switches for several tools. Each tool recognizes its switches by
kono
parents:
diff changeset
92 -- separating them with special switches that act as section separators.
kono
parents:
diff changeset
93 -- Each section acts as a command line of its own.
kono
parents:
diff changeset
94
kono
parents:
diff changeset
95 -- begin
kono
parents:
diff changeset
96 -- Initialize_Option_Scan ('-', False, "largs bargs cargs");
kono
parents:
diff changeset
97 -- loop
kono
parents:
diff changeset
98 -- -- Same loop as above to get switches and arguments
kono
parents:
diff changeset
99 -- end loop;
kono
parents:
diff changeset
100
kono
parents:
diff changeset
101 -- Goto_Section ("bargs");
kono
parents:
diff changeset
102 -- loop
kono
parents:
diff changeset
103 -- -- Same loop as above to get switches and arguments
kono
parents:
diff changeset
104 -- -- The supported switches in Getopt might be different
kono
parents:
diff changeset
105 -- end loop;
kono
parents:
diff changeset
106
kono
parents:
diff changeset
107 -- Goto_Section ("cargs");
kono
parents:
diff changeset
108 -- loop
kono
parents:
diff changeset
109 -- -- Same loop as above to get switches and arguments
kono
parents:
diff changeset
110 -- -- The supported switches in Getopt might be different
kono
parents:
diff changeset
111 -- end loop;
kono
parents:
diff changeset
112 -- end;
kono
parents:
diff changeset
113
kono
parents:
diff changeset
114 -------------------------------
kono
parents:
diff changeset
115 -- Parsing a List of Strings --
kono
parents:
diff changeset
116 -------------------------------
kono
parents:
diff changeset
117
kono
parents:
diff changeset
118 -- The examples above show how to parse the command line when the arguments
kono
parents:
diff changeset
119 -- are read directly from Ada.Command_Line. However, these arguments can also
kono
parents:
diff changeset
120 -- be read from a list of strings. This can be useful in several contexts,
kono
parents:
diff changeset
121 -- either because your system does not support Ada.Command_Line, or because
kono
parents:
diff changeset
122 -- you are manipulating other tools and creating their command lines by hand,
kono
parents:
diff changeset
123 -- or for any other reason.
kono
parents:
diff changeset
124
kono
parents:
diff changeset
125 -- To create the list of strings, it is recommended to use
kono
parents:
diff changeset
126 -- GNAT.OS_Lib.Argument_String_To_List.
kono
parents:
diff changeset
127
kono
parents:
diff changeset
128 -- The example below shows how to get the parameters from such a list. Note
kono
parents:
diff changeset
129 -- also the use of '*' to get all the switches, and not report errors when an
kono
parents:
diff changeset
130 -- unexpected switch was used by the user
kono
parents:
diff changeset
131
kono
parents:
diff changeset
132 -- declare
kono
parents:
diff changeset
133 -- Parser : Opt_Parser;
kono
parents:
diff changeset
134 -- Args : constant Argument_List_Access :=
kono
parents:
diff changeset
135 -- GNAT.OS_Lib.Argument_String_To_List ("-g -O1 -Ipath");
kono
parents:
diff changeset
136 -- begin
kono
parents:
diff changeset
137 -- Initialize_Option_Scan (Parser, Args);
kono
parents:
diff changeset
138 -- while Getopt ("* g O! I=", Parser) /= ASCII.NUL loop
kono
parents:
diff changeset
139 -- Put_Line ("Switch " & Full_Switch (Parser)
kono
parents:
diff changeset
140 -- & " param=" & Parameter (Parser));
kono
parents:
diff changeset
141 -- end loop;
kono
parents:
diff changeset
142 -- Free (Parser);
kono
parents:
diff changeset
143 -- end;
kono
parents:
diff changeset
144
kono
parents:
diff changeset
145 -------------------------------------------
kono
parents:
diff changeset
146 -- High-Level Command Line Configuration --
kono
parents:
diff changeset
147 -------------------------------------------
kono
parents:
diff changeset
148
kono
parents:
diff changeset
149 -- As shown above, the code is still relatively low-level. For instance, there
kono
parents:
diff changeset
150 -- is no way to indicate which switches are related (thus if "-l" and "--long"
kono
parents:
diff changeset
151 -- should have the same effect, your code will need to test for both cases).
kono
parents:
diff changeset
152 -- Likewise, it is difficult to handle more advanced constructs, like:
kono
parents:
diff changeset
153
kono
parents:
diff changeset
154 -- * Specifying -gnatwa is the same as specifying -gnatwu -gnatwv, but
kono
parents:
diff changeset
155 -- shorter and more readable
kono
parents:
diff changeset
156
kono
parents:
diff changeset
157 -- * All switches starting with -gnatw can be grouped, for instance one
kono
parents:
diff changeset
158 -- can write -gnatwcd instead of -gnatwc -gnatwd.
kono
parents:
diff changeset
159 -- Of course, this can be combined with the above and -gnatwacd is the
kono
parents:
diff changeset
160 -- same as -gnatwc -gnatwd -gnatwu -gnatwv
kono
parents:
diff changeset
161
kono
parents:
diff changeset
162 -- * The switch -T is the same as -gnatwAB (same as -gnatwA -gnatwB)
kono
parents:
diff changeset
163
kono
parents:
diff changeset
164 -- With the above form of Getopt, you would receive "-gnatwa", "-T" or
kono
parents:
diff changeset
165 -- "-gnatwcd" in the examples above, and thus you require additional manual
kono
parents:
diff changeset
166 -- parsing of the switch.
kono
parents:
diff changeset
167
kono
parents:
diff changeset
168 -- Instead, this package provides the type Command_Line_Configuration, which
kono
parents:
diff changeset
169 -- stores all the knowledge above. For instance:
kono
parents:
diff changeset
170
kono
parents:
diff changeset
171 -- Config : Command_Line_Configuration;
kono
parents:
diff changeset
172 -- Define_Alias (Config, "-gnatwa", "-gnatwu -gnatwv");
kono
parents:
diff changeset
173 -- Define_Prefix (Config, "-gnatw");
kono
parents:
diff changeset
174 -- Define_Alias (Config, "-T", "-gnatwAB");
kono
parents:
diff changeset
175
kono
parents:
diff changeset
176 -- You then need to specify all possible switches in your application by
kono
parents:
diff changeset
177 -- calling Define_Switch, for instance:
kono
parents:
diff changeset
178
kono
parents:
diff changeset
179 -- Define_Switch (Config, "-gnatwu", Help => "warn on unused entities");
kono
parents:
diff changeset
180 -- Define_Switch (Config, "-gnatwv", Help => "warn on unassigned var");
kono
parents:
diff changeset
181 -- ...
kono
parents:
diff changeset
182
kono
parents:
diff changeset
183 -- Specifying the help message is optional, but makes it easy to then call
kono
parents:
diff changeset
184 -- the function:
kono
parents:
diff changeset
185
kono
parents:
diff changeset
186 -- Display_Help (Config);
kono
parents:
diff changeset
187
kono
parents:
diff changeset
188 -- that will display a properly formatted help message for your application,
kono
parents:
diff changeset
189 -- listing all possible switches. That way you have a single place in which
kono
parents:
diff changeset
190 -- to maintain the list of switches and their meaning, rather than maintaining
kono
parents:
diff changeset
191 -- both the string to pass to Getopt and a subprogram to display the help.
kono
parents:
diff changeset
192 -- Both will properly stay synchronized.
kono
parents:
diff changeset
193
kono
parents:
diff changeset
194 -- Once you have this Config, you just have to call:
kono
parents:
diff changeset
195
kono
parents:
diff changeset
196 -- Getopt (Config, Callback'Access);
kono
parents:
diff changeset
197
kono
parents:
diff changeset
198 -- to parse the command line. The Callback will be called for each switch
kono
parents:
diff changeset
199 -- found on the command line (in the case of our example, that is "-gnatwu"
kono
parents:
diff changeset
200 -- and then "-gnatwv", not "-gnatwa" itself). This simplifies command line
kono
parents:
diff changeset
201 -- parsing a lot.
kono
parents:
diff changeset
202
kono
parents:
diff changeset
203 -- In fact, this can be further automated for the most command case where the
kono
parents:
diff changeset
204 -- parameter passed to a switch is stored in a variable in the application.
kono
parents:
diff changeset
205 -- When a switch is defined, you only have to indicate where to store the
kono
parents:
diff changeset
206 -- value, and let Getopt do the rest. For instance:
kono
parents:
diff changeset
207
kono
parents:
diff changeset
208 -- Optimization : aliased Integer;
kono
parents:
diff changeset
209 -- Verbose : aliased Boolean;
kono
parents:
diff changeset
210
kono
parents:
diff changeset
211 -- Define_Switch (Config, Verbose'Access,
kono
parents:
diff changeset
212 -- "-v", Long_Switch => "--verbose",
kono
parents:
diff changeset
213 -- Help => "Output extra verbose information");
kono
parents:
diff changeset
214 -- Define_Switch (Config, Optimization'Access,
kono
parents:
diff changeset
215 -- "-O?", Help => "Optimization level");
kono
parents:
diff changeset
216
kono
parents:
diff changeset
217 -- Getopt (Config); -- No callback
kono
parents:
diff changeset
218
kono
parents:
diff changeset
219 -- Since all switches are handled automatically, we don't even need to pass
kono
parents:
diff changeset
220 -- a callback to Getopt. Once getopt has been called, the two variables
kono
parents:
diff changeset
221 -- Optimization and Verbose have been properly initialized, either to the
kono
parents:
diff changeset
222 -- default value or to the value found on the command line.
kono
parents:
diff changeset
223
kono
parents:
diff changeset
224 ------------------------------------------------
kono
parents:
diff changeset
225 -- Creating and Manipulating the Command Line --
kono
parents:
diff changeset
226 ------------------------------------------------
kono
parents:
diff changeset
227
kono
parents:
diff changeset
228 -- This package provides mechanisms to create and modify command lines by
kono
parents:
diff changeset
229 -- adding or removing arguments from them. The resulting command line is kept
kono
parents:
diff changeset
230 -- as short as possible by coalescing arguments whenever possible.
kono
parents:
diff changeset
231
kono
parents:
diff changeset
232 -- Complex command lines can thus be constructed, for example from a GUI
kono
parents:
diff changeset
233 -- (although this package does not by itself depend upon any specific GUI
kono
parents:
diff changeset
234 -- toolkit).
kono
parents:
diff changeset
235
kono
parents:
diff changeset
236 -- Using the configuration defined earlier, one can then construct a command
kono
parents:
diff changeset
237 -- line for the tool with:
kono
parents:
diff changeset
238
kono
parents:
diff changeset
239 -- Cmd : Command_Line;
kono
parents:
diff changeset
240 -- Set_Configuration (Cmd, Config); -- Config created earlier
kono
parents:
diff changeset
241 -- Add_Switch (Cmd, "-bar");
kono
parents:
diff changeset
242 -- Add_Switch (Cmd, "-gnatwu");
kono
parents:
diff changeset
243 -- Add_Switch (Cmd, "-gnatwv"); -- will be grouped with the above
kono
parents:
diff changeset
244 -- Add_Switch (Cmd, "-T");
kono
parents:
diff changeset
245
kono
parents:
diff changeset
246 -- The resulting command line can be iterated over to get all its switches,
kono
parents:
diff changeset
247 -- There are two modes for this iteration: either you want to get the
kono
parents:
diff changeset
248 -- shortest possible command line, which would be:
kono
parents:
diff changeset
249
kono
parents:
diff changeset
250 -- -bar -gnatwaAB
kono
parents:
diff changeset
251
kono
parents:
diff changeset
252 -- or on the other hand you want each individual switch (so that your own
kono
parents:
diff changeset
253 -- tool does not have to do further complex processing), which would be:
kono
parents:
diff changeset
254
kono
parents:
diff changeset
255 -- -bar -gnatwu -gnatwv -gnatwA -gnatwB
kono
parents:
diff changeset
256
kono
parents:
diff changeset
257 -- Of course, we can assume that the tool you want to spawn would understand
kono
parents:
diff changeset
258 -- both of these, since they are both compatible with the description we gave
kono
parents:
diff changeset
259 -- above. However, the first result is useful if you want to show the user
kono
parents:
diff changeset
260 -- what you are spawning (since that keeps the output shorter), and the second
kono
parents:
diff changeset
261 -- output is more useful for a tool that would check whether -gnatwu was
kono
parents:
diff changeset
262 -- passed (which isn't obvious in the first output). Likewise, the second
kono
parents:
diff changeset
263 -- output is more useful if you have a graphical interface since each switch
kono
parents:
diff changeset
264 -- can be associated with a widget, and you immediately know whether -gnatwu
kono
parents:
diff changeset
265 -- was selected.
kono
parents:
diff changeset
266 --
kono
parents:
diff changeset
267 -- Some command line arguments can have parameters, which on a command line
kono
parents:
diff changeset
268 -- appear as a separate argument that must immediately follow the switch.
kono
parents:
diff changeset
269 -- Since the subprograms in this package will reorganize the switches to group
kono
parents:
diff changeset
270 -- them, you need to indicate what is a command line parameter, and what is a
kono
parents:
diff changeset
271 -- switch argument.
kono
parents:
diff changeset
272
kono
parents:
diff changeset
273 -- This is done by passing an extra argument to Add_Switch, as in:
kono
parents:
diff changeset
274
kono
parents:
diff changeset
275 -- Add_Switch (Cmd, "-foo", Parameter => "arg1");
kono
parents:
diff changeset
276
kono
parents:
diff changeset
277 -- This ensures that "arg1" will always be treated as the argument to -foo,
kono
parents:
diff changeset
278 -- and will not be grouped with other parts of the command line.
kono
parents:
diff changeset
279
kono
parents:
diff changeset
280 with Ada.Command_Line;
kono
parents:
diff changeset
281
kono
parents:
diff changeset
282 with GNAT.Directory_Operations;
kono
parents:
diff changeset
283 with GNAT.OS_Lib;
kono
parents:
diff changeset
284 with GNAT.Regexp;
kono
parents:
diff changeset
285 with GNAT.Strings;
kono
parents:
diff changeset
286
kono
parents:
diff changeset
287 package GNAT.Command_Line is
kono
parents:
diff changeset
288
kono
parents:
diff changeset
289 -------------
kono
parents:
diff changeset
290 -- Parsing --
kono
parents:
diff changeset
291 -------------
kono
parents:
diff changeset
292
kono
parents:
diff changeset
293 type Opt_Parser is private;
kono
parents:
diff changeset
294 Command_Line_Parser : constant Opt_Parser;
kono
parents:
diff changeset
295 -- This object is responsible for parsing a list of arguments, which by
kono
parents:
diff changeset
296 -- default are the standard command line arguments from Ada.Command_Line.
kono
parents:
diff changeset
297 -- This is really a pointer to actual data, which must therefore be
kono
parents:
diff changeset
298 -- initialized through a call to Initialize_Option_Scan, and must be freed
kono
parents:
diff changeset
299 -- with a call to Free.
kono
parents:
diff changeset
300 --
kono
parents:
diff changeset
301 -- As a special case, Command_Line_Parser does not need to be either
kono
parents:
diff changeset
302 -- initialized or free-ed.
kono
parents:
diff changeset
303
kono
parents:
diff changeset
304 procedure Initialize_Option_Scan
kono
parents:
diff changeset
305 (Switch_Char : Character := '-';
kono
parents:
diff changeset
306 Stop_At_First_Non_Switch : Boolean := False;
kono
parents:
diff changeset
307 Section_Delimiters : String := "");
kono
parents:
diff changeset
308 procedure Initialize_Option_Scan
kono
parents:
diff changeset
309 (Parser : out Opt_Parser;
kono
parents:
diff changeset
310 Command_Line : GNAT.OS_Lib.Argument_List_Access;
kono
parents:
diff changeset
311 Switch_Char : Character := '-';
kono
parents:
diff changeset
312 Stop_At_First_Non_Switch : Boolean := False;
kono
parents:
diff changeset
313 Section_Delimiters : String := "");
kono
parents:
diff changeset
314 -- The first procedure resets the internal state of the package to prepare
kono
parents:
diff changeset
315 -- to rescan the parameters. It does not need to be called before the
kono
parents:
diff changeset
316 -- first use of Getopt (but it could be), but it must be called if you
kono
parents:
diff changeset
317 -- want to start rescanning the command line parameters from the start.
kono
parents:
diff changeset
318 -- The optional parameter Switch_Char can be used to reset the switch
kono
parents:
diff changeset
319 -- character, e.g. to '/' for use in DOS-like systems.
kono
parents:
diff changeset
320 --
kono
parents:
diff changeset
321 -- The second subprogram initializes a parser that takes its arguments
kono
parents:
diff changeset
322 -- from an array of strings rather than directly from the command line. In
kono
parents:
diff changeset
323 -- this case, the parser is responsible for freeing the strings stored in
kono
parents:
diff changeset
324 -- Command_Line. If you pass null to Command_Line, this will in fact create
kono
parents:
diff changeset
325 -- a second parser for Ada.Command_Line, which doesn't share any data with
kono
parents:
diff changeset
326 -- the default parser. This parser must be free'ed.
kono
parents:
diff changeset
327 --
kono
parents:
diff changeset
328 -- The optional parameter Stop_At_First_Non_Switch indicates if Getopt is
kono
parents:
diff changeset
329 -- to look for switches on the whole command line, or if it has to stop as
kono
parents:
diff changeset
330 -- soon as a non-switch argument is found.
kono
parents:
diff changeset
331 --
kono
parents:
diff changeset
332 -- Example:
kono
parents:
diff changeset
333 --
kono
parents:
diff changeset
334 -- Arguments: my_application file1 -c
kono
parents:
diff changeset
335 --
kono
parents:
diff changeset
336 -- If Stop_At_First_Non_Switch is False, then -c will be considered
kono
parents:
diff changeset
337 -- as a switch (returned by getopt), otherwise it will be considered
kono
parents:
diff changeset
338 -- as a normal argument (returned by Get_Argument).
kono
parents:
diff changeset
339 --
kono
parents:
diff changeset
340 -- If Section_Delimiters is set, then every following subprogram
kono
parents:
diff changeset
341 -- (Getopt and Get_Argument) will only operate within a section, which
kono
parents:
diff changeset
342 -- is delimited by any of these delimiters or the end of the command line.
kono
parents:
diff changeset
343 --
kono
parents:
diff changeset
344 -- Example:
kono
parents:
diff changeset
345 -- Initialize_Option_Scan (Section_Delimiters => "largs bargs cargs");
kono
parents:
diff changeset
346 --
kono
parents:
diff changeset
347 -- Arguments on command line : my_application -c -bargs -d -e -largs -f
kono
parents:
diff changeset
348 -- This line contains three sections, the first one is the default one
kono
parents:
diff changeset
349 -- and includes only the '-c' switch, the second one is between -bargs
kono
parents:
diff changeset
350 -- and -largs and includes '-d -e' and the last one includes '-f'.
kono
parents:
diff changeset
351
kono
parents:
diff changeset
352 procedure Free (Parser : in out Opt_Parser);
kono
parents:
diff changeset
353 -- Free the memory used by the parser. Calling this is not mandatory for
kono
parents:
diff changeset
354 -- the Command_Line_Parser
kono
parents:
diff changeset
355
kono
parents:
diff changeset
356 procedure Goto_Section
kono
parents:
diff changeset
357 (Name : String := "";
kono
parents:
diff changeset
358 Parser : Opt_Parser := Command_Line_Parser);
kono
parents:
diff changeset
359 -- Change the current section. The next Getopt or Get_Argument will start
kono
parents:
diff changeset
360 -- looking at the beginning of the section. An empty name ("") refers to
kono
parents:
diff changeset
361 -- the first section between the program name and the first section
kono
parents:
diff changeset
362 -- delimiter. If the section does not exist in Section_Delimiters, then
kono
parents:
diff changeset
363 -- Invalid_Section is raised. If the section does not appear on the command
kono
parents:
diff changeset
364 -- line, then it is treated as an empty section.
kono
parents:
diff changeset
365
kono
parents:
diff changeset
366 function Full_Switch
kono
parents:
diff changeset
367 (Parser : Opt_Parser := Command_Line_Parser) return String;
kono
parents:
diff changeset
368 -- Returns the full name of the last switch found (Getopt only returns the
kono
parents:
diff changeset
369 -- first character). Does not include the Switch_Char ('-' by default),
kono
parents:
diff changeset
370 -- unless the "*" option of Getopt is used (see below).
kono
parents:
diff changeset
371
kono
parents:
diff changeset
372 function Current_Section
kono
parents:
diff changeset
373 (Parser : Opt_Parser := Command_Line_Parser) return String;
kono
parents:
diff changeset
374 -- Return the name of the current section.
kono
parents:
diff changeset
375 -- The list of valid sections is defined through Initialize_Option_Scan
kono
parents:
diff changeset
376
kono
parents:
diff changeset
377 function Getopt
kono
parents:
diff changeset
378 (Switches : String;
kono
parents:
diff changeset
379 Concatenate : Boolean := True;
kono
parents:
diff changeset
380 Parser : Opt_Parser := Command_Line_Parser) return Character;
kono
parents:
diff changeset
381 -- This function moves to the next switch on the command line (defined as
kono
parents:
diff changeset
382 -- switch character followed by a character within Switches, casing being
kono
parents:
diff changeset
383 -- significant). The result returned is the first character of the switch
kono
parents:
diff changeset
384 -- that is located. If there are no more switches in the current section,
kono
parents:
diff changeset
385 -- returns ASCII.NUL. If Concatenate is True (the default), the switches do
kono
parents:
diff changeset
386 -- not need to be separated by spaces (they can be concatenated if they do
kono
parents:
diff changeset
387 -- not require an argument, e.g. -ab is the same as two separate arguments
kono
parents:
diff changeset
388 -- -a -b).
kono
parents:
diff changeset
389 --
kono
parents:
diff changeset
390 -- Switches is a string of all the possible switches, separated by
kono
parents:
diff changeset
391 -- spaces. A switch can be followed by one of the following characters:
kono
parents:
diff changeset
392 --
kono
parents:
diff changeset
393 -- ':' The switch requires a parameter. There can optionally be a space
kono
parents:
diff changeset
394 -- on the command line between the switch and its parameter.
kono
parents:
diff changeset
395 --
kono
parents:
diff changeset
396 -- '=' The switch requires a parameter. There can either be a '=' or a
kono
parents:
diff changeset
397 -- space on the command line between the switch and its parameter.
kono
parents:
diff changeset
398 --
kono
parents:
diff changeset
399 -- '!' The switch requires a parameter, but there can be no space on the
kono
parents:
diff changeset
400 -- command line between the switch and its parameter.
kono
parents:
diff changeset
401 --
kono
parents:
diff changeset
402 -- '?' The switch may have an optional parameter. There can be no space
kono
parents:
diff changeset
403 -- between the switch and its argument.
kono
parents:
diff changeset
404 --
kono
parents:
diff changeset
405 -- e.g. if Switches has the following value : "a? b",
kono
parents:
diff changeset
406 -- The command line can be:
kono
parents:
diff changeset
407 --
kono
parents:
diff changeset
408 -- -afoo : -a switch with 'foo' parameter
kono
parents:
diff changeset
409 -- -a foo : -a switch and another element on the
kono
parents:
diff changeset
410 -- command line 'foo', returned by Get_Argument
kono
parents:
diff changeset
411 --
kono
parents:
diff changeset
412 -- Example: if Switches is "-a: -aO:", you can have the following
kono
parents:
diff changeset
413 -- command lines:
kono
parents:
diff changeset
414 --
kono
parents:
diff changeset
415 -- -aarg : 'a' switch with 'arg' parameter
kono
parents:
diff changeset
416 -- -a arg : 'a' switch with 'arg' parameter
kono
parents:
diff changeset
417 -- -aOarg : 'aO' switch with 'arg' parameter
kono
parents:
diff changeset
418 -- -aO arg : 'aO' switch with 'arg' parameter
kono
parents:
diff changeset
419 --
kono
parents:
diff changeset
420 -- Example:
kono
parents:
diff changeset
421 --
kono
parents:
diff changeset
422 -- Getopt ("a b: ac ad?")
kono
parents:
diff changeset
423 --
kono
parents:
diff changeset
424 -- accept either 'a' or 'ac' with no argument,
kono
parents:
diff changeset
425 -- accept 'b' with a required argument
kono
parents:
diff changeset
426 -- accept 'ad' with an optional argument
kono
parents:
diff changeset
427 --
kono
parents:
diff changeset
428 -- If the first item in switches is '*', then Getopt will catch
kono
parents:
diff changeset
429 -- every element on the command line that was not caught by any other
kono
parents:
diff changeset
430 -- switch. The character returned by GetOpt is '*', but Full_Switch
kono
parents:
diff changeset
431 -- contains the full command line argument, including leading '-' if there
kono
parents:
diff changeset
432 -- is one. If this character was not returned, there would be no way of
kono
parents:
diff changeset
433 -- knowing whether it is there or not.
kono
parents:
diff changeset
434 --
kono
parents:
diff changeset
435 -- Example
kono
parents:
diff changeset
436 -- Getopt ("* a b")
kono
parents:
diff changeset
437 -- If the command line is '-a -c toto.o -b', Getopt will return
kono
parents:
diff changeset
438 -- successively 'a', '*', '*' and 'b', with Full_Switch returning
kono
parents:
diff changeset
439 -- "a", "-c", "toto.o", and "b".
kono
parents:
diff changeset
440 --
kono
parents:
diff changeset
441 -- When Getopt encounters an invalid switch, it raises the exception
kono
parents:
diff changeset
442 -- Invalid_Switch and sets Full_Switch to return the invalid switch.
kono
parents:
diff changeset
443 -- When Getopt cannot find the parameter associated with a switch, it
kono
parents:
diff changeset
444 -- raises Invalid_Parameter, and sets Full_Switch to return the invalid
kono
parents:
diff changeset
445 -- switch.
kono
parents:
diff changeset
446 --
kono
parents:
diff changeset
447 -- Note: in case of ambiguity, e.g. switches a ab abc, then the longest
kono
parents:
diff changeset
448 -- matching switch is returned.
kono
parents:
diff changeset
449 --
kono
parents:
diff changeset
450 -- Arbitrary characters are allowed for switches, although it is
kono
parents:
diff changeset
451 -- strongly recommended to use only letters and digits for portability
kono
parents:
diff changeset
452 -- reasons.
kono
parents:
diff changeset
453 --
kono
parents:
diff changeset
454 -- When Concatenate is False, individual switches need to be separated by
kono
parents:
diff changeset
455 -- spaces.
kono
parents:
diff changeset
456 --
kono
parents:
diff changeset
457 -- Example
kono
parents:
diff changeset
458 -- Getopt ("a b", Concatenate => False)
kono
parents:
diff changeset
459 -- If the command line is '-ab', exception Invalid_Switch will be
kono
parents:
diff changeset
460 -- raised and Full_Switch will return "ab".
kono
parents:
diff changeset
461
kono
parents:
diff changeset
462 function Get_Argument
kono
parents:
diff changeset
463 (Do_Expansion : Boolean := False;
kono
parents:
diff changeset
464 Parser : Opt_Parser := Command_Line_Parser) return String;
kono
parents:
diff changeset
465 -- Returns the next element on the command line that is not a switch. This
kono
parents:
diff changeset
466 -- function should not be called before Getopt has returned ASCII.NUL.
kono
parents:
diff changeset
467 --
kono
parents:
diff changeset
468 -- If Do_Expansion is True, then the parameter on the command line will
kono
parents:
diff changeset
469 -- be considered as a filename with wild cards, and will be expanded. The
kono
parents:
diff changeset
470 -- matching file names will be returned one at a time. This is useful in
kono
parents:
diff changeset
471 -- non-Unix systems for obtaining normal expansion of wild card references.
kono
parents:
diff changeset
472 -- When there are no more arguments on the command line, this function
kono
parents:
diff changeset
473 -- returns an empty string.
kono
parents:
diff changeset
474
kono
parents:
diff changeset
475 function Parameter
kono
parents:
diff changeset
476 (Parser : Opt_Parser := Command_Line_Parser) return String;
kono
parents:
diff changeset
477 -- Returns parameter associated with the last switch returned by Getopt.
kono
parents:
diff changeset
478 -- If no parameter was associated with the last switch, or no previous call
kono
parents:
diff changeset
479 -- has been made to Get_Argument, raises Invalid_Parameter. If the last
kono
parents:
diff changeset
480 -- switch was associated with an optional argument and this argument was
kono
parents:
diff changeset
481 -- not found on the command line, Parameter returns an empty string.
kono
parents:
diff changeset
482
kono
parents:
diff changeset
483 function Separator
kono
parents:
diff changeset
484 (Parser : Opt_Parser := Command_Line_Parser) return Character;
kono
parents:
diff changeset
485 -- The separator that was between the switch and its parameter. This is
kono
parents:
diff changeset
486 -- useful if you want to know exactly what was on the command line. This
kono
parents:
diff changeset
487 -- is in general a single character, set to ASCII.NUL if the switch and
kono
parents:
diff changeset
488 -- the parameter were concatenated. A space is returned if the switch and
kono
parents:
diff changeset
489 -- its argument were in two separate arguments.
kono
parents:
diff changeset
490
kono
parents:
diff changeset
491 Invalid_Section : exception;
kono
parents:
diff changeset
492 -- Raised when an invalid section is selected by Goto_Section
kono
parents:
diff changeset
493
kono
parents:
diff changeset
494 Invalid_Switch : exception;
kono
parents:
diff changeset
495 -- Raised when an invalid switch is detected in the command line
kono
parents:
diff changeset
496
kono
parents:
diff changeset
497 Invalid_Parameter : exception;
kono
parents:
diff changeset
498 -- Raised when a parameter is missing, or an attempt is made to obtain a
kono
parents:
diff changeset
499 -- parameter for a switch that does not allow a parameter.
kono
parents:
diff changeset
500
kono
parents:
diff changeset
501 -----------------------------------------
kono
parents:
diff changeset
502 -- Expansion of command line arguments --
kono
parents:
diff changeset
503 -----------------------------------------
kono
parents:
diff changeset
504
kono
parents:
diff changeset
505 -- These subprograms take care of expanding globbing patterns on the
kono
parents:
diff changeset
506 -- command line. On Unix, such expansion is done by the shell before your
kono
parents:
diff changeset
507 -- application is called. But on Windows you must do this expansion
kono
parents:
diff changeset
508 -- yourself.
kono
parents:
diff changeset
509
kono
parents:
diff changeset
510 type Expansion_Iterator is limited private;
kono
parents:
diff changeset
511 -- Type used during expansion of file names
kono
parents:
diff changeset
512
kono
parents:
diff changeset
513 procedure Start_Expansion
kono
parents:
diff changeset
514 (Iterator : out Expansion_Iterator;
kono
parents:
diff changeset
515 Pattern : String;
kono
parents:
diff changeset
516 Directory : String := "";
kono
parents:
diff changeset
517 Basic_Regexp : Boolean := True);
kono
parents:
diff changeset
518 -- Initialize a wild card expansion. The next calls to Expansion will
kono
parents:
diff changeset
519 -- return the next file name in Directory which match Pattern (Pattern
kono
parents:
diff changeset
520 -- is a regular expression, using only the Unix shell and DOS syntax if
kono
parents:
diff changeset
521 -- Basic_Regexp is True). When Directory is an empty string, the current
kono
parents:
diff changeset
522 -- directory is searched.
kono
parents:
diff changeset
523 --
kono
parents:
diff changeset
524 -- Pattern may contain directory separators (as in "src/*/*.ada").
kono
parents:
diff changeset
525 -- Subdirectories of Directory will also be searched, up to one
kono
parents:
diff changeset
526 -- hundred levels deep.
kono
parents:
diff changeset
527 --
kono
parents:
diff changeset
528 -- When Start_Expansion has been called, function Expansion should
kono
parents:
diff changeset
529 -- be called repeatedly until it returns an empty string, before
kono
parents:
diff changeset
530 -- Start_Expansion can be called again with the same Expansion_Iterator
kono
parents:
diff changeset
531 -- variable.
kono
parents:
diff changeset
532
kono
parents:
diff changeset
533 function Expansion (Iterator : Expansion_Iterator) return String;
kono
parents:
diff changeset
534 -- Returns the next file in the directory matching the parameters given
kono
parents:
diff changeset
535 -- to Start_Expansion and updates Iterator to point to the next entry.
kono
parents:
diff changeset
536 -- Returns an empty string when there are no more files.
kono
parents:
diff changeset
537 --
kono
parents:
diff changeset
538 -- If Expansion is called again after an empty string has been returned,
kono
parents:
diff changeset
539 -- then the exception GNAT.Directory_Operations.Directory_Error is raised.
kono
parents:
diff changeset
540
kono
parents:
diff changeset
541 -----------------
kono
parents:
diff changeset
542 -- Configuring --
kono
parents:
diff changeset
543 -----------------
kono
parents:
diff changeset
544
kono
parents:
diff changeset
545 -- The following subprograms are used to manipulate a command line
kono
parents:
diff changeset
546 -- represented as a string (for instance "-g -O2"), as well as parsing
kono
parents:
diff changeset
547 -- the switches from such a string. They provide high-level configurations
kono
parents:
diff changeset
548 -- to define aliases (a switch is equivalent to one or more other switches)
kono
parents:
diff changeset
549 -- or grouping of switches ("-gnatyac" is equivalent to "-gnatya" and
kono
parents:
diff changeset
550 -- "-gnatyc").
kono
parents:
diff changeset
551
kono
parents:
diff changeset
552 -- See the top of this file for examples on how to use these subprograms
kono
parents:
diff changeset
553
kono
parents:
diff changeset
554 type Command_Line_Configuration is private;
kono
parents:
diff changeset
555
kono
parents:
diff changeset
556 procedure Define_Section
kono
parents:
diff changeset
557 (Config : in out Command_Line_Configuration;
kono
parents:
diff changeset
558 Section : String);
kono
parents:
diff changeset
559 -- Indicates a new switch section. All switches belonging to the same
kono
parents:
diff changeset
560 -- section are ordered together, preceded by the section. They are placed
kono
parents:
diff changeset
561 -- at the end of the command line (as in "gnatmake somefile.adb -cargs -g")
kono
parents:
diff changeset
562 --
kono
parents:
diff changeset
563 -- The section name should not include the leading '-'. So for instance in
kono
parents:
diff changeset
564 -- the case of gnatmake we would use:
kono
parents:
diff changeset
565 --
kono
parents:
diff changeset
566 -- Define_Section (Config, "cargs");
kono
parents:
diff changeset
567 -- Define_Section (Config, "bargs");
kono
parents:
diff changeset
568
kono
parents:
diff changeset
569 procedure Define_Alias
kono
parents:
diff changeset
570 (Config : in out Command_Line_Configuration;
kono
parents:
diff changeset
571 Switch : String;
kono
parents:
diff changeset
572 Expanded : String;
kono
parents:
diff changeset
573 Section : String := "");
kono
parents:
diff changeset
574 -- Indicates that whenever Switch appears on the command line, it should
kono
parents:
diff changeset
575 -- be expanded as Expanded. For instance, for the GNAT compiler switches,
kono
parents:
diff changeset
576 -- we would define "-gnatwa" as an alias for "-gnatwcfijkmopruvz", ie some
kono
parents:
diff changeset
577 -- default warnings to be activated.
kono
parents:
diff changeset
578 --
kono
parents:
diff changeset
579 -- This expansion is only done within the specified section, which must
kono
parents:
diff changeset
580 -- have been defined first through a call to [Define_Section].
kono
parents:
diff changeset
581
kono
parents:
diff changeset
582 procedure Define_Prefix
kono
parents:
diff changeset
583 (Config : in out Command_Line_Configuration;
kono
parents:
diff changeset
584 Prefix : String);
kono
parents:
diff changeset
585 -- Indicates that all switches starting with the given prefix should be
kono
parents:
diff changeset
586 -- grouped. For instance, for the GNAT compiler we would define "-gnatw" as
kono
parents:
diff changeset
587 -- a prefix, so that "-gnatwu -gnatwv" can be grouped into "-gnatwuv" It is
kono
parents:
diff changeset
588 -- assumed that the remainder of the switch ("uv") is a set of characters
kono
parents:
diff changeset
589 -- whose order is irrelevant. In fact, this package will sort them
kono
parents:
diff changeset
590 -- alphabetically.
kono
parents:
diff changeset
591 --
kono
parents:
diff changeset
592 -- When grouping switches that accept arguments (for instance "-gnatyL!"
kono
parents:
diff changeset
593 -- as the definition, and "-gnatyaL12b" as the command line), only
kono
parents:
diff changeset
594 -- numerical arguments are accepted. The above is equivalent to
kono
parents:
diff changeset
595 -- "-gnatya -gnatyL12 -gnatyb".
kono
parents:
diff changeset
596
kono
parents:
diff changeset
597 procedure Define_Switch
kono
parents:
diff changeset
598 (Config : in out Command_Line_Configuration;
kono
parents:
diff changeset
599 Switch : String := "";
kono
parents:
diff changeset
600 Long_Switch : String := "";
kono
parents:
diff changeset
601 Help : String := "";
kono
parents:
diff changeset
602 Section : String := "";
kono
parents:
diff changeset
603 Argument : String := "ARG");
kono
parents:
diff changeset
604 -- Indicates a new switch. The format of this switch follows the getopt
kono
parents:
diff changeset
605 -- format (trailing ':', '?', etc for defining a switch with parameters).
kono
parents:
diff changeset
606 --
kono
parents:
diff changeset
607 -- Switch should also start with the leading '-' (or any other characters).
kono
parents:
diff changeset
608 -- If this character is not '-', you need to call Initialize_Option_Scan to
kono
parents:
diff changeset
609 -- set the proper character for the parser.
kono
parents:
diff changeset
610 --
kono
parents:
diff changeset
611 -- The switches defined in the command_line_configuration object are used
kono
parents:
diff changeset
612 -- when ungrouping switches with more that one character after the prefix.
kono
parents:
diff changeset
613 --
kono
parents:
diff changeset
614 -- Switch and Long_Switch (when specified) are aliases and can be used
kono
parents:
diff changeset
615 -- interchangeably. There is no check that they both take an argument or
kono
parents:
diff changeset
616 -- both take no argument. Switch can be set to "*" to indicate that any
kono
parents:
diff changeset
617 -- switch is supported (in which case Getopt will return '*', see its
kono
parents:
diff changeset
618 -- documentation).
kono
parents:
diff changeset
619 --
kono
parents:
diff changeset
620 -- Help is used by the Display_Help procedure to describe the supported
kono
parents:
diff changeset
621 -- switches.
kono
parents:
diff changeset
622 --
kono
parents:
diff changeset
623 -- In_Section indicates in which section the switch is valid (you need to
kono
parents:
diff changeset
624 -- first define the section through a call to Define_Section).
kono
parents:
diff changeset
625 --
kono
parents:
diff changeset
626 -- Argument is the name of the argument, as displayed in the automatic
kono
parents:
diff changeset
627 -- help message. It is always capitalized for consistency.
kono
parents:
diff changeset
628
kono
parents:
diff changeset
629 procedure Define_Switch
kono
parents:
diff changeset
630 (Config : in out Command_Line_Configuration;
kono
parents:
diff changeset
631 Output : access Boolean;
kono
parents:
diff changeset
632 Switch : String := "";
kono
parents:
diff changeset
633 Long_Switch : String := "";
kono
parents:
diff changeset
634 Help : String := "";
kono
parents:
diff changeset
635 Section : String := "";
kono
parents:
diff changeset
636 Value : Boolean := True);
kono
parents:
diff changeset
637 -- See Define_Switch for a description of the parameters.
kono
parents:
diff changeset
638 -- When the switch is found on the command line, Getopt will set
kono
parents:
diff changeset
639 -- Output.all to Value.
kono
parents:
diff changeset
640 --
kono
parents:
diff changeset
641 -- Output is always initially set to "not Value", so that if the switch is
kono
parents:
diff changeset
642 -- not found on the command line, Output still has a valid value.
kono
parents:
diff changeset
643 -- The switch must not take any parameter.
kono
parents:
diff changeset
644 --
kono
parents:
diff changeset
645 -- Output must exist at least as long as Config, otherwise an erroneous
kono
parents:
diff changeset
646 -- memory access may occur.
kono
parents:
diff changeset
647
kono
parents:
diff changeset
648 procedure Define_Switch
kono
parents:
diff changeset
649 (Config : in out Command_Line_Configuration;
kono
parents:
diff changeset
650 Output : access Integer;
kono
parents:
diff changeset
651 Switch : String := "";
kono
parents:
diff changeset
652 Long_Switch : String := "";
kono
parents:
diff changeset
653 Help : String := "";
kono
parents:
diff changeset
654 Section : String := "";
kono
parents:
diff changeset
655 Initial : Integer := 0;
kono
parents:
diff changeset
656 Default : Integer := 1;
kono
parents:
diff changeset
657 Argument : String := "ARG");
kono
parents:
diff changeset
658 -- See Define_Switch for a description of the parameters. When the
kono
parents:
diff changeset
659 -- switch is found on the command line, Getopt will set Output.all to the
kono
parents:
diff changeset
660 -- value of the switch's parameter. If the parameter is not an integer,
kono
parents:
diff changeset
661 -- Invalid_Parameter is raised.
kono
parents:
diff changeset
662
kono
parents:
diff changeset
663 -- Output is always initialized to Initial. If the switch has an optional
kono
parents:
diff changeset
664 -- argument which isn't specified by the user, then Output will be set to
kono
parents:
diff changeset
665 -- Default. The switch must accept an argument.
kono
parents:
diff changeset
666
kono
parents:
diff changeset
667 procedure Define_Switch
kono
parents:
diff changeset
668 (Config : in out Command_Line_Configuration;
kono
parents:
diff changeset
669 Output : access GNAT.Strings.String_Access;
kono
parents:
diff changeset
670 Switch : String := "";
kono
parents:
diff changeset
671 Long_Switch : String := "";
kono
parents:
diff changeset
672 Help : String := "";
kono
parents:
diff changeset
673 Section : String := "";
kono
parents:
diff changeset
674 Argument : String := "ARG");
kono
parents:
diff changeset
675 -- Set Output to the value of the switch's parameter when the switch is
kono
parents:
diff changeset
676 -- found on the command line. Output is always initialized to the empty
kono
parents:
diff changeset
677 -- string if it does not have a value already (otherwise it is left as is
kono
parents:
diff changeset
678 -- so that you can specify the default value directly in the declaration
kono
parents:
diff changeset
679 -- of the variable). The switch must accept an argument.
kono
parents:
diff changeset
680
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
681 type Value_Callback is access procedure (Switch, Value : String);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
682
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
683 procedure Define_Switch
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
684 (Config : in out Command_Line_Configuration;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
685 Callback : not null Value_Callback;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
686 Switch : String := "";
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
687 Long_Switch : String := "";
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
688 Help : String := "";
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
689 Section : String := "";
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
690 Argument : String := "ARG");
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
691 -- Call Callback for each instance of Switch. The callback is given the
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
692 -- actual switch and the corresponding value. The switch must accept
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
693 -- an argument.
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
694
111
kono
parents:
diff changeset
695 procedure Set_Usage
kono
parents:
diff changeset
696 (Config : in out Command_Line_Configuration;
kono
parents:
diff changeset
697 Usage : String := "[switches] [arguments]";
kono
parents:
diff changeset
698 Help : String := "";
kono
parents:
diff changeset
699 Help_Msg : String := "");
kono
parents:
diff changeset
700 -- Defines the general format of the call to the application, and a short
kono
parents:
diff changeset
701 -- help text. These are both displayed by Display_Help. When a non-empty
kono
parents:
diff changeset
702 -- Help_Msg is given, it is used by Display_Help instead of the
kono
parents:
diff changeset
703 -- automatically generated list of supported switches.
kono
parents:
diff changeset
704
kono
parents:
diff changeset
705 procedure Display_Help (Config : Command_Line_Configuration);
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
706 -- Display the help for the tool (i.e. its usage, and its supported
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
707 -- switches).
111
kono
parents:
diff changeset
708
kono
parents:
diff changeset
709 function Get_Switches
kono
parents:
diff changeset
710 (Config : Command_Line_Configuration;
kono
parents:
diff changeset
711 Switch_Char : Character := '-';
kono
parents:
diff changeset
712 Section : String := "") return String;
kono
parents:
diff changeset
713 -- Get the switches list as expected by Getopt, for a specific section of
kono
parents:
diff changeset
714 -- the command line. This list is built using all switches defined
kono
parents:
diff changeset
715 -- previously via Define_Switch above.
kono
parents:
diff changeset
716
kono
parents:
diff changeset
717 function Section_Delimiters
kono
parents:
diff changeset
718 (Config : Command_Line_Configuration) return String;
kono
parents:
diff changeset
719 -- Return a string suitable for use in Initialize_Option_Scan
kono
parents:
diff changeset
720
kono
parents:
diff changeset
721 procedure Free (Config : in out Command_Line_Configuration);
kono
parents:
diff changeset
722 -- Free the memory used by Config
kono
parents:
diff changeset
723
kono
parents:
diff changeset
724 type Switch_Handler is access procedure
kono
parents:
diff changeset
725 (Switch : String;
kono
parents:
diff changeset
726 Parameter : String;
kono
parents:
diff changeset
727 Section : String);
kono
parents:
diff changeset
728 -- Called when a switch is found on the command line. Switch includes
kono
parents:
diff changeset
729 -- any leading '-' that was specified in Define_Switch. This is slightly
kono
parents:
diff changeset
730 -- different from the functional version of Getopt above, for which
kono
parents:
diff changeset
731 -- Full_Switch omits the first leading '-'.
kono
parents:
diff changeset
732
kono
parents:
diff changeset
733 Exit_From_Command_Line : exception;
kono
parents:
diff changeset
734 -- Emitted when the program should exit. This is called when Getopt below
kono
parents:
diff changeset
735 -- has seen -h, --help or an invalid switch.
kono
parents:
diff changeset
736
kono
parents:
diff changeset
737 procedure Getopt
kono
parents:
diff changeset
738 (Config : Command_Line_Configuration;
kono
parents:
diff changeset
739 Callback : Switch_Handler := null;
kono
parents:
diff changeset
740 Parser : Opt_Parser := Command_Line_Parser;
kono
parents:
diff changeset
741 Concatenate : Boolean := True);
kono
parents:
diff changeset
742 -- Similar to the standard Getopt function. For each switch found on the
kono
parents:
diff changeset
743 -- command line, this calls Callback, if the switch is not handled
kono
parents:
diff changeset
744 -- automatically.
kono
parents:
diff changeset
745 --
kono
parents:
diff changeset
746 -- The list of valid switches are the ones from the configuration. The
kono
parents:
diff changeset
747 -- switches that were declared through Define_Switch with an Output
kono
parents:
diff changeset
748 -- parameter are never returned (and result in a modification of the Output
kono
parents:
diff changeset
749 -- variable). This function will in fact never call [Callback] if all
kono
parents:
diff changeset
750 -- switches were handled automatically and there is nothing left to do.
kono
parents:
diff changeset
751 --
kono
parents:
diff changeset
752 -- The option Concatenate is identical to the one of the standard Getopt
kono
parents:
diff changeset
753 -- function.
kono
parents:
diff changeset
754 --
kono
parents:
diff changeset
755 -- This procedure automatically adds -h and --help to the valid switches,
kono
parents:
diff changeset
756 -- to display the help message and raises Exit_From_Command_Line.
kono
parents:
diff changeset
757 -- If an invalid switch is specified on the command line, this procedure
kono
parents:
diff changeset
758 -- will display an error message and raises Invalid_Switch again.
kono
parents:
diff changeset
759 --
kono
parents:
diff changeset
760 -- This function automatically expands switches:
kono
parents:
diff changeset
761 --
kono
parents:
diff changeset
762 -- If Define_Prefix was called (for instance "-gnaty") and the user
kono
parents:
diff changeset
763 -- specifies "-gnatycb" on the command line, then Getopt returns
kono
parents:
diff changeset
764 -- "-gnatyc" and "-gnatyb" separately.
kono
parents:
diff changeset
765 --
kono
parents:
diff changeset
766 -- If Define_Alias was called (for instance "-gnatya = -gnatycb") then
kono
parents:
diff changeset
767 -- the latter is returned (in this case it also expands -gnaty as per
kono
parents:
diff changeset
768 -- the above.
kono
parents:
diff changeset
769 --
kono
parents:
diff changeset
770 -- The goal is to make handling as easy as possible by leaving as much
kono
parents:
diff changeset
771 -- work as possible to this package.
kono
parents:
diff changeset
772 --
kono
parents:
diff changeset
773 -- As opposed to the standard Getopt, this one will analyze all sections
kono
parents:
diff changeset
774 -- as defined by Define_Section, and automatically jump from one section to
kono
parents:
diff changeset
775 -- the next.
kono
parents:
diff changeset
776
kono
parents:
diff changeset
777 ------------------------------
kono
parents:
diff changeset
778 -- Generating command lines --
kono
parents:
diff changeset
779 ------------------------------
kono
parents:
diff changeset
780
kono
parents:
diff changeset
781 -- Once the command line configuration has been created, you can build your
kono
parents:
diff changeset
782 -- own command line. This will be done in general because you need to spawn
kono
parents:
diff changeset
783 -- external tools from your application.
kono
parents:
diff changeset
784
kono
parents:
diff changeset
785 -- Although it could be done by concatenating strings, the following
kono
parents:
diff changeset
786 -- subprograms will properly take care of grouping switches when possible,
kono
parents:
diff changeset
787 -- so as to keep the command line as short as possible. They also provide a
kono
parents:
diff changeset
788 -- way to remove a switch from an existing command line.
kono
parents:
diff changeset
789
kono
parents:
diff changeset
790 -- For instance:
kono
parents:
diff changeset
791
kono
parents:
diff changeset
792 -- declare
kono
parents:
diff changeset
793 -- Config : Command_Line_Configuration;
kono
parents:
diff changeset
794 -- Line : Command_Line;
kono
parents:
diff changeset
795 -- Args : Argument_List_Access;
kono
parents:
diff changeset
796
kono
parents:
diff changeset
797 -- begin
kono
parents:
diff changeset
798 -- Define_Switch (Config, "-gnatyc");
kono
parents:
diff changeset
799 -- Define_Switch (Config, ...); -- for all valid switches
kono
parents:
diff changeset
800 -- Define_Prefix (Config, "-gnaty");
kono
parents:
diff changeset
801
kono
parents:
diff changeset
802 -- Set_Configuration (Line, Config);
kono
parents:
diff changeset
803 -- Add_Switch (Line, "-O2");
kono
parents:
diff changeset
804 -- Add_Switch (Line, "-gnatyc");
kono
parents:
diff changeset
805 -- Add_Switch (Line, "-gnatyd");
kono
parents:
diff changeset
806 --
kono
parents:
diff changeset
807 -- Build (Line, Args);
kono
parents:
diff changeset
808 -- -- Args is now ["-O2", "-gnatycd"]
kono
parents:
diff changeset
809 -- end;
kono
parents:
diff changeset
810
kono
parents:
diff changeset
811 type Command_Line is private;
kono
parents:
diff changeset
812
kono
parents:
diff changeset
813 procedure Set_Configuration
kono
parents:
diff changeset
814 (Cmd : in out Command_Line;
kono
parents:
diff changeset
815 Config : Command_Line_Configuration);
kono
parents:
diff changeset
816 function Get_Configuration
kono
parents:
diff changeset
817 (Cmd : Command_Line) return Command_Line_Configuration;
kono
parents:
diff changeset
818 -- Set or retrieve the configuration used for that command line. The Config
kono
parents:
diff changeset
819 -- must have been initialized first, by calling one of the Define_Switches
kono
parents:
diff changeset
820 -- subprograms.
kono
parents:
diff changeset
821
kono
parents:
diff changeset
822 procedure Set_Command_Line
kono
parents:
diff changeset
823 (Cmd : in out Command_Line;
kono
parents:
diff changeset
824 Switches : String;
kono
parents:
diff changeset
825 Getopt_Description : String := "";
kono
parents:
diff changeset
826 Switch_Char : Character := '-');
kono
parents:
diff changeset
827 -- Set the new content of the command line, by replacing the current
kono
parents:
diff changeset
828 -- version with Switches.
kono
parents:
diff changeset
829 --
kono
parents:
diff changeset
830 -- The parsing of Switches is done through calls to Getopt, by passing
kono
parents:
diff changeset
831 -- Getopt_Description as an argument. (A "*" is automatically prepended so
kono
parents:
diff changeset
832 -- that all switches and command line arguments are accepted). If a config
kono
parents:
diff changeset
833 -- was defined via Set_Configuration, the Getopt_Description parameter will
kono
parents:
diff changeset
834 -- be ignored.
kono
parents:
diff changeset
835 --
kono
parents:
diff changeset
836 -- To properly handle switches that take parameters, you should document
kono
parents:
diff changeset
837 -- them in Getopt_Description. Otherwise, the switch and its parameter will
kono
parents:
diff changeset
838 -- be recorded as two separate command line arguments as returned by a
kono
parents:
diff changeset
839 -- Command_Line_Iterator (which might be fine depending on your
kono
parents:
diff changeset
840 -- application).
kono
parents:
diff changeset
841 --
kono
parents:
diff changeset
842 -- If the command line has sections (such as -bargs -cargs), then they
kono
parents:
diff changeset
843 -- should be listed in the Sections parameter (as "-bargs -cargs").
kono
parents:
diff changeset
844 --
kono
parents:
diff changeset
845 -- This function can be used to reset Cmd by passing an empty string
kono
parents:
diff changeset
846 --
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
847 -- If an invalid switch is found on the command line (i.e. wasn't defined
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
848 -- in the configuration via Define_Switch), and the configuration wasn't
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
849 -- set to accept all switches (by defining "*" as a valid switch), then an
111
kono
parents:
diff changeset
850 -- exception Invalid_Switch is raised. The exception message indicates the
kono
parents:
diff changeset
851 -- invalid switch.
kono
parents:
diff changeset
852
kono
parents:
diff changeset
853 procedure Add_Switch
kono
parents:
diff changeset
854 (Cmd : in out Command_Line;
kono
parents:
diff changeset
855 Switch : String;
kono
parents:
diff changeset
856 Parameter : String := "";
kono
parents:
diff changeset
857 Separator : Character := ASCII.NUL;
kono
parents:
diff changeset
858 Section : String := "";
kono
parents:
diff changeset
859 Add_Before : Boolean := False);
kono
parents:
diff changeset
860 -- Add a new switch to the command line, and combine/group it with existing
kono
parents:
diff changeset
861 -- switches if possible. Nothing is done if the switch already exists with
kono
parents:
diff changeset
862 -- the same parameter.
kono
parents:
diff changeset
863 --
kono
parents:
diff changeset
864 -- If the Switch takes a parameter, the latter should be specified
kono
parents:
diff changeset
865 -- separately, so that the association between the two is always correctly
kono
parents:
diff changeset
866 -- recognized even if the order of switches on the command line changes.
kono
parents:
diff changeset
867 -- For instance, you should pass "--check=full" as ("--check", "full") so
kono
parents:
diff changeset
868 -- that Remove_Switch below can simply take "--check" in parameter. That
kono
parents:
diff changeset
869 -- will automatically remove "full" as well. The value of the parameter is
kono
parents:
diff changeset
870 -- never modified by this package.
kono
parents:
diff changeset
871 --
kono
parents:
diff changeset
872 -- On the other hand, you could decide to simply pass "--check=full" as
kono
parents:
diff changeset
873 -- the Switch above, and then pass no parameter. This means that you need
kono
parents:
diff changeset
874 -- to pass "--check=full" to Remove_Switch as well.
kono
parents:
diff changeset
875 --
kono
parents:
diff changeset
876 -- A Switch with a parameter will never be grouped with another switch to
kono
parents:
diff changeset
877 -- avoid ambiguities as to what the parameter applies to.
kono
parents:
diff changeset
878 --
kono
parents:
diff changeset
879 -- If the switch is part of a section, then it should be specified so that
kono
parents:
diff changeset
880 -- the switch is correctly placed in the command line, and the section
kono
parents:
diff changeset
881 -- added if not already present. For example, to add the -g switch into the
kono
parents:
diff changeset
882 -- -cargs section, you need to call (Cmd, "-g", Section => "-cargs").
kono
parents:
diff changeset
883 --
kono
parents:
diff changeset
884 -- [Separator], if specified, overrides the separator that was defined
kono
parents:
diff changeset
885 -- through Define_Switch. For instance, if the switch was defined as
kono
parents:
diff changeset
886 -- "-from:", the separator defaults to a space. But if your application
kono
parents:
diff changeset
887 -- uses unusual separators not supported by GNAT.Command_Line (for instance
kono
parents:
diff changeset
888 -- it requires ":"), you can specify this separator here.
kono
parents:
diff changeset
889 --
kono
parents:
diff changeset
890 -- For instance,
kono
parents:
diff changeset
891 -- Add_Switch(Cmd, "-from", "bar", ':')
kono
parents:
diff changeset
892 --
kono
parents:
diff changeset
893 -- results in
kono
parents:
diff changeset
894 -- -from:bar
kono
parents:
diff changeset
895 --
kono
parents:
diff changeset
896 -- rather than the default
kono
parents:
diff changeset
897 -- -from bar
kono
parents:
diff changeset
898 --
kono
parents:
diff changeset
899 -- Note however that Getopt doesn't know how to handle ":" as a separator.
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
900 -- So the recommendation is to declare the switch as "-from!" (i.e. no
111
kono
parents:
diff changeset
901 -- space between the switch and its parameter). Then Getopt will return
kono
parents:
diff changeset
902 -- ":bar" as the parameter, and you can trim the ":" in your application.
kono
parents:
diff changeset
903 --
kono
parents:
diff changeset
904 -- Invalid_Section is raised if Section was not defined in the
kono
parents:
diff changeset
905 -- configuration of the command line.
kono
parents:
diff changeset
906 --
kono
parents:
diff changeset
907 -- Add_Before allows insertion of the switch at the beginning of the
kono
parents:
diff changeset
908 -- command line.
kono
parents:
diff changeset
909
kono
parents:
diff changeset
910 procedure Add_Switch
kono
parents:
diff changeset
911 (Cmd : in out Command_Line;
kono
parents:
diff changeset
912 Switch : String;
kono
parents:
diff changeset
913 Parameter : String := "";
kono
parents:
diff changeset
914 Separator : Character := ASCII.NUL;
kono
parents:
diff changeset
915 Section : String := "";
kono
parents:
diff changeset
916 Add_Before : Boolean := False;
kono
parents:
diff changeset
917 Success : out Boolean);
kono
parents:
diff changeset
918 -- Same as above, returning the status of the operation
kono
parents:
diff changeset
919
kono
parents:
diff changeset
920 procedure Remove_Switch
kono
parents:
diff changeset
921 (Cmd : in out Command_Line;
kono
parents:
diff changeset
922 Switch : String;
kono
parents:
diff changeset
923 Remove_All : Boolean := False;
kono
parents:
diff changeset
924 Has_Parameter : Boolean := False;
kono
parents:
diff changeset
925 Section : String := "");
kono
parents:
diff changeset
926 -- Remove Switch from the command line, and ungroup existing switches if
kono
parents:
diff changeset
927 -- necessary.
kono
parents:
diff changeset
928 --
kono
parents:
diff changeset
929 -- The actual parameter to the switches are ignored. If for instance
kono
parents:
diff changeset
930 -- you are removing "-foo", then "-foo param1" and "-foo param2" can
kono
parents:
diff changeset
931 -- be removed.
kono
parents:
diff changeset
932 --
kono
parents:
diff changeset
933 -- If Remove_All is True, then all matching switches are removed, otherwise
kono
parents:
diff changeset
934 -- only the first matching one is removed.
kono
parents:
diff changeset
935 --
kono
parents:
diff changeset
936 -- If Has_Parameter is set to True, then only switches having a parameter
kono
parents:
diff changeset
937 -- are removed.
kono
parents:
diff changeset
938 --
kono
parents:
diff changeset
939 -- If the switch belongs to a section, then this section should be
kono
parents:
diff changeset
940 -- specified: Remove_Switch (Cmd_Line, "-g", Section => "-cargs") called
kono
parents:
diff changeset
941 -- on the command line "-g -cargs -g" will result in "-g", while if
kono
parents:
diff changeset
942 -- called with (Cmd_Line, "-g") this will result in "-cargs -g".
kono
parents:
diff changeset
943 -- If Remove_All is set, then both "-g" will be removed.
kono
parents:
diff changeset
944
kono
parents:
diff changeset
945 procedure Remove_Switch
kono
parents:
diff changeset
946 (Cmd : in out Command_Line;
kono
parents:
diff changeset
947 Switch : String;
kono
parents:
diff changeset
948 Remove_All : Boolean := False;
kono
parents:
diff changeset
949 Has_Parameter : Boolean := False;
kono
parents:
diff changeset
950 Section : String := "";
kono
parents:
diff changeset
951 Success : out Boolean);
kono
parents:
diff changeset
952 -- Same as above, reporting the success of the operation (Success is False
kono
parents:
diff changeset
953 -- if no switch was removed).
kono
parents:
diff changeset
954
kono
parents:
diff changeset
955 procedure Remove_Switch
kono
parents:
diff changeset
956 (Cmd : in out Command_Line;
kono
parents:
diff changeset
957 Switch : String;
kono
parents:
diff changeset
958 Parameter : String;
kono
parents:
diff changeset
959 Section : String := "");
kono
parents:
diff changeset
960 -- Remove a switch with a specific parameter. If Parameter is the empty
kono
parents:
diff changeset
961 -- string, then only a switch with no parameter will be removed.
kono
parents:
diff changeset
962
kono
parents:
diff changeset
963 procedure Free (Cmd : in out Command_Line);
kono
parents:
diff changeset
964 -- Free the memory used by Cmd
kono
parents:
diff changeset
965
kono
parents:
diff changeset
966 ---------------
kono
parents:
diff changeset
967 -- Iteration --
kono
parents:
diff changeset
968 ---------------
kono
parents:
diff changeset
969
kono
parents:
diff changeset
970 -- When a command line was created with the above, you can then iterate
kono
parents:
diff changeset
971 -- over its contents using the following iterator.
kono
parents:
diff changeset
972
kono
parents:
diff changeset
973 type Command_Line_Iterator is private;
kono
parents:
diff changeset
974
kono
parents:
diff changeset
975 procedure Start
kono
parents:
diff changeset
976 (Cmd : in out Command_Line;
kono
parents:
diff changeset
977 Iter : in out Command_Line_Iterator;
kono
parents:
diff changeset
978 Expanded : Boolean := False);
kono
parents:
diff changeset
979 -- Start iterating over the command line arguments. If Expanded is true,
kono
parents:
diff changeset
980 -- then the arguments are not grouped and no alias is used. For instance,
kono
parents:
diff changeset
981 -- "-gnatwv" and "-gnatwu" would be returned instead of "-gnatwuv".
kono
parents:
diff changeset
982 --
kono
parents:
diff changeset
983 -- The iterator becomes invalid if the command line is changed through a
kono
parents:
diff changeset
984 -- call to Add_Switch, Remove_Switch or Set_Command_Line.
kono
parents:
diff changeset
985
kono
parents:
diff changeset
986 function Current_Switch (Iter : Command_Line_Iterator) return String;
kono
parents:
diff changeset
987 function Is_New_Section (Iter : Command_Line_Iterator) return Boolean;
kono
parents:
diff changeset
988 function Current_Section (Iter : Command_Line_Iterator) return String;
kono
parents:
diff changeset
989 function Current_Separator (Iter : Command_Line_Iterator) return String;
kono
parents:
diff changeset
990 function Current_Parameter (Iter : Command_Line_Iterator) return String;
kono
parents:
diff changeset
991 -- Return the current switch and its parameter (or the empty string if
kono
parents:
diff changeset
992 -- there is no parameter or the switch was added through Add_Switch
kono
parents:
diff changeset
993 -- without specifying the parameter.
kono
parents:
diff changeset
994 --
kono
parents:
diff changeset
995 -- Separator is the string that goes between the switch and its separator.
kono
parents:
diff changeset
996 -- It could be the empty string if they should be concatenated, or a space
kono
parents:
diff changeset
997 -- for instance. When printing, you should not add any other character.
kono
parents:
diff changeset
998
kono
parents:
diff changeset
999 function Has_More (Iter : Command_Line_Iterator) return Boolean;
kono
parents:
diff changeset
1000 -- Return True if there are more switches to be returned
kono
parents:
diff changeset
1001
kono
parents:
diff changeset
1002 procedure Next (Iter : in out Command_Line_Iterator);
kono
parents:
diff changeset
1003 -- Move to the next switch
kono
parents:
diff changeset
1004
kono
parents:
diff changeset
1005 procedure Build
kono
parents:
diff changeset
1006 (Line : in out Command_Line;
kono
parents:
diff changeset
1007 Args : out GNAT.OS_Lib.Argument_List_Access;
kono
parents:
diff changeset
1008 Expanded : Boolean := False;
kono
parents:
diff changeset
1009 Switch_Char : Character := '-');
kono
parents:
diff changeset
1010 -- This is a wrapper using the Command_Line_Iterator. It provides a simple
kono
parents:
diff changeset
1011 -- way to get all switches (grouped as much as possible), and possibly
kono
parents:
diff changeset
1012 -- create an Opt_Parser.
kono
parents:
diff changeset
1013 --
kono
parents:
diff changeset
1014 -- Args must be freed by the caller.
kono
parents:
diff changeset
1015 --
kono
parents:
diff changeset
1016 -- Expanded has the same meaning as in Start.
kono
parents:
diff changeset
1017
kono
parents:
diff changeset
1018 procedure Try_Help;
kono
parents:
diff changeset
1019 -- Output a message on standard error to indicate how to get the usage for
kono
parents:
diff changeset
1020 -- the executable. This procedure should only be called when the executable
kono
parents:
diff changeset
1021 -- accepts switch --help. When this procedure is called by executable xxx,
kono
parents:
diff changeset
1022 -- the following message is displayed on standard error:
kono
parents:
diff changeset
1023 -- try "xxx --help" for more information.
kono
parents:
diff changeset
1024
kono
parents:
diff changeset
1025 private
kono
parents:
diff changeset
1026
kono
parents:
diff changeset
1027 Max_Depth : constant := 100;
kono
parents:
diff changeset
1028 -- Maximum depth of subdirectories
kono
parents:
diff changeset
1029
kono
parents:
diff changeset
1030 Max_Path_Length : constant := 1024;
kono
parents:
diff changeset
1031 -- Maximum length of relative path
kono
parents:
diff changeset
1032
kono
parents:
diff changeset
1033 type Depth is range 1 .. Max_Depth;
kono
parents:
diff changeset
1034
kono
parents:
diff changeset
1035 type Level is record
kono
parents:
diff changeset
1036 Name_Last : Natural := 0;
kono
parents:
diff changeset
1037 Dir : GNAT.Directory_Operations.Dir_Type;
kono
parents:
diff changeset
1038 end record;
kono
parents:
diff changeset
1039
kono
parents:
diff changeset
1040 type Level_Array is array (Depth) of Level;
kono
parents:
diff changeset
1041
kono
parents:
diff changeset
1042 type Section_Number is new Natural range 0 .. 65534;
kono
parents:
diff changeset
1043 for Section_Number'Size use 16;
kono
parents:
diff changeset
1044
kono
parents:
diff changeset
1045 type Parameter_Type is record
kono
parents:
diff changeset
1046 Arg_Num : Positive;
kono
parents:
diff changeset
1047 First : Positive;
kono
parents:
diff changeset
1048 Last : Natural;
kono
parents:
diff changeset
1049 Extra : Character;
kono
parents:
diff changeset
1050 end record;
kono
parents:
diff changeset
1051
kono
parents:
diff changeset
1052 type Is_Switch_Type is array (Natural range <>) of Boolean;
kono
parents:
diff changeset
1053 pragma Pack (Is_Switch_Type);
kono
parents:
diff changeset
1054
kono
parents:
diff changeset
1055 type Section_Type is array (Natural range <>) of Section_Number;
kono
parents:
diff changeset
1056 pragma Pack (Section_Type);
kono
parents:
diff changeset
1057
kono
parents:
diff changeset
1058 type Expansion_Iterator is limited record
kono
parents:
diff changeset
1059 Start : Positive := 1;
kono
parents:
diff changeset
1060 -- Position of the first character of the relative path to check against
kono
parents:
diff changeset
1061 -- the pattern.
kono
parents:
diff changeset
1062
kono
parents:
diff changeset
1063 Dir_Name : String (1 .. Max_Path_Length);
kono
parents:
diff changeset
1064
kono
parents:
diff changeset
1065 Current_Depth : Depth := 1;
kono
parents:
diff changeset
1066
kono
parents:
diff changeset
1067 Levels : Level_Array;
kono
parents:
diff changeset
1068
kono
parents:
diff changeset
1069 Regexp : GNAT.Regexp.Regexp;
kono
parents:
diff changeset
1070 -- Regular expression built with the pattern
kono
parents:
diff changeset
1071
kono
parents:
diff changeset
1072 Maximum_Depth : Depth := 1;
kono
parents:
diff changeset
1073 -- The maximum depth of directories, reflecting the number of directory
kono
parents:
diff changeset
1074 -- separators in the pattern.
kono
parents:
diff changeset
1075 end record;
kono
parents:
diff changeset
1076
kono
parents:
diff changeset
1077 type Opt_Parser_Data (Arg_Count : Natural) is record
kono
parents:
diff changeset
1078 Arguments : GNAT.OS_Lib.Argument_List_Access;
kono
parents:
diff changeset
1079 -- null if reading from the command line
kono
parents:
diff changeset
1080
kono
parents:
diff changeset
1081 The_Parameter : Parameter_Type;
kono
parents:
diff changeset
1082 The_Separator : Character;
kono
parents:
diff changeset
1083 The_Switch : Parameter_Type;
kono
parents:
diff changeset
1084 -- This type and this variable are provided to store the current switch
kono
parents:
diff changeset
1085 -- and parameter.
kono
parents:
diff changeset
1086
kono
parents:
diff changeset
1087 Is_Switch : Is_Switch_Type (1 .. Arg_Count) := (others => False);
kono
parents:
diff changeset
1088 -- Indicates wich arguments on the command line are considered not be
kono
parents:
diff changeset
1089 -- switches or parameters to switches (leaving e.g. filenames,...)
kono
parents:
diff changeset
1090
kono
parents:
diff changeset
1091 Section : Section_Type (1 .. Arg_Count) := (others => 1);
kono
parents:
diff changeset
1092 -- Contains the number of the section associated with the current
kono
parents:
diff changeset
1093 -- switch. If this number is 0, then it is a section delimiter, which is
kono
parents:
diff changeset
1094 -- never returned by GetOpt.
kono
parents:
diff changeset
1095
kono
parents:
diff changeset
1096 Current_Argument : Natural := 1;
kono
parents:
diff changeset
1097 -- Number of the current argument parsed on the command line
kono
parents:
diff changeset
1098
kono
parents:
diff changeset
1099 Current_Index : Natural := 1;
kono
parents:
diff changeset
1100 -- Index in the current argument of the character to be processed
kono
parents:
diff changeset
1101
kono
parents:
diff changeset
1102 Current_Section : Section_Number := 1;
kono
parents:
diff changeset
1103
kono
parents:
diff changeset
1104 Expansion_It : aliased Expansion_Iterator;
kono
parents:
diff changeset
1105 -- When Get_Argument is expanding a file name, this is the iterator used
kono
parents:
diff changeset
1106
kono
parents:
diff changeset
1107 In_Expansion : Boolean := False;
kono
parents:
diff changeset
1108 -- True if we are expanding a file
kono
parents:
diff changeset
1109
kono
parents:
diff changeset
1110 Switch_Character : Character := '-';
kono
parents:
diff changeset
1111 -- The character at the beginning of the command line arguments,
kono
parents:
diff changeset
1112 -- indicating the beginning of a switch.
kono
parents:
diff changeset
1113
kono
parents:
diff changeset
1114 Stop_At_First : Boolean := False;
kono
parents:
diff changeset
1115 -- If it is True then Getopt stops at the first non-switch argument
kono
parents:
diff changeset
1116 end record;
kono
parents:
diff changeset
1117
kono
parents:
diff changeset
1118 Command_Line_Parser_Data : aliased Opt_Parser_Data
kono
parents:
diff changeset
1119 (Ada.Command_Line.Argument_Count);
kono
parents:
diff changeset
1120 -- The internal data used when parsing the command line
kono
parents:
diff changeset
1121
kono
parents:
diff changeset
1122 type Opt_Parser is access all Opt_Parser_Data;
kono
parents:
diff changeset
1123 Command_Line_Parser : constant Opt_Parser :=
kono
parents:
diff changeset
1124 Command_Line_Parser_Data'Access;
kono
parents:
diff changeset
1125
kono
parents:
diff changeset
1126 type Switch_Type is (Switch_Untyped,
kono
parents:
diff changeset
1127 Switch_Boolean,
kono
parents:
diff changeset
1128 Switch_Integer,
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1129 Switch_String,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1130 Switch_Callback);
111
kono
parents:
diff changeset
1131
kono
parents:
diff changeset
1132 type Switch_Definition (Typ : Switch_Type := Switch_Untyped) is record
kono
parents:
diff changeset
1133 Switch : GNAT.OS_Lib.String_Access;
kono
parents:
diff changeset
1134 Long_Switch : GNAT.OS_Lib.String_Access;
kono
parents:
diff changeset
1135 Section : GNAT.OS_Lib.String_Access;
kono
parents:
diff changeset
1136 Help : GNAT.OS_Lib.String_Access;
kono
parents:
diff changeset
1137
kono
parents:
diff changeset
1138 Argument : GNAT.OS_Lib.String_Access;
kono
parents:
diff changeset
1139 -- null if "ARG".
kono
parents:
diff changeset
1140 -- Name of the argument for this switch.
kono
parents:
diff changeset
1141
kono
parents:
diff changeset
1142 case Typ is
kono
parents:
diff changeset
1143 when Switch_Untyped =>
kono
parents:
diff changeset
1144 null;
kono
parents:
diff changeset
1145 when Switch_Boolean =>
kono
parents:
diff changeset
1146 Boolean_Output : access Boolean;
kono
parents:
diff changeset
1147 Boolean_Value : Boolean; -- will set Output to that value
kono
parents:
diff changeset
1148 when Switch_Integer =>
kono
parents:
diff changeset
1149 Integer_Output : access Integer;
kono
parents:
diff changeset
1150 Integer_Initial : Integer;
kono
parents:
diff changeset
1151 Integer_Default : Integer;
kono
parents:
diff changeset
1152 when Switch_String =>
kono
parents:
diff changeset
1153 String_Output : access GNAT.Strings.String_Access;
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1154 when Switch_Callback =>
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1155 Callback : Value_Callback;
111
kono
parents:
diff changeset
1156 end case;
kono
parents:
diff changeset
1157 end record;
kono
parents:
diff changeset
1158 type Switch_Definitions is array (Natural range <>) of Switch_Definition;
kono
parents:
diff changeset
1159 type Switch_Definitions_List is access all Switch_Definitions;
kono
parents:
diff changeset
1160 -- [Switch] includes the leading '-'
kono
parents:
diff changeset
1161
kono
parents:
diff changeset
1162 type Alias_Definition is record
kono
parents:
diff changeset
1163 Alias : GNAT.OS_Lib.String_Access;
kono
parents:
diff changeset
1164 Expansion : GNAT.OS_Lib.String_Access;
kono
parents:
diff changeset
1165 Section : GNAT.OS_Lib.String_Access;
kono
parents:
diff changeset
1166 end record;
kono
parents:
diff changeset
1167 type Alias_Definitions is array (Natural range <>) of Alias_Definition;
kono
parents:
diff changeset
1168 type Alias_Definitions_List is access all Alias_Definitions;
kono
parents:
diff changeset
1169
kono
parents:
diff changeset
1170 type Command_Line_Configuration_Record is record
kono
parents:
diff changeset
1171 Prefixes : GNAT.OS_Lib.Argument_List_Access;
kono
parents:
diff changeset
1172 -- The list of prefixes
kono
parents:
diff changeset
1173
kono
parents:
diff changeset
1174 Sections : GNAT.OS_Lib.Argument_List_Access;
kono
parents:
diff changeset
1175 -- The list of sections
kono
parents:
diff changeset
1176
kono
parents:
diff changeset
1177 Star_Switch : Boolean := False;
kono
parents:
diff changeset
1178 -- Whether switches not described in this configuration should be
kono
parents:
diff changeset
1179 -- returned to the user (True). If False, an exception Invalid_Switch
kono
parents:
diff changeset
1180 -- is raised.
kono
parents:
diff changeset
1181
kono
parents:
diff changeset
1182 Aliases : Alias_Definitions_List;
kono
parents:
diff changeset
1183 Usage : GNAT.OS_Lib.String_Access;
kono
parents:
diff changeset
1184 Help : GNAT.OS_Lib.String_Access;
kono
parents:
diff changeset
1185 Help_Msg : GNAT.OS_Lib.String_Access;
kono
parents:
diff changeset
1186 Switches : Switch_Definitions_List;
kono
parents:
diff changeset
1187 -- List of expected switches (Used when expanding switch groups)
kono
parents:
diff changeset
1188 end record;
kono
parents:
diff changeset
1189 type Command_Line_Configuration is access Command_Line_Configuration_Record;
kono
parents:
diff changeset
1190
kono
parents:
diff changeset
1191 type Command_Line is record
kono
parents:
diff changeset
1192 Config : Command_Line_Configuration;
kono
parents:
diff changeset
1193 Expanded : GNAT.OS_Lib.Argument_List_Access;
kono
parents:
diff changeset
1194
kono
parents:
diff changeset
1195 Params : GNAT.OS_Lib.Argument_List_Access;
kono
parents:
diff changeset
1196 -- Parameter for the corresponding switch in Expanded. The first
kono
parents:
diff changeset
1197 -- character is the separator (or ASCII.NUL if there is no separator).
kono
parents:
diff changeset
1198
kono
parents:
diff changeset
1199 Sections : GNAT.OS_Lib.Argument_List_Access;
kono
parents:
diff changeset
1200 -- The list of sections
kono
parents:
diff changeset
1201
kono
parents:
diff changeset
1202 Coalesce : GNAT.OS_Lib.Argument_List_Access;
kono
parents:
diff changeset
1203 Coalesce_Params : GNAT.OS_Lib.Argument_List_Access;
kono
parents:
diff changeset
1204 Coalesce_Sections : GNAT.OS_Lib.Argument_List_Access;
kono
parents:
diff changeset
1205 -- Cached version of the command line. This is recomputed every time
kono
parents:
diff changeset
1206 -- the command line changes. Switches are grouped as much as possible,
kono
parents:
diff changeset
1207 -- and aliases are used to reduce the length of the command line. The
kono
parents:
diff changeset
1208 -- parameters are not allocated, they point into Params, so they must
kono
parents:
diff changeset
1209 -- not be freed.
kono
parents:
diff changeset
1210 end record;
kono
parents:
diff changeset
1211
kono
parents:
diff changeset
1212 type Command_Line_Iterator is record
kono
parents:
diff changeset
1213 List : GNAT.OS_Lib.Argument_List_Access;
kono
parents:
diff changeset
1214 Sections : GNAT.OS_Lib.Argument_List_Access;
kono
parents:
diff changeset
1215 Params : GNAT.OS_Lib.Argument_List_Access;
kono
parents:
diff changeset
1216 Current : Natural;
kono
parents:
diff changeset
1217 end record;
kono
parents:
diff changeset
1218
kono
parents:
diff changeset
1219 end GNAT.Command_Line;