Commit 42e508b4 by Pascal Obry Committed by Pierre-Marie de Rodat

[Ada] Add support for Define_Switch with a callback in GNAT.Command_Line

Add support for Define_Switch with a callback in GNAT.Command_Line.

The callback is called for every instance of the switch found on the
command line. This make it possible to have full control over the
switch value and chain multiple actions if needed.

2018-05-30  Pascal Obry  <obry@adacore.com>

gcc/ada/

	* libgnat/g-comlin.ads (Value_Callback, Define_Switch): New.
	* libgnat/g-comlin.adb: Add corresponding implementation.

From-SVN: r260940
parent 7cc6d416
2018-05-30 Pascal Obry <obry@adacore.com>
* libgnat/g-comlin.ads (Value_Callback, Define_Switch): New.
* libgnat/g-comlin.adb: Add corresponding implementation.
2018-05-30 Gary Dismukes <dismukes@adacore.com> 2018-05-30 Gary Dismukes <dismukes@adacore.com>
* sem_res.adb, sem_util.adb: Fix several typos. * sem_res.adb, sem_util.adb: Fix several typos.
......
...@@ -1474,6 +1474,29 @@ package body GNAT.Command_Line is ...@@ -1474,6 +1474,29 @@ package body GNAT.Command_Line is
end if; end if;
end Define_Switch; end Define_Switch;
-------------------
-- Define_Switch --
-------------------
procedure Define_Switch
(Config : in out Command_Line_Configuration;
Callback : not null Value_Callback;
Switch : String := "";
Long_Switch : String := "";
Help : String := "";
Section : String := "";
Argument : String := "ARG")
is
Def : Switch_Definition (Switch_Callback);
begin
if Switch /= "" or else Long_Switch /= "" then
Initialize_Switch_Def
(Def, Switch, Long_Switch, Help, Section, Argument);
Def.Callback := Callback;
Add (Config, Def);
end if;
end Define_Switch;
-------------------- --------------------
-- Define_Section -- -- Define_Section --
-------------------- --------------------
...@@ -3403,6 +3426,10 @@ package body GNAT.Command_Line is ...@@ -3403,6 +3426,10 @@ package body GNAT.Command_Line is
Local_Config.Switches (Index).String_Output.all := Local_Config.Switches (Index).String_Output.all :=
new String'(Parameter); new String'(Parameter);
return; return;
when Switch_Callback =>
Local_Config.Switches (Index).Callback (Switch, Parameter);
return;
end case; end case;
end if; end if;
...@@ -3471,7 +3498,7 @@ package body GNAT.Command_Line is ...@@ -3471,7 +3498,7 @@ package body GNAT.Command_Line is
for S in Local_Config.Switches'Range loop for S in Local_Config.Switches'Range loop
case Local_Config.Switches (S).Typ is case Local_Config.Switches (S).Typ is
when Switch_Untyped => when Switch_Untyped | Switch_Callback =>
null; -- Nothing to do null; -- Nothing to do
when Switch_Boolean => when Switch_Boolean =>
......
...@@ -678,6 +678,20 @@ package GNAT.Command_Line is ...@@ -678,6 +678,20 @@ package GNAT.Command_Line is
-- so that you can specify the default value directly in the declaration -- so that you can specify the default value directly in the declaration
-- of the variable). The switch must accept an argument. -- of the variable). The switch must accept an argument.
type Value_Callback is access procedure (Switch, Value : String);
procedure Define_Switch
(Config : in out Command_Line_Configuration;
Callback : not null Value_Callback;
Switch : String := "";
Long_Switch : String := "";
Help : String := "";
Section : String := "";
Argument : String := "ARG");
-- Call Callback for each instance of Switch. The callback is given the
-- actual switch and the corresponding value. The switch must accept
-- an argument.
procedure Set_Usage procedure Set_Usage
(Config : in out Command_Line_Configuration; (Config : in out Command_Line_Configuration;
Usage : String := "[switches] [arguments]"; Usage : String := "[switches] [arguments]";
...@@ -1111,7 +1125,8 @@ private ...@@ -1111,7 +1125,8 @@ private
type Switch_Type is (Switch_Untyped, type Switch_Type is (Switch_Untyped,
Switch_Boolean, Switch_Boolean,
Switch_Integer, Switch_Integer,
Switch_String); Switch_String,
Switch_Callback);
type Switch_Definition (Typ : Switch_Type := Switch_Untyped) is record type Switch_Definition (Typ : Switch_Type := Switch_Untyped) is record
Switch : GNAT.OS_Lib.String_Access; Switch : GNAT.OS_Lib.String_Access;
...@@ -1135,6 +1150,8 @@ private ...@@ -1135,6 +1150,8 @@ private
Integer_Default : Integer; Integer_Default : Integer;
when Switch_String => when Switch_String =>
String_Output : access GNAT.Strings.String_Access; String_Output : access GNAT.Strings.String_Access;
when Switch_Callback =>
Callback : Value_Callback;
end case; end case;
end record; end record;
type Switch_Definitions is array (Natural range <>) of Switch_Definition; type Switch_Definitions is array (Natural range <>) of Switch_Definition;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment