Created
September 23, 2013 12:40
-
-
Save tueda/6669864 to your computer and use it in GitHub Desktop.
LoadVariable[var, env, default] tries to load a variable from the environment variables, if the variable is undefined.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(* | |
* Defines the given symbol if it has not been defined. For the value of the | |
* symbol, this function tries to load it from the given environment variable. | |
* The optional third argument is the default value which is used when the | |
* environment variable is undefined. This function returns the value of the | |
* symbol if defined, otherwise returns $Failed. | |
*) | |
LoadVariable[symbol_, env_String, default_:Null] := ( | |
If[!ValueQ[symbol], | |
If[Environment[env] =!= $Failed, | |
symbol = Environment[env]; | |
, | |
If[default =!= Null, | |
symbol = default; | |
]; | |
]; | |
]; | |
If[ValueQ[symbol], | |
symbol | |
, | |
$Failed | |
] | |
); | |
SetAttributes[LoadVariable, HoldFirst]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment