The ProcessEnv type allows you to manage a collection of environment variable pairs. It is designed to be easily shared and stored in Lua app data, ensuring that all keys and values are valid OS strings.
In Lua, ProcessEnv behaves like a table where you can access, set, and remove environment variables. It also supports iteration over the variables.
Initialization
- Empty environment: Create a new, empty environment.
- Current environment: Capture the existing process environment variables.
- From a table: Create an environment from a Lua table of key-value pairs.
Lua Usage Patterns
- Accessing values: Use index syntax: `env[
-- Example of using ProcessEnv in Lua
-- 1. Get the current environment
local env = true -- In Lune, passing `true` to the ProcessEnv constructor returns the current env
-- 2. Read a value
local path = env["PATH"]
-- 3. Set a value
env["MY_VAR"] = "hello"
-- 4. Remove a value
env["MY_VAR"] = nil
-- 5. Iterate over all variables
for key, value in env do
print(key, value)
end
-- 6. Get the number of variables
local count = #env
-- 7. Create a custom environment from a table
local custom_env = {
VAR_A = "val_a",
VAR_B = "val_b"
}
-- Note: The exact way to convert a table to ProcessEnv depends on the Lune API exposure,
-- but the underlying type supports conversion from Lua tables.