| 1 |
with Ada.Command_Line; |
| 2 |
with Ada.Exceptions; |
| 3 |
with GNAT.OS_Lib; |
| 4 |
with GNAT.Strings; |
| 5 |
|
| 6 |
with Ada.Containers.Hashed_Maps; |
| 7 |
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded; |
| 8 |
with Ada.Strings.Unbounded.Hash; |
| 9 |
|
| 10 |
with AAA.Table_IO; |
| 11 |
with AAA.Text_IO; |
| 12 |
|
| 13 |
with CLIC.Config.Info; |
| 14 |
with CLIC.Command_Line; use CLIC.Command_Line; |
| 15 |
|
| 16 |
package body CLIC.Subcommand.Instance is |
| 17 |
|
| 18 |
package Command_Maps is new Ada.Containers.Hashed_Maps |
| 19 |
(Key_Type => Ada.Strings.Unbounded.Unbounded_String, |
| 20 |
Element_Type => Command_Access, |
| 21 |
Hash => Ada.Strings.Unbounded.Hash, |
| 22 |
Equivalent_Keys => Ada.Strings.Unbounded."="); |
| 23 |
|
| 24 |
package Topic_Maps is new Ada.Containers.Hashed_Maps |
| 25 |
(Key_Type => Ada.Strings.Unbounded.Unbounded_String, |
| 26 |
Element_Type => Help_Topic_Access, |
| 27 |
Hash => Ada.Strings.Unbounded.Hash, |
| 28 |
Equivalent_Keys => Ada.Strings.Unbounded."="); |
| 29 |
|
| 30 |
package Group_Maps is new Ada.Containers.Hashed_Maps |
| 31 |
(Key_Type => Ada.Strings.Unbounded.Unbounded_String, |
| 32 |
Element_Type => AAA.Strings.Vector, |
| 33 |
"=" => AAA.Strings."=", |
| 34 |
Hash => Ada.Strings.Unbounded.Hash, |
| 35 |
Equivalent_Keys => Ada.Strings.Unbounded."="); |
| 36 |
|
| 37 |
package Alias_Maps is new Ada.Containers.Hashed_Maps |
| 38 |
(Key_Type => Ada.Strings.Unbounded.Unbounded_String, |
| 39 |
Element_Type => AAA.Strings.Vector, |
| 40 |
"=" => AAA.Strings."=", |
| 41 |
Hash => Ada.Strings.Unbounded.Hash, |
| 42 |
Equivalent_Keys => Ada.Strings.Unbounded."="); |
| 43 |
|
| 44 |
Registered_Commands : Command_Maps.Map; |
| 45 |
-- A map of commands based on their names |
| 46 |
|
| 47 |
Registered_Topics : Topic_Maps.Map; |
| 48 |
-- A map of topics based on their names |
| 49 |
|
| 50 |
Registered_Groups : Group_Maps.Map; |
| 51 |
-- A map of list of commands based on their group names |
| 52 |
|
| 53 |
Registered_Aliases : Alias_Maps.Map; |
| 54 |
-- A map of aliases and their replacement args |
| 55 |
|
| 56 |
Not_In_A_Group : AAA.Strings.Vector; |
| 57 |
-- List of commands that are not in a group |
| 58 |
|
| 59 |
Global_Arguments : AAA.Strings.Vector; |
| 60 |
-- Vector of arguments for the global command, the first one should be the |
| 61 |
-- command name, otherwise there is an invalid global switch on the command |
| 62 |
-- line. |
| 63 |
|
| 64 |
Help_Requested : Boolean; |
| 65 |
|
| 66 |
Global_Config : Switches_Configuration; |
| 67 |
|
| 68 |
Global_Parsing_Done : Boolean := False; |
| 69 |
|
| 70 |
procedure Display_Options |
| 71 |
(Config : Switches_Configuration; |
| 72 |
Title : String); |
| 73 |
procedure Display_Global_Options; |
| 74 |
function Highlight_Switches (Line : String) return String; |
| 75 |
function What_Command (Str : String := "") return not null Command_Access; |
| 76 |
procedure Put_Line_For_Access (Str : String); |
| 77 |
function To_Argument_List (V : AAA.Strings.Vector) |
| 78 |
return GNAT.OS_Lib.Argument_List_Access; |
| 79 |
function To_Vector (List : GNAT.OS_Lib.Argument_List_Access) |
| 80 |
return AAA.Strings.Vector; |
| 81 |
procedure Process_Aliases |
| 82 |
with Pre => not Global_Arguments.Is_Empty; |
| 83 |
|
| 84 |
procedure Split_At_Double_Dash (Args : AAA.Strings.Vector; |
| 85 |
Before, After : out AAA.Strings.Vector) |
| 86 |
with Post => (if not Args.Contains ("--") then Before."=" (Args)) |
| 87 |
and then |
| 88 |
not Before.Contains ("--"); |
| 89 |
-- Split a vector of command line arguments, everything before a potential |
| 90 |
-- first "--" (double dash) is added to the Before output, the arguments |
| 91 |
-- after the "--" are added to the After output. The first "--" is not |
| 92 |
-- added to either the Before or After output. |
| 93 |
-- |
| 94 |
-- When there is no "--" argument, Before is equal to Args. |
| 95 |
|
| 96 |
|
| 97 |
-------------------------- |
| 98 |
-- Split_At_Double_Dash -- |
| 99 |
-------------------------- |
| 100 |
|
| 101 |
procedure Split_At_Double_Dash (Args : AAA.Strings.Vector; |
| 102 |
Before, After : out AAA.Strings.Vector) |
| 103 |
is |
| 104 |
Before_Double_Dash : Boolean := True; |
| 105 |
begin |
| 106 |
for Elt of Args loop |
| 107 |
if Before_Double_Dash then |
| 108 |
if Elt = "--" then |
| 109 |
Before_Double_Dash := False; |
| 110 |
else |
| 111 |
Before.Append (Elt); |
| 112 |
end if; |
| 113 |
else |
| 114 |
After.Append (Elt); |
| 115 |
end if; |
| 116 |
end loop; |
| 117 |
end Split_At_Double_Dash; |
| 118 |
|
| 119 |
------------------------- |
| 120 |
-- Put_Line_For_Access -- |
| 121 |
------------------------- |
| 122 |
|
| 123 |
procedure Put_Line_For_Access (Str : String) is |
| 124 |
begin |
| 125 |
Put_Line (Str); |
| 126 |
end Put_Line_For_Access; |
| 127 |
|
| 128 |
---------------------- |
| 129 |
-- To_Argument_List -- |
| 130 |
---------------------- |
| 131 |
|
| 132 |
function To_Argument_List (V : AAA.Strings.Vector) |
| 133 |
return GNAT.OS_Lib.Argument_List_Access |
| 134 |
is |
| 135 |
List : constant GNAT.OS_Lib.Argument_List_Access := |
| 136 |
new GNAT.Strings.String_List (1 .. V.Count); |
| 137 |
begin |
| 138 |
for Index in List.all'Range loop |
| 139 |
List (Index) := new String'(V (Index)); |
| 140 |
end loop; |
| 141 |
return List; |
| 142 |
end To_Argument_List; |
| 143 |
|
| 144 |
--------------- |
| 145 |
-- To_Vector -- |
| 146 |
--------------- |
| 147 |
|
| 148 |
function To_Vector (List : GNAT.OS_Lib.Argument_List_Access) |
| 149 |
return AAA.Strings.Vector |
| 150 |
is |
| 151 |
use GNAT.OS_Lib; |
| 152 |
|
| 153 |
Result : AAA.Strings.Vector; |
| 154 |
begin |
| 155 |
if List /= null then |
| 156 |
for Elt of List.all loop |
| 157 |
if Elt /= null then |
| 158 |
Result.Append (Elt.all); |
| 159 |
end if; |
| 160 |
end loop; |
| 161 |
end if; |
| 162 |
return Result; |
| 163 |
end To_Vector; |
| 164 |
|
| 165 |
-------------- |
| 166 |
-- Register -- |
| 167 |
-------------- |
| 168 |
|
| 169 |
procedure Register (Cmd : not null Command_Access) is |
| 170 |
Name : constant Unbounded_String := To_Unbounded_String (Cmd.Name); |
| 171 |
begin |
| 172 |
if Registered_Commands.Contains (Name) then |
| 173 |
raise Command_Already_Defined with Cmd.Name; |
| 174 |
else |
| 175 |
-- Register the command |
| 176 |
Registered_Commands.Insert (Name, Cmd); |
| 177 |
|
| 178 |
-- This command is not in a group |
| 179 |
Not_In_A_Group.Append (Cmd.Name); |
| 180 |
end if; |
| 181 |
end Register; |
| 182 |
|
| 183 |
-------------- |
| 184 |
-- Register -- |
| 185 |
-------------- |
| 186 |
|
| 187 |
procedure Register (Group : String; Cmd : not null Command_Access) is |
| 188 |
Name : constant Unbounded_String := To_Unbounded_String (Cmd.Name); |
| 189 |
Ugroup : constant Unbounded_String := To_Unbounded_String (Group); |
| 190 |
begin |
| 191 |
if Registered_Commands.Contains (Name) then |
| 192 |
raise Command_Already_Defined with Cmd.Name; |
| 193 |
else |
| 194 |
-- Register the command |
| 195 |
Registered_Commands.Insert (Name, Cmd); |
| 196 |
|
| 197 |
-- Create the group if it doesn't exist yet |
| 198 |
if not Registered_Groups.Contains (Ugroup) then |
| 199 |
Registered_Groups.Insert (Ugroup, AAA.Strings.Empty_Vector); |
| 200 |
end if; |
| 201 |
|
| 202 |
-- Add this command to the list of commands for the group |
| 203 |
Registered_Groups.Reference (Ugroup).Append (Cmd.Name); |
| 204 |
end if; |
| 205 |
end Register; |
| 206 |
|
| 207 |
-------------- |
| 208 |
-- Register -- |
| 209 |
-------------- |
| 210 |
|
| 211 |
procedure Register (Topic : not null Help_Topic_Access) is |
| 212 |
Name : constant Unbounded_String := To_Unbounded_String (Topic.Name); |
| 213 |
begin |
| 214 |
if Registered_Topics.Contains (Name) then |
| 215 |
raise Program_Error; |
| 216 |
else |
| 217 |
Registered_Topics.Insert (Name, Topic); |
| 218 |
end if; |
| 219 |
end Register; |
| 220 |
|
| 221 |
--------------- |
| 222 |
-- Set_Alias -- |
| 223 |
--------------- |
| 224 |
|
| 225 |
procedure Set_Alias (Alias : Identifier; Replacement : AAA.Strings.Vector) |
| 226 |
is |
| 227 |
Name : constant Unbounded_String := To_Unbounded_String (Alias); |
| 228 |
begin |
| 229 |
Registered_Aliases.Include (Name, Replacement); |
| 230 |
end Set_Alias; |
| 231 |
|
| 232 |
------------------ |
| 233 |
-- Load_Aliases -- |
| 234 |
------------------ |
| 235 |
|
| 236 |
procedure Load_Aliases (Conf : CLIC.Config.Instance; |
| 237 |
Root_Key : String := "alias") |
| 238 |
is |
| 239 |
Keys : constant AAA.Strings.Vector := |
| 240 |
CLIC.Config.Info.List_Keys (Conf, "^" & Root_Key & "\.[^.]+$"); |
| 241 |
begin |
| 242 |
for Key of Keys loop |
| 243 |
declare |
| 244 |
Alias : constant String := AAA.Strings.Tail (Key, '.'); |
| 245 |
|
| 246 |
Value : constant String := Conf.Get (Key, Default => ""); |
| 247 |
|
| 248 |
List : GNAT.OS_Lib.String_List_Access |
| 249 |
:= GNAT.OS_Lib.Argument_String_To_List (Value); |
| 250 |
begin |
| 251 |
if Value /= "" then |
| 252 |
Set_Alias (Alias, To_Vector (List)); |
| 253 |
end if; |
| 254 |
GNAT.OS_Lib.Free (List); |
| 255 |
end; |
| 256 |
end loop; |
| 257 |
end Load_Aliases; |
| 258 |
|
| 259 |
------------------ |
| 260 |
-- What_Command -- |
| 261 |
------------------ |
| 262 |
|
| 263 |
function What_Command (Str : String := "") return not null Command_Access is |
| 264 |
Name : constant Unbounded_String := |
| 265 |
To_Unbounded_String (if Str = "" then What_Command else Str); |
| 266 |
begin |
| 267 |
if Registered_Commands.Contains (Name) then |
| 268 |
return Registered_Commands.Element (Name); |
| 269 |
else |
| 270 |
raise Error_No_Command; |
| 271 |
end if; |
| 272 |
end What_Command; |
| 273 |
|
| 274 |
------------------ |
| 275 |
-- What_Command -- |
| 276 |
------------------ |
| 277 |
|
| 278 |
function What_Command return String is |
| 279 |
begin |
| 280 |
if Global_Arguments.Is_Empty |
| 281 |
or else |
| 282 |
AAA.Strings.Has_Prefix (Global_Arguments.First_Element, "-") |
| 283 |
then |
| 284 |
raise Error_No_Command; |
| 285 |
else |
| 286 |
return Global_Arguments.First_Element; |
| 287 |
end if; |
| 288 |
end What_Command; |
| 289 |
|
| 290 |
---------------------------- |
| 291 |
-- Display_Valid_Commands -- |
| 292 |
---------------------------- |
| 293 |
|
| 294 |
procedure Display_Valid_Commands is |
| 295 |
use Command_Maps; |
| 296 |
use Group_Maps; |
| 297 |
|
| 298 |
Tab : constant String (1 .. 1) := (others => ' '); |
| 299 |
Table : AAA.Table_IO.Table; |
| 300 |
|
| 301 |
----------------- |
| 302 |
-- Add_Command -- |
| 303 |
----------------- |
| 304 |
|
| 305 |
procedure Add_Command (Cmd : not null Command_Access) is |
| 306 |
begin |
| 307 |
Table.New_Row; |
| 308 |
Table.Append (Tab); |
| 309 |
Table.Append (TTY_Description (Cmd.Name)); |
| 310 |
Table.Append (Tab); |
| 311 |
Table.Append (Cmd.Short_Description); |
| 312 |
end Add_Command; |
| 313 |
|
| 314 |
First_Group : Boolean := Not_In_A_Group.Is_Empty; |
| 315 |
begin |
| 316 |
if Registered_Commands.Is_Empty then |
| 317 |
return; |
| 318 |
end if; |
| 319 |
|
| 320 |
Put_Line (""); |
| 321 |
Put_Line (TTY_Chapter ("COMMANDS")); |
| 322 |
|
| 323 |
for Name of Not_In_A_Group loop |
| 324 |
Add_Command (Registered_Commands (To_Unbounded_String (Name))); |
| 325 |
end loop; |
| 326 |
|
| 327 |
for Iter in Registered_Groups.Iterate loop |
| 328 |
|
| 329 |
if not First_Group then |
| 330 |
Table.New_Row; |
| 331 |
Table.Append (Tab); |
| 332 |
else |
| 333 |
First_Group := False; |
| 334 |
end if; |
| 335 |
|
| 336 |
declare |
| 337 |
Group : constant String := To_String (Key (Iter)); |
| 338 |
begin |
| 339 |
Table.New_Row; |
| 340 |
Table.Append (Tab); |
| 341 |
Table.Append (TTY_Underline (Group)); |
| 342 |
for Name of Element (Iter) loop |
| 343 |
Add_Command (Registered_Commands (To_Unbounded_String (Name))); |
| 344 |
end loop; |
| 345 |
end; |
| 346 |
end loop; |
| 347 |
|
| 348 |
Table.Print (Separator => " ", Put_Line => Put_Line_For_Access'Access); |
| 349 |
end Display_Valid_Commands; |
| 350 |
|
| 351 |
-------------------------- |
| 352 |
-- Display_Valid_Topics -- |
| 353 |
-------------------------- |
| 354 |
|
| 355 |
procedure Display_Valid_Topics is |
| 356 |
Tab : constant String (1 .. 1) := (others => ' '); |
| 357 |
Table : AAA.Table_IO.Table; |
| 358 |
use Topic_Maps; |
| 359 |
|
| 360 |
begin |
| 361 |
if Registered_Topics.Is_Empty then |
| 362 |
return; |
| 363 |
end if; |
| 364 |
|
| 365 |
Put_Line (""); |
| 366 |
Put_Line (TTY_Chapter ("TOPICS")); |
| 367 |
|
| 368 |
for Elt in Registered_Topics.Iterate loop |
| 369 |
Table.New_Row; |
| 370 |
Table.Append (Tab); |
| 371 |
Table.Append (TTY_Description (To_String (Key (Elt)))); |
| 372 |
Table.Append (Tab); |
| 373 |
Table.Append (Element (Elt).Title); |
| 374 |
end loop; |
| 375 |
|
| 376 |
Table.Print (Separator => " ", Put_Line => Put_Line_For_Access'Access); |
| 377 |
end Display_Valid_Topics; |
| 378 |
|
| 379 |
--------------------- |
| 380 |
-- Display_Aliases -- |
| 381 |
--------------------- |
| 382 |
|
| 383 |
procedure Display_Aliases is |
| 384 |
Tab : constant String (1 .. 1) := (others => ' '); |
| 385 |
Table : AAA.Table_IO.Table; |
| 386 |
use Alias_Maps; |
| 387 |
|
| 388 |
begin |
| 389 |
if Registered_Aliases.Is_Empty then |
| 390 |
return; |
| 391 |
end if; |
| 392 |
|
| 393 |
Put_Line (TTY_Chapter ("ALIASES")); |
| 394 |
|
| 395 |
for Elt in Registered_Aliases.Iterate loop |
| 396 |
Table.New_Row; |
| 397 |
Table.Append (Tab); |
| 398 |
Table.Append (TTY_Description (To_String (Key (Elt)))); |
| 399 |
Table.Append (Tab); |
| 400 |
Table.Append (Element (Elt).Flatten); |
| 401 |
end loop; |
| 402 |
|
| 403 |
Table.Print (Separator => " ", Put_Line => Put_Line_For_Access'Access); |
| 404 |
end Display_Aliases; |
| 405 |
|
| 406 |
------------------- |
| 407 |
-- Display_Usage -- |
| 408 |
------------------- |
| 409 |
|
| 410 |
procedure Display_Usage (Cmd : not null Command_Access) is |
| 411 |
Config : Switches_Configuration; |
| 412 |
Canary1 : Switches_Configuration; |
| 413 |
Canary2 : Switches_Configuration; |
| 414 |
begin |
| 415 |
Put_Line (TTY_Chapter ("SUMMARY")); |
| 416 |
Put_Line (" " & Cmd.Short_Description); |
| 417 |
|
| 418 |
Put_Line (""); |
| 419 |
Put_Line (TTY_Chapter ("USAGE")); |
| 420 |
Put (" "); |
| 421 |
Put_Line |
| 422 |
(TTY_Underline (Main_Command_Name) & |
| 423 |
" " & |
| 424 |
TTY_Underline (Cmd.Name) & |
| 425 |
" [options] " & |
| 426 |
Cmd.Usage_Custom_Parameters); |
| 427 |
|
| 428 |
-- We use the following two canaries to detect if a command is adding |
| 429 |
-- its own switches, in which case we need to show their specific help. |
| 430 |
|
| 431 |
Set_Global_Switches (Canary1); -- For comparison |
| 432 |
Set_Global_Switches (Canary2); -- For comparison |
| 433 |
Cmd.Setup_Switches (Canary1); |
| 434 |
|
| 435 |
if Get_Switches (Canary1.GNAT_Cfg) /= Get_Switches (Canary2.GNAT_Cfg) |
| 436 |
then |
| 437 |
Cmd.Setup_Switches (Config); |
| 438 |
end if; |
| 439 |
|
| 440 |
-- Without the following line, GNAT.Display_Help causes a segfault for |
| 441 |
-- reasons I'm unable to pinpoint. This way it prints a harmless blank |
| 442 |
-- line that we want anyway. |
| 443 |
|
| 444 |
Define_Switch (Config, " ", " ", " ", " ", " "); |
| 445 |
|
| 446 |
Display_Options (Config, "OPTIONS"); |
| 447 |
|
| 448 |
Display_Global_Options; |
| 449 |
|
| 450 |
-- Format and print the long command description |
| 451 |
Put_Line (""); |
| 452 |
Put_Line (TTY_Chapter ("DESCRIPTION")); |
| 453 |
|
| 454 |
for Line of Cmd.Long_Description loop |
| 455 |
AAA.Text_IO.Put_Paragraph (Highlight_Switches (Line), |
| 456 |
Line_Prefix => " "); |
| 457 |
-- GNATCOLL.Paragraph_Filling seems buggy at the moment, otherwise |
| 458 |
-- it would be the logical choice. |
| 459 |
end loop; |
| 460 |
end Display_Usage; |
| 461 |
|
| 462 |
------------------- |
| 463 |
-- Display_Usage -- |
| 464 |
------------------- |
| 465 |
|
| 466 |
procedure Display_Usage (Displayed_Error : Boolean := False) is |
| 467 |
begin |
| 468 |
if not Displayed_Error then |
| 469 |
Put_Line (Main_Command_Name & " " & TTY_Version (Version)); |
| 470 |
Put_Line (""); |
| 471 |
end if; |
| 472 |
|
| 473 |
Put_Line (TTY_Chapter ("USAGE")); |
| 474 |
Put_Line (" " & TTY_Underline (Main_Command_Name) & |
| 475 |
" [global options] " & |
| 476 |
"<command> [command options] [<arguments>]"); |
| 477 |
Put_Line (""); |
| 478 |
Put_Line (" " & TTY_Underline (Main_Command_Name) & " " & |
| 479 |
TTY_Underline ("help") & |
| 480 |
" [<command>|<topic>]"); |
| 481 |
|
| 482 |
Put_Line (""); |
| 483 |
Put_Line (TTY_Chapter ("ARGUMENTS")); |
| 484 |
declare |
| 485 |
Tab : constant String (1 .. 1) := (others => ' '); |
| 486 |
Table : AAA.Table_IO.Table; |
| 487 |
begin |
| 488 |
Table.New_Row; |
| 489 |
Table.Append (Tab); |
| 490 |
Table.Append (TTY_Description ("<command>")); |
| 491 |
Table.Append ("Command to execute"); |
| 492 |
|
| 493 |
Table.New_Row; |
| 494 |
Table.Append (Tab); |
| 495 |
Table.Append (TTY_Description ("<arguments>")); |
| 496 |
Table.Append ("List of arguments for the command"); |
| 497 |
|
| 498 |
Table.Print (Separator => " ", |
| 499 |
Put_Line => Put_Line_For_Access'Access); |
| 500 |
end; |
| 501 |
|
| 502 |
Display_Global_Options; |
| 503 |
Display_Valid_Commands; |
| 504 |
Display_Valid_Topics; |
| 505 |
Display_Aliases; |
| 506 |
end Display_Usage; |
| 507 |
|
| 508 |
--------------------------- |
| 509 |
-- Parse_Global_Switches -- |
| 510 |
--------------------------- |
| 511 |
|
| 512 |
procedure Parse_Global_Switches |
| 513 |
(Command_Line : AAA.Strings.Vector := AAA.Strings.Empty_Vector) |
| 514 |
is |
| 515 |
|
| 516 |
---------------------- |
| 517 |
-- Ada_Command_Line -- |
| 518 |
---------------------- |
| 519 |
|
| 520 |
function Ada_Command_Line return AAA.Strings.Vector is |
| 521 |
use Ada.Command_Line; |
| 522 |
|
| 523 |
Result : AAA.Strings.Vector; |
| 524 |
begin |
| 525 |
for I in 1 .. Argument_Count loop |
| 526 |
Result.Append (Ada.Command_Line.Argument (I)); |
| 527 |
end loop; |
| 528 |
return Result; |
| 529 |
end Ada_Command_Line; |
| 530 |
|
| 531 |
---------------------- |
| 532 |
-- Filter_Arguments -- |
| 533 |
---------------------- |
| 534 |
|
| 535 |
function Filter_Arguments return GNAT.OS_Lib.Argument_List_Access is |
| 536 |
Arguments : AAA.Strings.Vector; |
| 537 |
|
| 538 |
Cmd_Line : constant AAA.Strings.Vector := |
| 539 |
(if Command_Line.Is_Empty then Ada_Command_Line else Command_Line); |
| 540 |
|
| 541 |
begin |
| 542 |
-- GNAT switch handling intercepts -h/--help. To have the same output |
| 543 |
-- for '<main> -h command' and '<main> help command', we do manual |
| 544 |
for Arg of Cmd_Line loop |
| 545 |
if Arg not in "-h" | "--help" then |
| 546 |
Arguments.Append (Arg); |
| 547 |
else |
| 548 |
Help_Requested := True; |
| 549 |
end if; |
| 550 |
end loop; |
| 551 |
return To_Argument_List (Arguments); |
| 552 |
end Filter_Arguments; |
| 553 |
|
| 554 |
Arguments : GNAT.OS_Lib.Argument_List_Access; |
| 555 |
Parser : Opt_Parser; |
| 556 |
begin |
| 557 |
|
| 558 |
-- Only do the global parsing once |
| 559 |
if Global_Parsing_Done and then Command_Line.Is_Empty then |
| 560 |
return; |
| 561 |
else |
| 562 |
Global_Arguments.Clear; |
| 563 |
Help_Requested := False; |
| 564 |
Clear (Global_Config); |
| 565 |
Global_Parsing_Done := True; |
| 566 |
end if; |
| 567 |
|
| 568 |
-- We filter the command line arguments to remove -h/--help that would |
| 569 |
-- trigger the Getopt automatic help system. |
| 570 |
Arguments := Filter_Arguments; |
| 571 |
|
| 572 |
Set_Global_Switches (Global_Config); |
| 573 |
|
| 574 |
-- Run the parser first with only the global switches. With the |
| 575 |
-- Stop_At_First_Non_Switch => True the parser will stop at the first |
| 576 |
-- argument, witch is supposed to be a sub-command, topic, or alias. |
| 577 |
-- The rest of the command line args are added to the Global_Arguments. |
| 578 |
Initialize_Option_Scan (Parser, |
| 579 |
Arguments, |
| 580 |
Stop_At_First_Non_Switch => True); |
| 581 |
|
| 582 |
Getopt (Global_Config.GNAT_Cfg, |
| 583 |
Parser => Parser, |
| 584 |
Quiet => True); |
| 585 |
|
| 586 |
-- Make a vector of arguments starting from the first non switch |
| 587 |
loop |
| 588 |
declare |
| 589 |
Arg : constant String := |
| 590 |
CLIC.Command_Line.Get_Argument (Parser => Parser); |
| 591 |
begin |
| 592 |
exit when Arg = ""; |
| 593 |
Global_Arguments.Append (Arg); |
| 594 |
end; |
| 595 |
end loop; |
| 596 |
|
| 597 |
GNAT.OS_Lib.Free (Arguments); |
| 598 |
|
| 599 |
-- At this point the command and all unknown switches are in |
| 600 |
-- Global_Arguments. |
| 601 |
|
| 602 |
exception |
| 603 |
-- Getopt should never raise Exit_From_Command_Line because we filtered |
| 604 |
-- the -h/--help switches from the command line. |
| 605 |
|
| 606 |
when E : Invalid_Switch | Invalid_Parameter => |
| 607 |
Put_Error (Ada.Exceptions.Exception_Message (E) & |
| 608 |
" (global)."); |
| 609 |
Put_Error ("try """ & Main_Command_Name & |
| 610 |
" --help"" for more information."); |
| 611 |
Error_Exit (1); |
| 612 |
end Parse_Global_Switches; |
| 613 |
|
| 614 |
--------------------- |
| 615 |
-- Process_Aliases -- |
| 616 |
--------------------- |
| 617 |
|
| 618 |
procedure Process_Aliases is |
| 619 |
Name : constant String := Global_Arguments.First_Element; |
| 620 |
Uname : constant Unbounded_String := To_Unbounded_String (Name); |
| 621 |
begin |
| 622 |
|
| 623 |
-- If we have a valid alias |
| 624 |
if Registered_Aliases.Contains (Uname) then |
| 625 |
|
| 626 |
-- Remove the alias from the arguments |
| 627 |
Global_Arguments.Delete_First; |
| 628 |
|
| 629 |
-- And replace it by the alias value |
| 630 |
Global_Arguments.Prepend (Registered_Aliases (Uname)); |
| 631 |
end if; |
| 632 |
end Process_Aliases; |
| 633 |
|
| 634 |
------------- |
| 635 |
-- Execute -- |
| 636 |
------------- |
| 637 |
|
| 638 |
procedure Execute |
| 639 |
(Command_Line : AAA.Strings.Vector := AAA.Strings.Empty_Vector) |
| 640 |
is |
| 641 |
Parser : Opt_Parser; |
| 642 |
-- Subcommand parser, declared here so it is available to the exception |
| 643 |
-- handler below. |
| 644 |
begin |
| 645 |
|
| 646 |
Parse_Global_Switches (Command_Line); |
| 647 |
|
| 648 |
if Global_Arguments.Is_Empty then |
| 649 |
-- We should at least have the sub-command name in the arguments |
| 650 |
Display_Usage; |
| 651 |
Error_Exit (1); |
| 652 |
|
| 653 |
elsif Help_Requested then |
| 654 |
Display_Help (Global_Arguments.First_Element); |
| 655 |
Error_Exit (0); |
| 656 |
end if; |
| 657 |
|
| 658 |
-- Take care of a potential alias |
| 659 |
Process_Aliases; |
| 660 |
|
| 661 |
-- Dispatch to the appropriate command |
| 662 |
|
| 663 |
declare |
| 664 |
Cmd : constant not null Command_Access := What_Command; |
| 665 |
-- Might raise if invalid, if so we are done |
| 666 |
|
| 667 |
Command_Config : Switches_Configuration; |
| 668 |
|
| 669 |
Sub_Cmd_Line : GNAT.OS_Lib.String_List_Access; |
| 670 |
|
| 671 |
To_Parse, Sub_Arguments : AAA.Strings.Vector; |
| 672 |
begin |
| 673 |
|
| 674 |
-- Add command specific switches to the config. We don't need the |
| 675 |
-- global switches because they have been parsed before. |
| 676 |
Cmd.Setup_Switches (Command_Config); |
| 677 |
|
| 678 |
case Cmd.Switch_Parsing is |
| 679 |
when Parse_All => |
| 680 |
-- Parse all global arguments |
| 681 |
To_Parse := Global_Arguments; |
| 682 |
Sub_Arguments := AAA.Strings.Empty_Vector; |
| 683 |
|
| 684 |
when Before_Double_Dash => |
| 685 |
-- Only parse the arguments before a potential "--" |
| 686 |
Split_At_Double_Dash (Global_Arguments, |
| 687 |
Before => To_Parse, |
| 688 |
After => Sub_Arguments); |
| 689 |
|
| 690 |
when All_As_Args => |
| 691 |
-- Skip all sub-command switch parsing |
| 692 |
To_Parse := AAA.Strings.Empty_Vector; |
| 693 |
Sub_Arguments := Global_Arguments; |
| 694 |
|
| 695 |
end case; |
| 696 |
|
| 697 |
if not To_Parse.Is_Empty then |
| 698 |
|
| 699 |
-- Make a new command line argument list from the remaining |
| 700 |
-- arguments and switches after global parsing. |
| 701 |
Sub_Cmd_Line := To_Argument_List (To_Parse); |
| 702 |
|
| 703 |
-- Initialize a new switch parser that will only see the new |
| 704 |
-- sub-command line (i.e. the remaining args and switches after |
| 705 |
-- global parsing). |
| 706 |
Initialize_Option_Scan (Parser, Sub_Cmd_Line); |
| 707 |
|
| 708 |
-- Parse sub-command line, invalid switches will raise an |
| 709 |
-- exception |
| 710 |
Getopt (Command_Config.GNAT_Cfg, |
| 711 |
Parser => Parser, |
| 712 |
Quiet => True); |
| 713 |
|
| 714 |
-- Make a vector of arguments for the sub-command (every element |
| 715 |
-- that was not a switch in the sub-command line). |
| 716 |
declare |
| 717 |
Args_After_Parsing : AAA.Strings.Vector; |
| 718 |
begin |
| 719 |
loop |
| 720 |
declare |
| 721 |
Arg : constant String := |
| 722 |
CLIC.Command_Line.Get_Argument (Parser => Parser); |
| 723 |
begin |
| 724 |
exit when Arg = ""; |
| 725 |
Args_After_Parsing.Append (Arg); |
| 726 |
end; |
| 727 |
end loop; |
| 728 |
|
| 729 |
Sub_Arguments.Prepend (Args_After_Parsing); |
| 730 |
end; |
| 731 |
|
| 732 |
-- We don't need this anymore |
| 733 |
GNAT.OS_Lib.Free (Sub_Cmd_Line); |
| 734 |
|
| 735 |
end if; |
| 736 |
|
| 737 |
pragma Assert (not Sub_Arguments.Is_Empty, |
| 738 |
"Should have at least the command name"); |
| 739 |
|
| 740 |
-- Remove the sub-command name from the list |
| 741 |
Sub_Arguments.Delete_First; |
| 742 |
Cmd.Execute (Sub_Arguments); |
| 743 |
end; |
| 744 |
|
| 745 |
exception |
| 746 |
-- Getopt should never raise Exit_From_Command_Line because we filtered |
| 747 |
-- the -h/--help switches from the command line. |
| 748 |
|
| 749 |
when E : Invalid_Switch | Invalid_Parameter => |
| 750 |
Put_Error (Ada.Exceptions.Exception_Message (E) & |
| 751 |
" (command/topic """ & |
| 752 |
What_Command & """)."); |
| 753 |
Put_Error ("try """ & Main_Command_Name & |
| 754 |
" help " & What_Command & """ for more information."); |
| 755 |
if Is_Global_Switch (Full_Switch (Parser)) then |
| 756 |
Put_Line ("Option '" & Full_Switch (Parser) |
| 757 |
& "' is a valid global option, to use it as such please" |
| 758 |
& " give it before the subcommand."); |
| 759 |
end if; |
| 760 |
Error_Exit (1); |
| 761 |
|
| 762 |
when Error_No_Command => |
| 763 |
Put_Error ("Unrecognized command: " & Global_Arguments.First_Element); |
| 764 |
Put_Line (""); |
| 765 |
Display_Usage (Displayed_Error => True); |
| 766 |
Error_Exit (1); |
| 767 |
end Execute; |
| 768 |
|
| 769 |
------------------------ |
| 770 |
-- Highlight_Switches -- |
| 771 |
------------------------ |
| 772 |
|
| 773 |
function Highlight_Switches (Line : String) return String is |
| 774 |
|
| 775 |
use AAA.Strings.Vectors; |
| 776 |
use AAA.Strings; |
| 777 |
|
| 778 |
--------------- |
| 779 |
-- Highlight -- |
| 780 |
--------------- |
| 781 |
-- Avoid highlighting non-alphanumeric characters |
| 782 |
function Highlight (Word : String) return String is |
| 783 |
subtype Valid_Chars is Character with |
| 784 |
Dynamic_Predicate => Valid_Chars in '0' .. '9' | 'a' .. 'z'; |
| 785 |
I : Natural := Word'Last; -- last char to highlight |
| 786 |
begin |
| 787 |
while I >= Word'First and then Word (I) not in Valid_Chars loop |
| 788 |
I := I - 1; |
| 789 |
end loop; |
| 790 |
|
| 791 |
return TTY_Emph (Word (Word'First .. I)) & Word (I + 1 .. Word'Last); |
| 792 |
end Highlight; |
| 793 |
|
| 794 |
Words : AAA.Strings.Vector := AAA.Strings.Split (Line, Separator => ' '); |
| 795 |
I, J : Vectors.Cursor; |
| 796 |
begin |
| 797 |
I := Words.First; |
| 798 |
while Has_Element (I) loop |
| 799 |
declare |
| 800 |
Word : constant String := Element (I); |
| 801 |
begin |
| 802 |
J := Next (I); |
| 803 |
if Has_Prefix (Word, "--") and then Word'Length > 2 |
| 804 |
then |
| 805 |
Words.Insert (Before => J, New_Item => Highlight (Word)); |
| 806 |
Words.Delete (I); |
| 807 |
end if; |
| 808 |
I := J; |
| 809 |
end; |
| 810 |
end loop; |
| 811 |
return Flatten (Words); |
| 812 |
end Highlight_Switches; |
| 813 |
|
| 814 |
--------------------- |
| 815 |
-- Display_Options -- |
| 816 |
--------------------- |
| 817 |
|
| 818 |
procedure Display_Options |
| 819 |
(Config : Switches_Configuration; |
| 820 |
Title : String) |
| 821 |
is |
| 822 |
Tab : constant String (1 .. 1) := (others => ' '); |
| 823 |
Table : AAA.Table_IO.Table; |
| 824 |
|
| 825 |
Has_Printable_Rows : Boolean := False; |
| 826 |
|
| 827 |
----------------- |
| 828 |
-- Without_Arg -- |
| 829 |
----------------- |
| 830 |
|
| 831 |
function Without_Arg (Value : String) return String is |
| 832 |
Required_Character : constant Character := Value (Value'Last); |
| 833 |
begin |
| 834 |
return |
| 835 |
(if Required_Character in '=' | ':' | '!' | '?' then |
| 836 |
Value (Value'First .. Value'Last - 1) |
| 837 |
else |
| 838 |
Value); |
| 839 |
end Without_Arg; |
| 840 |
|
| 841 |
-------------- |
| 842 |
-- With_Arg -- |
| 843 |
-------------- |
| 844 |
|
| 845 |
function With_Arg (Value, Arg : String) return String is |
| 846 |
Required_Character : constant Character := Value (Value'Last); |
| 847 |
begin |
| 848 |
return |
| 849 |
(if Required_Character in '=' | ':' | '!' | '?' then |
| 850 |
AAA.Strings.Replace |
| 851 |
(Value, |
| 852 |
"" & Required_Character, |
| 853 |
(case Required_Character is |
| 854 |
when '=' => "=" & Arg, |
| 855 |
when ':' => "[ ] " & Arg, |
| 856 |
when '!' => Arg, |
| 857 |
when '?' => "[" & Arg & "]", |
| 858 |
when others => raise Program_Error)) |
| 859 |
else Value); |
| 860 |
end With_Arg; |
| 861 |
|
| 862 |
--------------- |
| 863 |
-- Print_Row -- |
| 864 |
--------------- |
| 865 |
|
| 866 |
procedure Print_Row (Short_Switch, Long_Switch, Arg, Help : String) is |
| 867 |
Has_Short : constant Boolean := Short_Switch not in " " | ""; |
| 868 |
Has_Long : constant Boolean := Long_Switch not in " " | ""; |
| 869 |
begin |
| 870 |
if not Has_Short and then not Has_Long then |
| 871 |
return; |
| 872 |
end if; |
| 873 |
|
| 874 |
Table.New_Row; |
| 875 |
Table.Append (Tab); |
| 876 |
|
| 877 |
if Has_Short and Has_Long then |
| 878 |
Table.Append (TTY_Description (Without_Arg (Short_Switch)) & |
| 879 |
" (" & With_Arg (Long_Switch, Arg) & ")"); |
| 880 |
elsif not Has_Short and Has_Long then |
| 881 |
Table.Append (TTY_Description (With_Arg (Long_Switch, Arg))); |
| 882 |
elsif Has_Short and not Has_Long then |
| 883 |
Table.Append (TTY_Description (With_Arg (Short_Switch, Arg))); |
| 884 |
end if; |
| 885 |
|
| 886 |
Table.Append (Help); |
| 887 |
|
| 888 |
Has_Printable_Rows := True; |
| 889 |
end Print_Row; |
| 890 |
begin |
| 891 |
for Elt of Config.Info loop |
| 892 |
Print_Row (To_String (Elt.Switch), |
| 893 |
To_String (Elt.Long_Switch), |
| 894 |
To_String (Elt.Argument), |
| 895 |
To_String (Elt.Help)); |
| 896 |
end loop; |
| 897 |
|
| 898 |
if Has_Printable_Rows then |
| 899 |
Put_Line (""); |
| 900 |
Put_Line (TTY_Chapter (Title)); |
| 901 |
|
| 902 |
Table.Print (Separator => " ", |
| 903 |
Put_Line => Put_Line_For_Access'Access); |
| 904 |
end if; |
| 905 |
end Display_Options; |
| 906 |
|
| 907 |
---------------------------- |
| 908 |
-- Display_Global_Options -- |
| 909 |
---------------------------- |
| 910 |
|
| 911 |
procedure Display_Global_Options is |
| 912 |
Global_Config : Switches_Configuration; |
| 913 |
begin |
| 914 |
Set_Global_Switches (Global_Config); |
| 915 |
Display_Options (Global_Config, "GLOBAL OPTIONS"); |
| 916 |
end Display_Global_Options; |
| 917 |
|
| 918 |
------------------ |
| 919 |
-- Display_Help -- |
| 920 |
------------------ |
| 921 |
|
| 922 |
procedure Display_Help (Keyword : String) is |
| 923 |
|
| 924 |
------------ |
| 925 |
-- Format -- |
| 926 |
------------ |
| 927 |
|
| 928 |
procedure Format (Text : AAA.Strings.Vector) is |
| 929 |
begin |
| 930 |
for Line of Text loop |
| 931 |
AAA.Text_IO.Put_Paragraph (Highlight_Switches (Line), |
| 932 |
Line_Prefix => " "); |
| 933 |
end loop; |
| 934 |
end Format; |
| 935 |
|
| 936 |
Ukey : constant Unbounded_String := To_Unbounded_String (Keyword); |
| 937 |
begin |
| 938 |
if Registered_Commands.Contains (Ukey) then |
| 939 |
Display_Usage (What_Command (Keyword)); |
| 940 |
|
| 941 |
elsif Registered_Topics.Contains (Ukey) then |
| 942 |
Put_Line (TTY_Chapter (Registered_Topics.Element (Ukey).Title)); |
| 943 |
Format (Registered_Topics.Element (Ukey).Content); |
| 944 |
elsif Registered_Aliases.Contains (Ukey) then |
| 945 |
Put_Line ("'" & Keyword & "' is an alias for: '" & |
| 946 |
Main_Command_Name & " " & |
| 947 |
Registered_Aliases.Element (Ukey).Flatten & "'"); |
| 948 |
else |
| 949 |
Put_Error ("No help found for: " & Keyword); |
| 950 |
Display_Global_Options; |
| 951 |
Display_Valid_Commands; |
| 952 |
Display_Valid_Topics; |
| 953 |
Display_Aliases; |
| 954 |
Error_Exit (1); |
| 955 |
end if; |
| 956 |
end Display_Help; |
| 957 |
|
| 958 |
------------- |
| 959 |
-- Execute -- |
| 960 |
------------- |
| 961 |
|
| 962 |
overriding |
| 963 |
procedure Execute (This : in out Builtin_Help; |
| 964 |
Args : AAA.Strings.Vector) |
| 965 |
is |
| 966 |
pragma Unreferenced (This); |
| 967 |
begin |
| 968 |
if Args.Count /= 1 then |
| 969 |
if Args.Count > 1 then |
| 970 |
Put_Error ("Please specify a single help keyword"); |
| 971 |
Put_Line (""); |
| 972 |
end if; |
| 973 |
|
| 974 |
Put_Line (TTY_Chapter ("USAGE")); |
| 975 |
Put_Line (" " & TTY_Underline (Main_Command_Name) & " " & |
| 976 |
TTY_Underline ("help") & " [<command>|<topic>]"); |
| 977 |
|
| 978 |
Put_Line (""); |
| 979 |
Put_Line (TTY_Chapter ("ARGUMENTS")); |
| 980 |
declare |
| 981 |
Tab : constant String (1 .. 1) := (others => ' '); |
| 982 |
Table : AAA.Table_IO.Table; |
| 983 |
begin |
| 984 |
Table.New_Row; |
| 985 |
Table.Append (Tab); |
| 986 |
Table.Append (TTY_Description ("<command>")); |
| 987 |
Table.Append ("Command for which to show a description"); |
| 988 |
|
| 989 |
Table.New_Row; |
| 990 |
Table.Append (Tab); |
| 991 |
Table.Append (TTY_Description ("<topic>")); |
| 992 |
Table.Append ("Topic for which to show a description"); |
| 993 |
|
| 994 |
Table.Print (Separator => " ", |
| 995 |
Put_Line => Put_Line_For_Access'Access); |
| 996 |
end; |
| 997 |
|
| 998 |
Display_Global_Options; |
| 999 |
Display_Valid_Commands; |
| 1000 |
Display_Valid_Topics; |
| 1001 |
Display_Aliases; |
| 1002 |
Error_Exit (1); |
| 1003 |
end if; |
| 1004 |
|
| 1005 |
Display_Help (Args (1)); |
| 1006 |
end Execute; |
| 1007 |
|
| 1008 |
---------------------- |
| 1009 |
-- Is_Global_Switch -- |
| 1010 |
---------------------- |
| 1011 |
|
| 1012 |
function Is_Global_Switch (Switch : String) return Boolean is |
| 1013 |
Parser : Opt_Parser; |
| 1014 |
Command_Line : GNAT.OS_Lib.Argument_List_Access := |
| 1015 |
GNAT.OS_Lib.Argument_String_To_List (Switch); |
| 1016 |
Result : Boolean := False; |
| 1017 |
begin |
| 1018 |
Initialize_Option_Scan |
| 1019 |
(Parser, |
| 1020 |
Command_Line => Command_Line, |
| 1021 |
Stop_At_First_Non_Switch => True); |
| 1022 |
|
| 1023 |
begin |
| 1024 |
Getopt (Config => Global_Config.GNAT_Cfg, |
| 1025 |
Parser => Parser, |
| 1026 |
Quiet => True); |
| 1027 |
Result := True; |
| 1028 |
exception |
| 1029 |
when Invalid_Switch => |
| 1030 |
Result := False; |
| 1031 |
end; |
| 1032 |
|
| 1033 |
GNAT.OS_Lib.Free (Command_Line); |
| 1034 |
return Result; |
| 1035 |
end Is_Global_Switch; |
| 1036 |
|
| 1037 |
end CLIC.Subcommand.Instance; |