Skip to content

Application Configuration

Chris Michael edited this page Oct 7, 2026 · 1 revision

Pudu

Application Configuration

A program built on Std.App.Config declares its settings with defaults and layers sources over them. Configuration.withVariables is one more layer: each declared setting takes the loaded variable its key names — server.port reads SERVER_PORT, log-level reads LOG_LEVEL — and records that the value came from the environment. It stands where withEnvironment() would, reading the loaded view instead of the bare process environment:

module Ex10Configuration

import Std.App.Config as Config
import Std.Io as Io
import PuduLangEnvironment.Configuration as Configuration
import PuduLangEnvironment.Loader as Loader
import PuduLangEnvironment.Options as Options
import PuduLangEnvironment.Source as Source

export fn main() -> Int {
  let declared = Config.declaring(&[("server.port", "8080"), ("server.host", "localhost"), ("log-level", "info")])
  let options = Options.defaults().withSources(&[Source.Text("inline", "SERVER_PORT=9443\nLOG_LEVEL=debug\nUNRELATED=ignored\n")])
  match Loader.load(&options) {
    case Ok(variables) => {
      let config = declared.withVariables(&variables)
      for pair in Config.entries(&config) {
        let _shown = Io.writeLine(pair[0] + " = " + pair[1].value + " (" + Config.sourceName(&pair[1].source) + ")")
      }
      if Config.whole(&config, "server.port") == Ok(9443) && Config.textOr(&config, "server.host", "") == "localhost" { 0 } else { 1 }
    }
    case Err(_) => 1
  }
}

Output:

log-level = debug (environment)
server.host = localhost (declared)
server.port = 9443 (environment)

A variable no setting declares, like UNRELATED above, never becomes a setting. An empty variable leaves the declared value. The configuration's profile and its other layers are untouched, so the chain continues: Config.declaring(&defaults).withVariables(&variables).withArguments().

Related

Clone this wiki locally