ini
INI and .env config file support - parse Windows-style [section] INI files to nested objects, serialise objects back to INI text, parse flat .env key=value files with $VAR and ${VAR} variable expansion, serialise to .env text, and load either format from a file path.
Load with: use ini
What this module does
ini handles two related flat-file config formats.
INI files use [section] headers followed by key = value
lines; the parser builds a nested object where each section is a sub-object.
Lines starting with ; or # are comments.
.env files are flat KEY=VALUE pairs (no sections),
with # comments and optional variable expansion - a value like
BASE_URL=https://${HOST}:${PORT} references earlier keys.
Both formats strip single and double quotes from values.
Quick example
use ini
# Parse INI
cfg = inipa("[server]\nhost = localhost\nport = 8080\n\n[db]\nname = mydb")
prn(cfg.server.host) # "localhost"
prn(cfg.db.name) # "mydb"
# Write INI
prn(iniwr(cfg))
# Parse .env (with variable expansion)
env = envpa("HOST=example.com\nPORT=443\nURL=https://${HOST}:${PORT}")
prn(env.URL) # "https://example.com:443"
# Write .env
prn(envwr(env))
# Load from file
cfg2 = inilo("config/app.ini")
env2 = envlo(".env")
Functions
INI format
inipa(source)
Parses an INI string. Section headers create sub-objects. Lines before the first section go into the root object. Keys with spaces, =, #, or ; in their values are auto-quoted on write. Comments (; or #) and blank lines are skipped. Double-quoted values are unquoted.
iniwr(config)
Serialises a nested config object back to INI text. Global (non-object) keys are written first, then each section as [section_name] followed by its key-value pairs.
inilo(path)
Reads an INI file from path and parses it with inipa.
.env format
envpa(source)
Parses a .env string. Each KEY=VALUE line is added to a flat object. Values are unquoted (both single and double quotes). ${VAR} and $VAR references are expanded using already-parsed keys.
envwr(config)
Serialises a flat config object to .env text. Values containing spaces, #, or $ are double-quoted.
envlo(path)
Reads a .env file from path and parses it with envpa.
Notes
- Variable expansion in
.envfiles only references keys defined earlier in the same file. - Requires
trlandtxt.