config #
config
config.load[T] fills a struct with string, int, and bool fields from CLI arguments, environment variables, TOML files, and field defaults, in that order.
import config
struct ServerConfig {
host string @[default: 'localhost']
port int @[default: '8080']
debug bool @[default: 'false']
}
fn main() {
cfg := config.load[ServerConfig](cli: ['--port=9090'], env_prefix: 'APP_')!
println(cfg.port) // 9090
}
CLI accepts --field=value, --field value, and bare --field as true. Underscores in field names may be written as dashes; the last CLI value wins across both spellings. Environment keys default to the prefix plus uppercase field name; @[env: 'NAME'] uses that exact key instead. The first TOML file containing a matching top-level key wins. Unreadable or invalid files return an error.
@[default: 'value'] supplies a fallback. Missing fields return an error; @[required] provides a required-key diagnostic. Empty strings are provided values. Decimal integers must fit V's signed 32-bit int; invalid values and overflow return errors. Booleans accept true, 1, yes, false, 0, and no, ignoring case. Other field types are unsupported.
fn load #
fn load[T](opts LoadOptions) !T
load fills a struct from the configured sources. Each field takes its value from the first source that provides it:
- cli:
--fieldor--field-with-dashes, as--k=vor--k v - env:
@[env: 'NAME'], elseenv_prefix + UPPER(field) - files: the TOML top-level key matching the field name
@[default: '...']
A field with no value and no default is an error, as is a field marked @[required]. Supported field types are string, int and bool; anything else is a compile error. Example:
struct ServerConfig {
host string @[env: 'HOST'; default: 'localhost']
port int @[default: '8080']
debug bool @[default: 'false']
token string @[required]
}
cfg := config.load[ServerConfig](cli: os.args[1..], env_prefix: 'APP_',
files: ['app.toml', '/etc/app.toml'])!
struct LoadOptions #
struct LoadOptions {
pub:
// cli are raw arguments, e.g. os.args[1..]; understands --key=value,
// --key value, and bare --flag (meaning "true")
cli []string
// env_prefix is prepended to UPPER(field) names, e.g. 'APP_'
env_prefix string
// files are TOML documents read in order; unreadable paths are errors
files []string
}
LoadOptions selects where configuration values come from, in precedence order: cli, then env, then files (first hit wins), then @[default].