| 1 |
-- Instantiate this package to create a sub-command parser/executor |
| 2 |
|
| 3 |
with CLIC.Config; |
| 4 |
|
| 5 |
generic |
| 6 |
|
| 7 |
Main_Command_Name : String; -- Name of the main command or program |
| 8 |
Version : String; -- Version of the program |
| 9 |
|
| 10 |
with procedure Set_Global_Switches |
| 11 |
(Config : in out CLIC.Subcommand.Switches_Configuration); |
| 12 |
-- This procedure should define the global switches using the |
| 13 |
-- Register_Switch procedures of the CLIC.Subcommand package. |
| 14 |
|
| 15 |
with procedure Put (Str : String); -- Used to print help and usage |
| 16 |
with procedure Put_Line (Str : String); -- Used to print help and usage |
| 17 |
with procedure Put_Error (Str : String); -- Used to print errors |
| 18 |
|
| 19 |
with procedure Error_Exit (Code : Integer); |
| 20 |
-- Used to signal that the program should terminate with the give error |
| 21 |
-- code. Typicaly use GNAT.OS_Lib.OS_Exit. |
| 22 |
|
| 23 |
-- The procedures below are used to format the output such as usage and |
| 24 |
-- help. Use CLIC.Subcommand.No_TTY if you don't want or need formating. |
| 25 |
with function TTY_Chapter (Str : String) return String; |
| 26 |
with function TTY_Description (Str : String) return String; |
| 27 |
with function TTY_Version (Str : String) return String; |
| 28 |
with function TTY_Underline (Str : String) return String; |
| 29 |
with function TTY_Emph (Str : String) return String; |
| 30 |
|
| 31 |
package CLIC.Subcommand.Instance is |
| 32 |
|
| 33 |
procedure Register (Cmd : not null Command_Access); |
| 34 |
-- Register a sub-command |
| 35 |
|
| 36 |
procedure Register (Group : String; Cmd : not null Command_Access); |
| 37 |
-- Register a sub-command in a group |
| 38 |
|
| 39 |
procedure Register (Topic : not null Help_Topic_Access); |
| 40 |
-- Register a help topic |
| 41 |
|
| 42 |
procedure Set_Alias (Alias : Identifier; Replacement : AAA.Strings.Vector); |
| 43 |
-- Define Alias such that "<Main_Command_Name> <Alias> <Extra_Args>" will |
| 44 |
-- be replaced by "<Main_Command_Name> <Replacement> <Extra_Args>". |
| 45 |
-- If Alias is already set, it will be silently replaced. |
| 46 |
|
| 47 |
procedure Load_Aliases (Conf : CLIC.Config.Instance; |
| 48 |
Root_Key : String := "alias"); |
| 49 |
-- Load aliases from the given configuration. |
| 50 |
-- |
| 51 |
-- All the config keys in the format "<Root_Key>.<sub-key>" are |
| 52 |
-- processed. If the value of a key is a (not null) String, <sub-key> |
| 53 |
-- is registered as an alias for the value (parsed into a list with |
| 54 |
-- GNAT.OS_Lib.Argument_String_To_List). |
| 55 |
-- |
| 56 |
-- For instance when Root_Key := "alias" (default): |
| 57 |
-- alias.test1 = "cmd arg1 arg2" # Valid: test1 -> ["cmd", "arg1", "arg2"] |
| 58 |
-- alias.test2 = "cmd" # Valid: test2 -> ["cmd"] |
| 59 |
-- alias.test3 = "" # Ignored |
| 60 |
-- alias.test4.test = "cmd arg1" # Ignored |
| 61 |
-- alias.test5 = 20 # Ignored |
| 62 |
-- alias.test6 = ["cmd", "arg1"] # Ignored |
| 63 |
|
| 64 |
procedure Execute |
| 65 |
(Command_Line : AAA.Strings.Vector := AAA.Strings.Empty_Vector); |
| 66 |
-- Parse the command line and execute a sub-command or display help/usage |
| 67 |
-- depending on command line args. |
| 68 |
-- |
| 69 |
-- If Command_Line is not empty it will be used instead of the actual |
| 70 |
-- command line arguments from Ada.Command_Line. |
| 71 |
|
| 72 |
procedure Parse_Global_Switches |
| 73 |
(Command_Line : AAA.Strings.Vector := AAA.Strings.Empty_Vector); |
| 74 |
-- Optional. Call this procedure before Execute to get only global switches |
| 75 |
-- parsed. This can be useful to check the values of global switches before |
| 76 |
-- running a sub-command or change the behavior of your program based on |
| 77 |
-- these (e.g. verbosity, output color, etc.). |
| 78 |
-- |
| 79 |
-- If Command_Line is not empty it will be used instead of the actual |
| 80 |
-- command line arguments from Ada.Command_Line. |
| 81 |
|
| 82 |
function What_Command return String; |
| 83 |
|
| 84 |
procedure Display_Usage (Displayed_Error : Boolean := False); |
| 85 |
|
| 86 |
procedure Display_Help (Keyword : String); |
| 87 |
|
| 88 |
Error_No_Command : exception; |
| 89 |
Command_Already_Defined : exception; |
| 90 |
|
| 91 |
type Builtin_Help is new Command with private; |
| 92 |
-- Use Register (new Builtin_Help); to provide a build-in help command |
| 93 |
|
| 94 |
function Is_Global_Switch (Switch : String) return Boolean; |
| 95 |
-- Say if the switch has been defined as global |
| 96 |
|
| 97 |
private |
| 98 |
|
| 99 |
-- Built-in Help Command -- |
| 100 |
type Builtin_Help is new Command with null record; |
| 101 |
|
| 102 |
overriding |
| 103 |
function Name (This : Builtin_Help) return Identifier |
| 104 |
is ("help"); |
| 105 |
|
| 106 |
overriding |
| 107 |
function Switch_Parsing (This : Builtin_Help) return Switch_Parsing_Kind |
| 108 |
is (Parse_All); |
| 109 |
|
| 110 |
overriding |
| 111 |
procedure Execute (This : in out Builtin_Help; |
| 112 |
Args : AAA.Strings.Vector); |
| 113 |
|
| 114 |
overriding |
| 115 |
function Long_Description (This : Builtin_Help) |
| 116 |
return AAA.Strings.Vector |
| 117 |
is (AAA.Strings.Empty_Vector |
| 118 |
.Append ("Shows information about commands and topics.") |
| 119 |
.Append ("See available commands with '" & |
| 120 |
Main_Command_Name & " help commands'") |
| 121 |
.Append ("See available topics with '" & |
| 122 |
Main_Command_Name & " help topics'.")); |
| 123 |
|
| 124 |
overriding |
| 125 |
procedure Setup_Switches |
| 126 |
(This : in out Builtin_Help; |
| 127 |
Config : in out CLIC.Subcommand.Switches_Configuration) |
| 128 |
is null; |
| 129 |
|
| 130 |
overriding |
| 131 |
function Short_Description (This : Builtin_Help) return String |
| 132 |
is ("Shows help on the given command/topic"); |
| 133 |
|
| 134 |
overriding |
| 135 |
function Usage_Custom_Parameters (This : Builtin_Help) return String |
| 136 |
is ("[<command>|<topic>]"); |
| 137 |
|
| 138 |
end CLIC.Subcommand.Instance; |