1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
5 -- G N A T . C O M M A N D _ L I N E --
11 -- Copyright (C) 1999-2001 Ada Core Technologies, Inc. --
13 -- GNAT is free software; you can redistribute it and/or modify it under --
14 -- terms of the GNU General Public License as published by the Free Soft- --
15 -- ware Foundation; either version 2, or (at your option) any later ver- --
16 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
19 -- for more details. You should have received a copy of the GNU General --
20 -- Public License distributed with GNAT; see file COPYING. If not, write --
21 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
22 -- MA 02111-1307, USA. --
24 -- As a special exception, if other files instantiate generics from this --
25 -- unit, or you link this unit with other files to produce an executable, --
26 -- this unit does not by itself cause the resulting executable to be --
27 -- covered by the GNU General Public License. This exception does not --
28 -- however invalidate any other reasons why the executable file might be --
29 -- covered by the GNU Public License. --
31 -- GNAT is maintained by Ada Core Technologies Inc (http://www.gnat.com). --
33 ------------------------------------------------------------------------------
35 -- High level package for command line parsing
37 -- This package provides an interface to Ada.Command_Line, to do the
38 -- parsing of command line arguments. Here is a small usage example:
42 -- case Getopt ("a b: ad") is -- Accepts '-a', '-ad', or '-b argument'
43 -- when ASCII.NUL => exit;
46 -- if Full_Switch = "a" then
47 -- Put_Line ("Got a");
49 -- Put_Line ("Got ad");
53 -- Put_Line ("Got b + " & Parameter);
56 -- raise Program_Error; -- cannot occur!
62 -- S : constant String := Get_Argument (Do_Expansion => True);
65 -- exit when S'Length = 0;
66 -- Put_Line ("Got " & S);
71 -- when Invalid_Switch => Put_Line ("Invalid Switch " & Full_Switch);
72 -- when Invalid_Parameter => Put_Line ("No parameter for " & Full_Switch);
75 -- A more complicated example would involve the use of sections for the
76 -- switches, as for instance in gnatmake. These sections are separated by
77 -- special switches, chosen by the programer. Each section act as a
78 -- command line of its own.
81 -- Initialize_Option_Scan ('-', False, "largs bargs cargs");
83 -- -- same loop as above to get switches and arguments
86 -- Goto_Section ("bargs");
88 -- -- same loop as above to get switches and arguments
89 -- -- The supports switches in Get_Opt might be different
92 -- Goto_Section ("cargs");
94 -- -- same loop as above to get switches and arguments
95 -- -- The supports switches in Get_Opt might be different
99 with GNAT
.Directory_Operations
;
102 package GNAT
.Command_Line
is
104 procedure Initialize_Option_Scan
105 (Switch_Char
: Character := '-';
106 Stop_At_First_Non_Switch
: Boolean := False;
107 Section_Delimiters
: String := "");
108 -- This procedure resets the internal state of the package to prepare
109 -- to rescan the parameters. It need not (but may be) called before the
110 -- first use of Getopt, but it must be called if you want to start
111 -- rescanning the command line parameters from the start. The optional
112 -- parameter Switch_Char can be used to reset the switch character,
113 -- e.g. to '/' for use in DOS-like systems. The optional parameter
114 -- Stop_At_First_Non_Switch indicates if Getopt is to look for switches
115 -- on the whole command line, or if it has to stop as soon as a
116 -- non-switch argument is found.
120 -- Arguments: my_application file1 -c
122 -- if Stop_At_First_Non_Switch is False, then -c will be considered
123 -- as a switch (returned by getopt), otherwise it will be considered
124 -- as a normal argument (returned by Get_Argument).
126 -- if SECTION_DELIMITERS is set, then every following subprogram
127 -- (Getopt and Get_Argument) will only operate within a section, which
128 -- is delimited by any of these delimiters or the end of the command line.
131 -- Initialize_Option_Scan ("largs bargs cargs");
133 -- Arguments on command line : my_application -c -bargs -d -e -largs -f
134 -- This line is made of three section, the first one is the default one
135 -- and includes only the '-c' switch, the second one is between -bargs
136 -- and -largs and includes '-d -e' and the last one includes '-f'
138 procedure Goto_Section
(Name
: String := "");
139 -- Change the current section. The next Getopt of Get_Argument will
140 -- start looking at the beginning of the section. An empty name ("")
141 -- refers to the first section between the program name and the first
142 -- section delimiter.
143 -- If the section does not exist, then Invalid_Section is raised.
145 function Full_Switch
return String;
146 -- Returns the full name of the last switch found (Getopt only returns
147 -- the first character)
149 function Getopt
(Switches
: String) return Character;
150 -- This function moves to the next switch on the command line (defined
151 -- as a switch character followed by a character within Switches,
152 -- casing being significant). The result returned is the first
153 -- character of the particular switch located. If there are no more
154 -- switches in the current section, returns ASCII.NUL. The switches
155 -- need not be separated by spaces (they can be concatenated if they do
156 -- not require an argument, e.g. -ab is the same as two separate
159 -- Switches is a string of all the possible switches, separated by a
160 -- space. A switch can be followed by one of the following characters :
162 -- ':' The switch requires a parameter. There can optionally be a space
163 -- on the command line between the switch and its parameter
164 -- '!' The switch requires a parameter, but there can be no space on the
165 -- command line between the switch and its parameter
166 -- '?' The switch may have an optional parameter. There can no space
167 -- between the switch and its argument
168 -- ex/ if Switches has the following value : "a? b"
169 -- The command line can be :
170 -- -afoo : -a switch with 'foo' parameter
171 -- -a foo : -a switch and another element on the
172 -- command line 'foo', returned by Get_Argument
174 -- Example: if Switches is "-a: -aO:", you can have the following
176 -- -aarg : 'a' switch with 'arg' parameter
177 -- -a arg : 'a' switch with 'arg' parameter
178 -- -aOarg : 'aO' switch with 'arg' parameter
179 -- -aO arg : 'aO' switch with 'arg' parameter
183 -- Getopt ("a b: ac ad?")
185 -- accept either 'a' or 'ac' with no argument,
186 -- accept 'b' with a required argument
187 -- accept 'ad' with an optional argument
189 -- If the first item in switches is '*', then Getopt will catch
190 -- every element on the command line that was not caught by any other
191 -- switch. The character returned by GetOpt is '*'
195 -- If the command line is '-a -c toto.o -b', GetOpt will return
196 -- successively 'a', '*', '*' and 'b'. When '*' is returnd,
197 -- Full_Switch returns the corresponding item on the command line.
200 -- When Getopt encounters an invalid switch, it raises the exception
201 -- Invalid_Switch and sets Full_Switch to return the invalid switch.
202 -- When Getopt can not find the parameter associated with a switch, it
203 -- raises Invalid_Parameter, and sets Full_Switch to return the invalid
206 -- Note: in case of ambiguity, e.g. switches a ab abc, then the longest
207 -- matching switch is returned.
209 -- Arbitrary characters are allowed for switches, although it is
210 -- strongly recommanded to use only letters and digits for portability
213 function Get_Argument
(Do_Expansion
: Boolean := False) return String;
214 -- Returns the next element in the command line which is not a switch.
215 -- This function should not be called before Getopt has returned
218 -- If Expansion is True, then the parameter on the command
219 -- line will considered as filename with wild cards, and will be
220 -- expanded. The matching file names will be returned one at a time.
221 -- When there are no more arguments on the command line, this function
222 -- returns an empty string. This is useful in non-Unix systems for
223 -- obtaining normal expansion of wild card references.
225 function Parameter
return String;
226 -- Returns parameter associated with the last switch returned by Getopt.
227 -- If no parameter was associated with the last switch, or no previous
228 -- call has been made to Get_Argument, raises Invalid_Parameter.
229 -- If the last switch was associated with an optional argument and this
230 -- argument was not found on the command line, Parameter returns an empty
233 type Expansion_Iterator
is limited private;
234 -- Type used during expansion of file names
236 procedure Start_Expansion
237 (Iterator
: out Expansion_Iterator
;
239 Directory
: String := "";
240 Basic_Regexp
: Boolean := True);
241 -- Initialize an wild card expansion. The next calls to Expansion will
242 -- return the next file name in Directory which match Pattern (Pattern
243 -- is a regular expression, using only the Unix shell and DOS syntax if
244 -- Basic_Regexp is True. When Directory is an empty string, the current
245 -- directory is searched.
247 function Expansion
(Iterator
: Expansion_Iterator
) return String;
248 -- Return the next file in the directory matching the parameters given
249 -- to Start_Expansion and updates Iterator to point to the next entry.
250 -- Returns an empty string when there are no more files in the directory.
251 -- If Expansion is called again after an empty string has been returned,
252 -- then the exception GNAT.Directory_Operations.Directory_Error is raised.
254 Invalid_Section
: exception;
255 -- Raised when an invalid section is selected by Goto_Section
257 Invalid_Switch
: exception;
258 -- Raised when an invalid switch is detected in the command line
260 Invalid_Parameter
: exception;
261 -- Raised when a parameter is missing, or an attempt is made to obtain
262 -- a parameter for a switch that does not allow a parameter
266 type Expansion_Iterator
is limited record
267 Dir
: GNAT
.Directory_Operations
.Dir_Type
;
268 Regexp
: GNAT
.Regexp
.Regexp
;
271 end GNAT
.Command_Line
;