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