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