SIMPLE_ENV
SCOOP-compatible environment variable access via Win32 API.
Creation
| Feature |
Signature |
Description |
default_create |
|
Create instance. No arguments needed. |
Access
| Feature |
Signature |
Description |
get |
(a_name: READABLE_STRING_GENERAL): detachable STRING_32 |
Get value of environment variable. Returns Void if not set. |
item alias [] |
(a_name: READABLE_STRING_GENERAL): detachable STRING_32 |
Array-style access. Alias for get. |
Contract: get
get (a_name: READABLE_STRING_GENERAL): detachable STRING_32
require
name_not_empty: not a_name.is_empty
Status Report
| Feature |
Signature |
Description |
has |
(a_name: READABLE_STRING_GENERAL): BOOLEAN |
Does environment variable exist? |
last_operation_succeeded |
: BOOLEAN |
Did the last set/unset operation succeed? |
Contract: has
has (a_name: READABLE_STRING_GENERAL): BOOLEAN
require
name_not_empty: not a_name.is_empty
Modification
| Feature |
Signature |
Description |
set |
(a_name, a_value: READABLE_STRING_GENERAL) |
Set environment variable to value. |
put |
(a_value, a_name: READABLE_STRING_GENERAL) |
Set variable (HASH_TABLE convention: value first). |
unset |
(a_name: READABLE_STRING_GENERAL) |
Remove environment variable. |
Contract: set
set (a_name, a_value: READABLE_STRING_GENERAL)
require
name_not_empty: not a_name.is_empty
ensure
variable_set: last_operation_succeeded implies attached get (a_name)
Contract: unset
unset (a_name: READABLE_STRING_GENERAL)
require
name_not_empty: not a_name.is_empty
ensure
variable_removed: last_operation_succeeded implies not has (a_name)
Expansion
| Feature |
Signature |
Description |
expand |
(a_string: READABLE_STRING_GENERAL): STRING_32 |
Expand %VAR% references in string. |
Contract: expand
expand (a_string: READABLE_STRING_GENERAL): STRING_32
require
string_not_empty: not a_string.is_empty
Example
Enumeration
| Feature |
Signature |
Description |
all_names |
: ARRAYED_LIST [STRING_32] |
All environment variable names in current process. |
names_with_prefix |
(a_prefix: READABLE_STRING_GENERAL): ARRAYED_LIST [STRING_32] |
All variable names starting with prefix (case-insensitive). |
Contract: all_names
all_names: ARRAYED_LIST [STRING_32]
ensure
result_attached: Result /= Void
Contract: names_with_prefix
names_with_prefix (a_prefix: READABLE_STRING_GENERAL): ARRAYED_LIST [STRING_32]
require
prefix_not_empty: not a_prefix.is_empty
ensure
result_attached: Result /= Void
Usage Examples
Getting Variables
local
env: SIMPLE_ENV
do
create env
if attached env.get ("PATH") as path then
print (path)
end
if attached env ["TEMP"] as temp then
print (temp)
end
end
Setting Variables
local
env: SIMPLE_ENV
do
create env
env.set ("MY_APP_CONFIG", "/etc/myapp/config")
if env.last_operation_succeeded then
print ("Configuration path set")
end
end
Expanding Paths
local
env: SIMPLE_ENV
config_path: STRING_32
do
create env
config_path := env.expand ("%APPDATA%\MyApp\config.json")
end
Enumerating Variables
local
env: SIMPLE_ENV
do
create env
across env.all_names as name loop
if attached env.get (name) as value then
print (name + "=" + value + "%N")
end
end
across env.names_with_prefix ("SIMPLE_") as lib loop
print (lib + "%N")
end
end