OSDN Git Service

57a68c2ab2f97d7dc6785f17b2fcbae8201651b3
[pf3gnuchains/gcc-fork.git] / gcc / ada / g-comlin.ads
1 ------------------------------------------------------------------------------
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-2009, 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 2,  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.  See the GNU General Public License --
17 -- for  more details.  You should have  received  a copy of the GNU General --
18 -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
19 -- to  the  Free Software Foundation,  51  Franklin  Street,  Fifth  Floor, --
20 -- Boston, MA 02110-1301, USA.                                              --
21 --                                                                          --
22 -- As a special exception,  if other files  instantiate  generics from this --
23 -- unit, or you link  this unit with other files  to produce an executable, --
24 -- this  unit  does not  by itself cause  the resulting  executable  to  be --
25 -- covered  by the  GNU  General  Public  License.  This exception does not --
26 -- however invalidate  any other reasons why  the executable file  might be --
27 -- covered by the  GNU Public License.                                      --
28 --                                                                          --
29 -- GNAT was originally developed  by the GNAT team at  New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
31 --                                                                          --
32 ------------------------------------------------------------------------------
33
34 --  High level package for command line parsing and manipulation
35
36 --  Parsing the command line
37 --  ========================
38
39 --  This package provides an interface for parsing command line arguments,
40 --  when they are either read from Ada.Command_Line or read from a string list.
41 --  As shown in the example below, one should first retrieve the switches
42 --  (special command line arguments starting with '-' by default) and their
43 --  parameters, and then the rest of the command line arguments.
44
45 --  This package is flexible enough to accommodate various needs: optional
46 --  switch parameters, various characters to separate a switch and its
47 --  parameter, whether to stop the parsing at the first non-switch argument
48 --  encountered, etc.
49
50 --  begin
51 --     loop
52 --        case Getopt ("a b: ad") is  -- Accepts '-a', '-ad', or '-b argument'
53 --           when ASCII.NUL => exit;
54
55 --           when 'a' =>
56 --                 if Full_Switch = "a" then
57 --                    Put_Line ("Got a");
58 --                 else
59 --                    Put_Line ("Got ad");
60 --                 end if;
61
62 --           when 'b' =>
63 --              Put_Line ("Got b + " & Parameter);
64
65 --           when others =>
66 --              raise Program_Error;         -- cannot occur!
67 --        end case;
68 --     end loop;
69
70 --     loop
71 --        declare
72 --           S : constant String := Get_Argument (Do_Expansion => True);
73 --        begin
74 --           exit when S'Length = 0;
75 --           Put_Line ("Got " & S);
76 --        end;
77 --     end loop;
78
79 --  exception
80 --     when Invalid_Switch    => Put_Line ("Invalid Switch " & Full_Switch);
81 --     when Invalid_Parameter => Put_Line ("No parameter for " & Full_Switch);
82 --  end;
83
84 --  A more complicated example would involve the use of sections for the
85 --  switches, as for instance in gnatmake. The same command line is used to
86 --  provide switches for several tools. Each tool recognizes its switches by
87 --  separating them with special switches, chosen by the programmer.
88 --  Each section acts as a command line of its own.
89
90 --  begin
91 --     Initialize_Option_Scan ('-', False, "largs bargs cargs");
92 --     loop
93 --        --  Same loop as above to get switches and arguments
94 --     end loop;
95
96 --     Goto_Section ("bargs");
97 --     loop
98 --        --  Same loop as above to get switches and arguments
99 --        --  The supported switches in Getopt might be different
100 --     end loop;
101
102 --     Goto_Section ("cargs");
103 --     loop
104 --        --  Same loop as above to get switches and arguments
105 --        --  The supported switches in Getopt might be different
106 --     end loop;
107 --  end;
108
109 --  The example above have shown how to parse the command line when the
110 --  arguments are read directly from Ada.Command_Line. However, these arguments
111 --  can also be read from a list of strings. This can be useful in several
112 --  contexts, either because your system does not support Ada.Command_Line, or
113 --  because you are manipulating other tools and creating their command line by
114 --  hand, or for any other reason.
115
116 --  To create the list of strings, it is recommended to use
117 --  GNAT.OS_Lib.Argument_String_To_List.
118
119 --  The example below shows how to get the parameters from such a list. Note
120 --  also the use of '*' to get all the switches, and not report errors when an
121 --  unexpected switch was used by the user
122
123 --  declare
124 --     Parser : Opt_Parser;
125 --     Args : constant Argument_List_Access :=
126 --        GNAT.OS_Lib.Argument_String_To_List ("-g -O1 -Ipath");
127 --  begin
128 --     Initialize_Option_Scan (Parser, Args);
129 --     while Getopt ("* g O! I=", Parser) /= ASCII.NUL loop
130 --        Put_Line ("Switch " & Full_Switch (Parser)
131 --                  & " param=" & Parameter (Parser));
132 --     end loop;
133 --     Free (Parser);
134 --  end;
135 --
136 --  Creating and manipulating the command line
137 --  ===========================================
138
139 --  This package provides handling of command line by providing methods to
140 --  add or remove arguments from it. The resulting command line is kept as
141 --  short as possible by coalescing arguments whenever possible.
142
143 --  This package can be used to construct complex command lines for instance
144 --  from an GUI interface (although the package itself does not depend on a
145 --  specific GUI toolkit). For instance, if you are configuring the command
146 --  line to use when spawning a tool with the following characteristics:
147
148 --    * Specifying -gnatwa is the same as specifying -gnatwu -gnatwv, but
149 --      shorter and more readable
150
151 --    * All switches starting with -gnatw can be grouped, for instance one
152 --      can write -gnatwcd instead of -gnatwc -gnatwd.
153 --      Of course, this can be combined with the above and -gnatwacd is the
154 --      same as -gnatwc -gnatwd -gnatwu -gnatwv
155
156 --    * The switch -T is the same as -gnatwAB
157
158 --    * A switch -foo takes one mandatory parameter
159
160 --  These attributes can be configured through this package with the following
161 --  calls:
162
163 --     Config : Command_Line_Configuration;
164 --     Define_Prefix (Config, "-gnatw");
165 --     Define_Alias  (Config, "-gnatwa", "-gnatwuv");
166 --     Define_Alias  (Config, "-T",      "-gnatwAB");
167
168 --  Using this configuration, one can then construct a command line for the
169 --  tool with:
170
171 --     Cmd : Command_Line;
172 --     Set_Configuration (Cmd, Config);
173 --     Add_Switch (Cmd, "-bar");
174 --     Add_Switch (Cmd, "-gnatwu");
175 --     Add_Switch (Cmd, "-gnatwv");  --  will be grouped with the above
176 --     Add_Switch (Cmd, "-T");
177
178 --  The resulting command line can be iterated over to get all its switches,
179 --  There are two modes for this iteration: either you want to get the
180 --  shortest possible command line, which would be:
181
182 --      -bar -gnatwaAB
183
184 --  or on the other hand you want each individual switch (so that your own
185 --  tool does not have to do further complex processing), which would be:
186
187 --      -bar -gnatwu -gnatwv -gnatwA -gnatwB
188
189 --  Of course, we can assume that the tool you want to spawn would understand
190 --  both of these, since they are both compatible with the description we gave
191 --  above. However, the first result is useful if you want to show the user
192 --  what you are spawning (since that keeps the output shorter), and the second
193 --  output is more useful for a tool that would check whether -gnatwu was
194 --  passed (which isn't obvious in the first output). Likewise, the second
195 --  output is more useful if you have a graphical interface since each switch
196 --  can be associated with a widget, and you immediately know whether -gnatwu
197 --  was selected.
198 --
199 --  Some command line arguments can have parameters, which on a command line
200 --  appear as a separate argument that must immediately follow the switch.
201 --  Since the subprograms in this package will reorganize the switches to group
202 --  them, you need to indicate what is a command line
203 --  parameter, and what is a switch argument.
204
205 --  This is done by passing an extra argument to Add_Switch, as in:
206
207 --     Add_Switch (Cmd, "-foo", "arg1");
208
209 --  This ensures that "arg1" will always be treated as the argument to -foo,
210 --  and will not be grouped with other parts of the command line.
211
212 --  Parsing the command line with grouped arguments
213 --  ===============================================
214
215 --  This package also works great in collaboration with GNAT.Command_Line, to
216 --  parse the input to your tools. If you are writing the tool we described
217 --  above, you would do a first loop with Getopt to pass the switches and
218 --  their arguments, and create a temporary representation of the command line
219 --  as a Command_Line object. Finally, you can ask each individual switch to
220 --  that object. For instance:
221
222 --    declare
223 --      Cmd  : Command_Line;
224 --      Iter : Command_Line_Iterator;
225
226 --    begin
227 --      while Getopt ("foo: gnatw! T bar") /= ASCII.NUL loop
228 --         Add_Switch (Cmd, Full_Switch, Parameter);
229 --      end loop;
230
231 --      Start (Cmd, Iter, Expanded => True);
232 --      while Has_More (Iter) loop
233 --        if Current_Switch (Iter) = "-gnatwu" then ..
234 --        elsif Current_Switch (Iter) = "-gnatwv" then ...
235 --        end if;
236 --        Next (Iter);
237 --      end loop;
238
239 --  The above means that your tool does not have to handle on its own whether
240 --  the user passed -gnatwa (in which case -gnatwu was indeed selected), or
241 --  just -gnatwu, or a combination of -gnatw switches as in -gnatwuv.
242
243 with Ada.Command_Line;
244 with GNAT.Directory_Operations;
245 with GNAT.OS_Lib;
246 with GNAT.Regexp;
247
248 package GNAT.Command_Line is
249
250    -------------
251    -- Parsing --
252    -------------
253
254    type Opt_Parser is private;
255    Command_Line_Parser : constant Opt_Parser;
256    --  This object is responsible for parsing a list of arguments, which by
257    --  default are the standard command line arguments from Ada.Command_Line.
258    --  This is really a pointer to actual data, which must therefore be
259    --  initialized through a call to Initialize_Option_Scan, and must be freed
260    --  with a call to Free.
261    --
262    --  As a special case, Command_Line_Parser does not need to be either
263    --  initialized or free-ed.
264
265    procedure Initialize_Option_Scan
266      (Switch_Char              : Character := '-';
267       Stop_At_First_Non_Switch : Boolean := False;
268       Section_Delimiters       : String := "");
269    procedure Initialize_Option_Scan
270      (Parser                   : out Opt_Parser;
271       Command_Line             : GNAT.OS_Lib.Argument_List_Access;
272       Switch_Char              : Character := '-';
273       Stop_At_First_Non_Switch : Boolean := False;
274       Section_Delimiters       : String := "");
275    --  The first procedure resets the internal state of the package to prepare
276    --  to rescan the parameters. It does not need to be called before the first
277    --  use of Getopt (but it could be), but it must be called if you want to
278    --  start rescanning the command line parameters from the start. The
279    --  optional parameter Switch_Char can be used to reset the switch
280    --  character, e.g. to '/' for use in DOS-like systems.
281    --
282    --  The second subprogram initializes a parser that takes its arguments from
283    --  an array of strings rather than directly from the command line. In this
284    --  case, the parser is responsible for freeing the strings stored in
285    --  Command_Line. If you pass null to Command_Line, this will in fact create
286    --  a second parser for Ada.Command_Line, which doesn't share any data with
287    --  the default parser. This parser must be free-ed.
288    --
289    --  The optional parameter Stop_At_First_Non_Switch indicates if Getopt is
290    --  to look for switches on the whole command line, or if it has to stop as
291    --  soon as a non-switch argument is found.
292    --
293    --  Example:
294    --
295    --      Arguments: my_application file1 -c
296    --
297    --      If Stop_At_First_Non_Switch is False, then -c will be considered
298    --      as a switch (returned by getopt), otherwise it will be considered
299    --      as a normal argument (returned by Get_Argument).
300    --
301    --  If SECTION_DELIMITERS is set, then every following subprogram
302    --  (Getopt and Get_Argument) will only operate within a section, which
303    --  is delimited by any of these delimiters or the end of the command line.
304    --
305    --  Example:
306    --      Initialize_Option_Scan (Section_Delimiters => "largs bargs cargs");
307    --
308    --      Arguments on command line : my_application -c -bargs -d -e -largs -f
309    --      This line is made of three section, the first one is the default one
310    --      and includes only the '-c' switch, the second one is between -bargs
311    --      and -largs and includes '-d -e' and the last one includes '-f'
312
313    procedure Free (Parser : in out Opt_Parser);
314    --  Free the memory used by the parser. Calling this is not mandatory for
315    --  the Command_Line_Parser
316
317    procedure Goto_Section
318      (Name   : String := "";
319       Parser : Opt_Parser := Command_Line_Parser);
320    --  Change the current section. The next Getopt of Get_Argument will start
321    --  looking at the beginning of the section. An empty name ("") refers to
322    --  the first section between the program name and the first section
323    --  delimiter. If the section does not exist, then Invalid_Section is
324    --  raised.
325
326    function Full_Switch
327      (Parser : Opt_Parser := Command_Line_Parser) return String;
328    --  Returns the full name of the last switch found (Getopt only returns
329    --  the first character)
330
331    function Getopt
332      (Switches    : String;
333       Concatenate : Boolean := True;
334       Parser      : Opt_Parser := Command_Line_Parser) return Character;
335    --  This function moves to the next switch on the command line (defined as
336    --  switch character followed by a character within Switches, casing being
337    --  significant). The result returned is the first character of the switch
338    --  that is located. If there are no more switches in the current section,
339    --  returns ASCII.NUL. If Concatenate is True (by default), the switches
340    --  does not need to be separated by spaces (they can be concatenated if
341    --  they do not require an argument, e.g. -ab is the ame as two separate
342    --  arguments -a -b).
343    --
344    --  Switches is a string of all the possible switches, separated by a
345    --  space. A switch can be followed by one of the following characters:
346    --
347    --   ':'  The switch requires a parameter. There can optionally be a space
348    --        on the command line between the switch and its parameter.
349    --
350    --   '='  The switch requires a parameter. There can either be a '=' or a
351    --        space on the command line between the switch and its parameter.
352    --
353    --   '!'  The switch requires a parameter, but there can be no space on the
354    --        command line between the switch and its parameter.
355    --
356    --   '?'  The switch may have an optional parameter. There can be no space
357    --        between the switch and its argument.
358    --
359    --        e.g. if Switches has the following value : "a? b",
360    --        The command line can be:
361    --
362    --             -afoo    :  -a switch with 'foo' parameter
363    --             -a foo   :  -a switch and another element on the
364    --                           command line 'foo', returned by Get_Argument
365    --
366    --     Example: if Switches is "-a: -aO:", you can have the following
367    --              command lines:
368    --
369    --                -aarg    :  'a' switch with 'arg' parameter
370    --                -a arg   :  'a' switch with 'arg' parameter
371    --                -aOarg   :  'aO' switch with 'arg' parameter
372    --                -aO arg  :  'aO' switch with 'arg' parameter
373    --
374    --    Example:
375    --
376    --       Getopt ("a b: ac ad?")
377    --
378    --         accept either 'a' or 'ac' with no argument,
379    --         accept 'b' with a required argument
380    --         accept 'ad' with an optional argument
381    --
382    --  If the first item in switches is '*', then Getopt will catch
383    --  every element on the command line that was not caught by any other
384    --  switch. The character returned by GetOpt is '*', but Full_Switch
385    --  contains the full command line argument, including leading '-' if there
386    --  is one. If this character was not returned, there would be no way of
387    --  knowing whether it is there or not.
388    --
389    --    Example
390    --       Getopt ("* a b")
391    --       If the command line is '-a -c toto.o -b', Getopt will return
392    --       successively 'a', '*', '*' and 'b'. When '*' is returned,
393    --       Full_Switch returns the corresponding item on the command line.
394    --
395    --  When Getopt encounters an invalid switch, it raises the exception
396    --  Invalid_Switch and sets Full_Switch to return the invalid switch.
397    --  When Getopt cannot find the parameter associated with a switch, it
398    --  raises Invalid_Parameter, and sets Full_Switch to return the invalid
399    --  switch character.
400    --
401    --  Note: in case of ambiguity, e.g. switches a ab abc, then the longest
402    --  matching switch is returned.
403    --
404    --  Arbitrary characters are allowed for switches, although it is
405    --  strongly recommended to use only letters and digits for portability
406    --  reasons.
407    --
408    --  When Concatenate is False, individual switches need to be separated by
409    --  spaces.
410    --
411    --    Example
412    --       Getopt ("a b", Concatenate => False)
413    --       If the command line is '-ab', exception Invalid_Switch will be
414    --       raised and Full_Switch will return "ab".
415
416    function Get_Argument
417      (Do_Expansion : Boolean := False;
418       Parser       : Opt_Parser := Command_Line_Parser) return String;
419    --  Returns the next element on the command line which is not a switch.
420    --  This function should not be called before Getopt has returned
421    --  ASCII.NUL.
422    --
423    --  If Expansion is True, then the parameter on the command line will be
424    --  considered as a filename with wild cards, and will be expanded. The
425    --  matching file names will be returned one at a time. When there are no
426    --  more arguments on the command line, this function returns an empty
427    --  string. This is useful in non-Unix systems for obtaining normal
428    --  expansion of wild card references.
429
430    function Parameter
431      (Parser : Opt_Parser := Command_Line_Parser) return String;
432    --  Returns the parameter associated with the last switch returned by
433    --  Getopt. If no parameter was associated with the last switch, or no
434    --  previous call has been made to Get_Argument, raises Invalid_Parameter.
435    --  If the last switch was associated with an optional argument and this
436    --  argument was not found on the command line, Parameter returns an empty
437    --  string.
438
439    function Separator
440      (Parser : Opt_Parser := Command_Line_Parser) return Character;
441    --  The separator that was between the switch and its parameter. This is
442    --  of little use in general, only if you want to know exactly what was on
443    --  the command line. This is in general a single character, set to
444    --  ASCII.NUL if the switch and the parameter were concatenated. A space is
445    --  returned if the switch and its argument were in two separate arguments.
446
447    type Expansion_Iterator is limited private;
448    --  Type used during expansion of file names
449
450    procedure Start_Expansion
451      (Iterator     : out Expansion_Iterator;
452       Pattern      : String;
453       Directory    : String := "";
454       Basic_Regexp : Boolean := True);
455    --  Initialize a wild card expansion. The next calls to Expansion will
456    --  return the next file name in Directory which match Pattern (Pattern
457    --  is a regular expression, using only the Unix shell and DOS syntax if
458    --  Basic_Regexp is True). When Directory is an empty string, the current
459    --  directory is searched.
460    --
461    --  Pattern may contain directory separators (as in "src/*/*.ada").
462    --  Subdirectories of Directory will also be searched, up to one
463    --  hundred levels deep.
464    --
465    --  When Start_Expansion has been called, function Expansion should be
466    --  called repeatedly until it returns an empty string, before
467    --  Start_Expansion can be called again with the same Expansion_Iterator
468    --  variable.
469
470    function Expansion (Iterator : Expansion_Iterator) return String;
471    --  Returns the next file in the directory matching the parameters given
472    --  to Start_Expansion and updates Iterator to point to the next entry.
473    --  Returns an empty string when there is no more file in the directory
474    --  and its subdirectories.
475    --
476    --  If Expansion is called again after an empty string has been returned,
477    --  then the exception GNAT.Directory_Operations.Directory_Error is raised.
478
479    Invalid_Section : exception;
480    --  Raised when an invalid section is selected by Goto_Section
481
482    Invalid_Switch : exception;
483    --  Raised when an invalid switch is detected in the command line
484
485    Invalid_Parameter : exception;
486    --  Raised when a parameter is missing, or an attempt is made to obtain a
487    --  parameter for a switch that does not allow a parameter
488
489    -----------------
490    -- Configuring --
491    -----------------
492
493    type Command_Line_Configuration is private;
494
495    procedure Define_Alias
496      (Config   : in out Command_Line_Configuration;
497       Switch   : String;
498       Expanded : String);
499    --  Indicates that whenever Switch appears on the command line, it should
500    --  be expanded as Expanded. For instance, for the GNAT compiler switches,
501    --  we would define "-gnatwa" as an alias for "-gnatwcfijkmopruvz", ie some
502    --  default warnings to be activated.
503    --
504    --  Likewise, in some context you could define "--verbose" as an alias for
505    --  ("-v", "--full"), ie two switches.
506
507    procedure Define_Prefix
508      (Config   : in out Command_Line_Configuration;
509       Prefix   : String);
510    --  Indicates that all switches starting with the given prefix should be
511    --  grouped. For instance, for the GNAT compiler we would define "-gnatw"
512    --  as a prefix, so that "-gnatwu -gnatwv" can be grouped into "-gnatwuv"
513    --  It is assume that the remaining of the switch ("uv") is a set of
514    --  characters whose order is irrelevant. In fact, this package will sort
515    --  them alphabetically.
516
517    procedure Define_Switch
518      (Config : in out Command_Line_Configuration;
519       Switch : String);
520    --  Indicates a new switch. The format of this switch follows the getopt
521    --  format (trailing ':', '?', etc for defining a switch with parameters).
522    --  The switches defined in the command_line_configuration object are used
523    --  when ungrouping switches with more that one character after the prefix.
524
525    procedure Define_Section
526      (Config  : in out Command_Line_Configuration;
527       Section : String);
528    --  Indicates a new switch section. Every switch belonging to the same
529    --  section are ordered together, preceded by the section. They are placed
530    --  at the end of the command line (as in 'gnatmake somefile.adb -cargs -g')
531
532    function Get_Switches
533      (Config      : Command_Line_Configuration;
534       Switch_Char : Character) return String;
535    --  Get the switches list as expected by getopt. This list is built using
536    --  all switches defined previously via Define_Switch above.
537
538    procedure Free (Config : in out Command_Line_Configuration);
539    --  Free the memory used by Config
540
541    -------------
542    -- Editing --
543    -------------
544
545    type Command_Line is private;
546
547    procedure Set_Configuration
548      (Cmd    : in out Command_Line;
549       Config : Command_Line_Configuration);
550    --  Set the configuration for this command line
551
552    function Get_Configuration
553      (Cmd : Command_Line) return Command_Line_Configuration;
554    --  Return the configuration used for that command line
555
556    procedure Set_Command_Line
557      (Cmd                : in out Command_Line;
558       Switches           : String;
559       Getopt_Description : String    := "";
560       Switch_Char        : Character := '-');
561    --  Set the new content of the command line, by replacing the current
562    --  version with Switches.
563    --
564    --  The parsing of Switches is done through calls to Getopt, by passing
565    --  Getopt_Description as an argument. (a "*" is automatically prepended so
566    --  that all switches and command line arguments are accepted).
567    --
568    --  To properly handle switches that take parameters, you should document
569    --  them in Getopt_Description. Otherwise, the switch and its parameter will
570    --  be recorded as two separate command line arguments as returned by a
571    --  Command_Line_Iterator (which might be fine depending on your
572    --  application).
573    --
574    --  If the command line has sections (such as -bargs -largs -cargs), then
575    --  they should be listed in the Sections parameter (as "-bargs -cargs")
576    --
577    --  This function can be used to reset Cmd by passing an empty string.
578
579    procedure Add_Switch
580      (Cmd        : in out Command_Line;
581       Switch     : String;
582       Parameter  : String    := "";
583       Separator  : Character := ' ';
584       Section    : String    := "";
585       Add_Before : Boolean   := False);
586    --  Add a new switch to the command line, and combine/group it with existing
587    --  switches if possible. Nothing is done if the switch already exists with
588    --  the same parameter.
589    --
590    --  If the Switch takes a parameter, the latter should be specified
591    --  separately, so that the association between the two is always correctly
592    --  recognized even if the order of switches on the command line changes.
593    --  For instance, you should pass "--check=full" as ("--check", "full") so
594    --  that Remove_Switch below can simply take "--check" in parameter. That
595    --  will automatically remove "full" as well. The value of the parameter is
596    --  never modified by this package.
597    --
598    --  On the other hand, you could decide to simply pass "--check=full" as
599    --  the Switch above, and then pass no parameter. This means that you need
600    --  to pass "--check=full" to Remove_Switch as well.
601    --
602    --  A Switch with a parameter will never be grouped with another switch to
603    --  avoid ambiguities as to who the parameter applies to.
604    --
605    --  Separator is the character that goes between the switches and its
606    --  parameter on the command line. If it is set to ASCII.NUL, then no
607    --  separator is applied, and they are concatenated
608    --
609    --  If the switch is part of a section, then it should be specified so that
610    --  the switch is correctly placed in the command line, and the section
611    --  added if not already present. For example, to add the -g switch into the
612    --  -cargs section, you need to call (Cmd, "-g", Section => "-cargs")
613    --
614    --  Add_Before allows insertion of the switch at the beginning of the
615    --  command line.
616
617    procedure Add_Switch
618      (Cmd        : in out Command_Line;
619       Switch     : String;
620       Parameter  : String    := "";
621       Separator  : Character := ' ';
622       Section    : String    := "";
623       Add_Before : Boolean   := False;
624       Success    : out Boolean);
625    --  Same as above, returning the status of
626    --  the operation
627
628    procedure Remove_Switch
629      (Cmd           : in out Command_Line;
630       Switch        : String;
631       Remove_All    : Boolean := False;
632       Has_Parameter : Boolean := False;
633       Section       : String := "");
634    --  Remove Switch from the command line, and ungroup existing switches if
635    --  necessary.
636    --
637    --  The actual parameter to the switches are ignored. If for instance
638    --  you are removing "-foo", then "-foo param1" and "-foo param2" can
639    --  be removed.
640    --
641    --  If Remove_All is True, then all matching switches are removed, otherwise
642    --  only the first matching one is removed.
643    --
644    --  If Has_Parameter is set to True, then only switches having a parameter
645    --  are removed.
646    --
647    --  If the switch belongs to a section, then this section should be
648    --  specified: Remove_Switch (Cmd_Line, "-g", Section => "-cargs") called
649    --  on the command line "-g -cargs -g" will result in "-g", while if
650    --  called with (Cmd_Line, "-g") this will result in "-cargs -g".
651    --  If Remove_All is set, then both "-g" will be removed.
652
653    procedure Remove_Switch
654      (Cmd           : in out Command_Line;
655       Switch        : String;
656       Remove_All    : Boolean := False;
657       Has_Parameter : Boolean := False;
658       Section       : String  := "";
659       Success       : out Boolean);
660    --  Same as above, reporting the success of the operation (Success is False
661    --  if no switch was removed).
662
663    procedure Remove_Switch
664      (Cmd       : in out Command_Line;
665       Switch    : String;
666       Parameter : String;
667       Section   : String := "");
668    --  Remove a switch with a specific parameter. If Parameter is the empty
669    --  string, then only a switch with no parameter will be removed.
670
671    ---------------
672    -- Iteration --
673    ---------------
674
675    type Command_Line_Iterator is private;
676
677    procedure Start
678      (Cmd      : in out Command_Line;
679       Iter     : in out Command_Line_Iterator;
680       Expanded : Boolean);
681    --  Start iterating over the command line arguments. If Expanded is true,
682    --  then the arguments are not grouped and no alias is used. For instance,
683    --  "-gnatwv" and "-gnatwu" would be returned instead of "-gnatwuv".
684    --
685    --  The iterator becomes invalid if the command line is changed through a
686    --  call to Add_Switch, Remove_Switch or Set_Command_Line.
687
688    function Current_Switch    (Iter : Command_Line_Iterator) return String;
689    function Is_New_Section    (Iter : Command_Line_Iterator) return Boolean;
690    function Current_Section   (Iter : Command_Line_Iterator) return String;
691    function Current_Separator (Iter : Command_Line_Iterator) return String;
692    function Current_Parameter (Iter : Command_Line_Iterator) return String;
693    --  Return the current switch and its parameter (or the empty string if
694    --  there is no parameter or the switch was added through Add_Switch
695    --  without specifying the parameter.
696    --
697    --  Separator is the string that goes between the switch and its separator.
698    --  It could be the empty string if they should be concatenated, or a space
699    --  for instance. When printing, you should not add any other character.
700
701    function Has_More (Iter : Command_Line_Iterator) return Boolean;
702    --  Return True if there are more switches to be returned
703
704    procedure Next (Iter : in out Command_Line_Iterator);
705    --  Move to the next switch
706
707    procedure Free (Cmd : in out Command_Line);
708    --  Free the memory used by Cmd
709
710 private
711
712    Max_Depth : constant := 100;
713    --  Maximum depth of subdirectories
714
715    Max_Path_Length : constant := 1024;
716    --  Maximum length of relative path
717
718    type Depth is range 1 .. Max_Depth;
719
720    type Level is record
721       Name_Last : Natural := 0;
722       Dir       : GNAT.Directory_Operations.Dir_Type;
723    end record;
724
725    type Level_Array is array (Depth) of Level;
726
727    type Section_Number is new Natural range 0 .. 65534;
728    for Section_Number'Size use 16;
729
730    type Parameter_Type is record
731       Arg_Num : Positive;
732       First   : Positive;
733       Last    : Positive;
734       Extra   : Character;
735    end record;
736
737    type Is_Switch_Type is array (Natural range <>) of Boolean;
738    pragma Pack (Is_Switch_Type);
739
740    type Section_Type is array (Natural range <>) of Section_Number;
741    pragma Pack (Section_Type);
742
743    type Expansion_Iterator is limited record
744       Start : Positive := 1;
745       --  Position of the first character of the relative path to check against
746       --  the pattern.
747
748       Dir_Name : String (1 .. Max_Path_Length);
749
750       Current_Depth : Depth := 1;
751
752       Levels : Level_Array;
753
754       Regexp : GNAT.Regexp.Regexp;
755       --  Regular expression built with the pattern
756
757       Maximum_Depth : Depth := 1;
758       --  The maximum depth of directories, reflecting the number of directory
759       --  separators in the pattern.
760    end record;
761
762    type Opt_Parser_Data (Arg_Count : Natural) is record
763       Arguments : GNAT.OS_Lib.Argument_List_Access;
764       --  null if reading from the command line
765
766       The_Parameter : Parameter_Type;
767       The_Separator : Character;
768       The_Switch    : Parameter_Type;
769       --  This type and this variable are provided to store the current switch
770       --  and parameter.
771
772       Is_Switch : Is_Switch_Type (1 .. Arg_Count) := (others => False);
773       --  Indicates wich arguments on the command line are considered not be
774       --  switches or parameters to switches (leaving e.g. filenames,...)
775
776       Section : Section_Type (1 .. Arg_Count) := (others => 1);
777       --  Contains the number of the section associated with the current
778       --  switch. If this number is 0, then it is a section delimiter, which is
779       --  never returned by GetOpt.
780
781       Current_Argument : Natural := 1;
782       --  Number of the current argument parsed on the command line
783
784       Current_Index : Natural := 1;
785       --  Index in the current argument of the character to be processed
786
787       Current_Section : Section_Number := 1;
788
789       Expansion_It : aliased Expansion_Iterator;
790       --  When Get_Argument is expanding a file name, this is the iterator used
791
792       In_Expansion : Boolean := False;
793       --  True if we are expanding a file
794
795       Switch_Character : Character := '-';
796       --  The character at the beginning of the command line arguments,
797       --  indicating the beginning of a switch.
798
799       Stop_At_First : Boolean := False;
800       --  If it is True then Getopt stops at the first non-switch argument
801    end record;
802
803    Command_Line_Parser_Data : aliased Opt_Parser_Data
804      (Ada.Command_Line.Argument_Count);
805    --  The internal data used when parsing the command line
806
807    type Opt_Parser is access all Opt_Parser_Data;
808    Command_Line_Parser : constant Opt_Parser :=
809                            Command_Line_Parser_Data'Access;
810
811    type Command_Line_Configuration_Record is record
812       Prefixes : GNAT.OS_Lib.Argument_List_Access;
813       --  The list of prefixes
814
815       Sections : GNAT.OS_Lib.Argument_List_Access;
816       --  The list of sections
817
818       Aliases    : GNAT.OS_Lib.Argument_List_Access;
819       Expansions : GNAT.OS_Lib.Argument_List_Access;
820       --  The aliases (Both arrays have the same bounds)
821
822       Switches : GNAT.OS_Lib.Argument_List_Access;
823       --  List of expected switches (Used when expanding switch groups)
824    end record;
825    type Command_Line_Configuration is access Command_Line_Configuration_Record;
826
827    type Command_Line is record
828       Config   : Command_Line_Configuration;
829       Expanded : GNAT.OS_Lib.Argument_List_Access;
830
831       Params : GNAT.OS_Lib.Argument_List_Access;
832       --  Parameter for the corresponding switch in Expanded. The first
833       --  character is the separator (or ASCII.NUL if there is no separator).
834
835       Sections : GNAT.OS_Lib.Argument_List_Access;
836       --  The list of sections
837
838       Coalesce          : GNAT.OS_Lib.Argument_List_Access;
839       Coalesce_Params   : GNAT.OS_Lib.Argument_List_Access;
840       Coalesce_Sections : GNAT.OS_Lib.Argument_List_Access;
841       --  Cached version of the command line. This is recomputed every time
842       --  the command line changes. Switches are grouped as much as possible,
843       --  and aliases are used to reduce the length of the command line. The
844       --  parameters are not allocated, they point into Params, so they must
845       --  not be freed.
846    end record;
847
848    type Command_Line_Iterator is record
849       List     : GNAT.OS_Lib.Argument_List_Access;
850       Sections : GNAT.OS_Lib.Argument_List_Access;
851       Params   : GNAT.OS_Lib.Argument_List_Access;
852       Current  : Natural;
853    end record;
854
855 end GNAT.Command_Line;