Ada 2012 added a new function in Ada.Environment_Variable: function Value (Name : in String; Default : in String) return String;
The test is to build this main procedure with the switch -gnat2012: with Ada.Text_IO; use Ada.Text_IO; with Ada.Environment_Variables; use Ada.Environment_Variables; procedure Main is EV : constant String := "GNAT_TOTO"; begin Put_Line (Boolean'Image (Exists (EV))); Put_Line (Value (EV, "toto")); Set (EV, "tata"); Put_Line (Boolean'Image (Exists (EV))); Put_Line (Value (EV, "toto")); Put_Line (Value (EV)); end Main; The output of its execution should be: FALSE toto TRUE tata tata Tested on x86_64-pc-linux-gnu, committed on trunk 2013-04-23 Vincent Celier <cel...@adacore.com> * a-envvar.adb, a-envvar.ads (Value): New.
Index: a-envvar.adb =================================================================== --- a-envvar.adb (revision 198175) +++ a-envvar.adb (working copy) @@ -6,7 +6,7 @@ -- -- -- B o d y -- -- -- --- Copyright (C) 2009-2012, Free Software Foundation, Inc. -- +-- Copyright (C) 2009-2013, Free Software Foundation, Inc. -- -- -- -- GNAT is free software; you can redistribute it and/or modify it under -- -- terms of the GNU General Public License as published by the Free Soft- -- @@ -223,4 +223,13 @@ end if; end Value; + function Value (Name : String; Default : String) return String is + begin + if Exists (Name) then + return Value (Name); + + else + return Default; + end if; + end Value; end Ada.Environment_Variables; Index: a-envvar.ads =================================================================== --- a-envvar.ads (revision 198175) +++ a-envvar.ads (working copy) @@ -23,6 +23,11 @@ -- Constraint_Error is propagated. If the execution environment does not -- support environment variables, then Program_Error is propagated. + function Value (Name : String; Default : String) return String; + -- If the external execution environment supports environment variables and + -- an environment variable with the given name currently exists, then Value + -- returns its value; otherwise, it returns Default. + function Exists (Name : String) return Boolean; -- If the external execution environment supports environment variables and -- an environment variable with the given name currently exists, then