| 1 |
with AAA.Strings; |
| 2 |
with GNAT.Strings; |
| 3 |
with CLIC.Command_Line; |
| 4 |
|
| 5 |
private with Ada.Strings.Unbounded; |
| 6 |
private with Ada.Containers.Vectors; |
| 7 |
|
| 8 |
package CLIC.Subcommand is |
| 9 |
|
| 10 |
-- This root package defines the interface types to be used in the |
| 11 |
-- Subcommand. See CLIC.Subcommand.Instance to create the parser/executor. |
| 12 |
|
| 13 |
type Switches_Configuration is limited private; |
| 14 |
-- This is a wrapper around GNAT.Command_Line.Command_Line_Configuration |
| 15 |
-- to provide extra features such as duplicate switch detection and custom |
| 16 |
-- usage format. The "Define_Switch" procedure below work exactly like the |
| 17 |
-- GNAT.Command_Line ones. |
| 18 |
|
| 19 |
procedure Define_Switch |
| 20 |
(Config : in out Switches_Configuration; |
| 21 |
Switch : String := ""; |
| 22 |
Long_Switch : String := ""; |
| 23 |
Help : String := ""; |
| 24 |
Section : String := ""; |
| 25 |
Argument : String := "ARG"); |
| 26 |
|
| 27 |
procedure Define_Switch |
| 28 |
(Config : in out Switches_Configuration; |
| 29 |
Output : access Boolean; |
| 30 |
Switch : String := ""; |
| 31 |
Long_Switch : String := ""; |
| 32 |
Help : String := ""; |
| 33 |
Section : String := ""; |
| 34 |
Value : Boolean := True); |
| 35 |
|
| 36 |
procedure Define_Switch |
| 37 |
(Config : in out Switches_Configuration; |
| 38 |
Output : access Integer; |
| 39 |
Switch : String := ""; |
| 40 |
Long_Switch : String := ""; |
| 41 |
Help : String := ""; |
| 42 |
Section : String := ""; |
| 43 |
Initial : Integer := 0; |
| 44 |
Default : Integer := 1; |
| 45 |
Argument : String := "ARG"); |
| 46 |
|
| 47 |
procedure Define_Switch |
| 48 |
(Config : in out Switches_Configuration; |
| 49 |
Output : access GNAT.Strings.String_Access; |
| 50 |
Switch : String := ""; |
| 51 |
Long_Switch : String := ""; |
| 52 |
Help : String := ""; |
| 53 |
Section : String := ""; |
| 54 |
Argument : String := "ARG"); |
| 55 |
|
| 56 |
procedure Define_Switch |
| 57 |
(Config : in out Switches_Configuration; |
| 58 |
Callback : not null CLIC.Command_Line.Value_Callback; |
| 59 |
Switch : String := ""; |
| 60 |
Long_Switch : String := ""; |
| 61 |
Help : String := ""; |
| 62 |
Section : String := ""; |
| 63 |
Argument : String := "ARG"); |
| 64 |
|
| 65 |
subtype Identifier is String |
| 66 |
with Predicate => (for all C of Identifier |
| 67 |
=> C in 'a' .. 'z' | 'A' .. 'Z' | |
| 68 |
'0' .. '9' | '-' | '.' | '_'); |
| 69 |
------------- |
| 70 |
-- Command -- |
| 71 |
------------- |
| 72 |
|
| 73 |
type Command is limited interface; |
| 74 |
-- This type encapsulates configuration and execution of a specific |
| 75 |
-- command. It also has help-related subprograms. |
| 76 |
|
| 77 |
type Command_Access is access all Command'Class; |
| 78 |
|
| 79 |
function Name (Cmd : Command) return Identifier |
| 80 |
is abstract; |
| 81 |
-- Commands must override this procedure to provide the sub-command name. |
| 82 |
-- This name is used to identify the sub-command in usage and command line. |
| 83 |
-- E.g. "my_app <name>" will exectute the <name> command. |
| 84 |
|
| 85 |
type Switch_Parsing_Kind is |
| 86 |
(Parse_All, |
| 87 |
-- All the sub-command arguments are parsed for switches |
| 88 |
|
| 89 |
Before_Double_Dash, |
| 90 |
-- Only the arguments before a potential "--" are parsed for switches. |
| 91 |
-- The remaining switches and arguments are passed to the Args parameter |
| 92 |
-- of the Execute primitive. |
| 93 |
|
| 94 |
All_As_Args |
| 95 |
-- Sub-command arguments parsing is disabled, both the sub-command |
| 96 |
-- switches and arguments passed to the Args parameter of the Execute |
| 97 |
-- primitive. |
| 98 |
); |
| 99 |
|
| 100 |
function Switch_Parsing (Cmd : Command) return Switch_Parsing_Kind |
| 101 |
is abstract; |
| 102 |
-- Return True to skip sub-command switches parsing and get both the |
| 103 |
-- sub-command switches and arguments passed to the Args parameter of |
| 104 |
-- the Execute primitive. |
| 105 |
|
| 106 |
procedure Execute (Cmd : in out Command; |
| 107 |
Args : AAA.Strings.Vector) |
| 108 |
is abstract; |
| 109 |
-- Commands must override this procedure to provide the command |
| 110 |
-- functionality. |
| 111 |
|
| 112 |
function Long_Description (Cmd : Command) return AAA.Strings.Vector |
| 113 |
is abstract; |
| 114 |
-- Return a detailed description of the command. Each string in the vector |
| 115 |
-- is a paragraph that will be reformatted into appropriate length lines. |
| 116 |
|
| 117 |
procedure Setup_Switches |
| 118 |
(Cmd : in out Command; |
| 119 |
Config : in out Switches_Configuration) |
| 120 |
is null; |
| 121 |
-- Gets called once the command has been identified, but before the call to |
| 122 |
-- Execute. Config must be set up with the switches used by the command. |
| 123 |
|
| 124 |
function Short_Description (Cmd : Command) return String |
| 125 |
is abstract; |
| 126 |
-- One-liner displayed in the list of commands that alr understands that |
| 127 |
-- gets shown when no command or unknown command is given. Also shown as |
| 128 |
-- SUMMARY in the help of a specific command. |
| 129 |
|
| 130 |
function Usage_Custom_Parameters (Cmd : Command) return String |
| 131 |
is abstract; |
| 132 |
-- The part after "<main> [global options] command [command options] " that |
| 133 |
-- gets shown in USAGE in the command help summary. That is, it is the |
| 134 |
-- specific command-line part that is not managed via Gnat.Command_Line |
| 135 |
|
| 136 |
----------------- |
| 137 |
-- Help_Topic -- |
| 138 |
----------------- |
| 139 |
|
| 140 |
type Help_Topic is limited interface; |
| 141 |
-- This type encapsulate the content of an "help topic", i.e. a piece of |
| 142 |
-- documentation that can displayed from the command line. |
| 143 |
|
| 144 |
type Help_Topic_Access is access all Help_Topic'Class; |
| 145 |
|
| 146 |
function Name (This : Help_Topic) return Identifier |
| 147 |
is abstract; |
| 148 |
-- This name is used to identify the topic in usage and command line. |
| 149 |
-- E.g. "my_app help <name>" will display the content of the <name> topic. |
| 150 |
|
| 151 |
function Title (This : Help_Topic) return String |
| 152 |
is abstract; |
| 153 |
-- Descriptive title for the topic content. Unlike the Name, the Title can |
| 154 |
-- containt whitespaces. |
| 155 |
|
| 156 |
function Content (This : Help_Topic) return AAA.Strings.Vector |
| 157 |
is abstract; |
| 158 |
-- Return the content of the help topic. Each string in the vector is a |
| 159 |
-- paragraph that will be reformatted into appropriate length lines. |
| 160 |
|
| 161 |
----------- |
| 162 |
-- Utils -- |
| 163 |
----------- |
| 164 |
|
| 165 |
function No_TTY (Str : String) return String |
| 166 |
is (Str); |
| 167 |
-- Use this function for the TTY_* generic parameters of |
| 168 |
-- CLIC.Subcommand.Instance if you don't want or need TTY formating. |
| 169 |
|
| 170 |
private |
| 171 |
|
| 172 |
type Switch_Info is record |
| 173 |
Switch : Ada.Strings.Unbounded.Unbounded_String; |
| 174 |
Long_Switch : Ada.Strings.Unbounded.Unbounded_String; |
| 175 |
Help : Ada.Strings.Unbounded.Unbounded_String; |
| 176 |
Argument : Ada.Strings.Unbounded.Unbounded_String; |
| 177 |
end record; |
| 178 |
-- Used internaly to store informations about the switches |
| 179 |
|
| 180 |
package Switch_Info_Vectors |
| 181 |
is new Ada.Containers.Vectors (Natural, Switch_Info); |
| 182 |
|
| 183 |
procedure Add (Vect : in out Switch_Info_Vectors.Vector; |
| 184 |
Switch, Long_Switch, Help, Argument : String); |
| 185 |
|
| 186 |
type Switches_Configuration is limited record |
| 187 |
GNAT_Cfg : CLIC.Command_Line.Command_Line_Configuration; |
| 188 |
-- Still use GNAT.Command_Line to do the actual parsing |
| 189 |
|
| 190 |
Info : Switch_Info_Vectors.Vector; |
| 191 |
end record; |
| 192 |
|
| 193 |
procedure Clear (This : in out Switches_Configuration); |
| 194 |
|
| 195 |
end CLIC.Subcommand; |