init - INI config file manipulation for OCaml#
Init provides bidirectional codecs for INI files following Python's configparser semantics. Define your configuration types once, and Init handles both parsing and serialization.
Features#
- Python-compatible: Follows configparser semantics for maximum compatibility
- Bidirectional codecs: Define once, use for both decoding and encoding
- Type-safe: Strongly-typed configuration with compile-time guarantees
- Multiline values: Support for continuation lines via indentation
- Interpolation: Basic
%(name)sand extended${section:name}variable substitution - DEFAULT section: Automatic inheritance of default values
- Optional values: Graceful handling of missing configuration options
Installation#
opam install init
For parsing/encoding support, also install the bytesrw sub-library:
opam install init bytesrw
For Eio file system integration:
opam install init bytesrw eio bytesrw-eio
Quick Start#
(* Define your configuration type *)
type server_config = {
host : string;
port : int;
debug : bool;
}
(* Define the codec *)
let server_codec = Init.Section.(
obj (fun host port debug -> { host; port; debug })
|> mem "host" Init.string ~enc:(fun c -> c.host)
|> mem "port" Init.int ~enc:(fun c -> c.port)
|> mem "debug" Init.bool ~dec_absent:false ~enc:(fun c -> c.debug)
|> finish
)
let config_codec = Init.Document.(
obj (fun server -> server)
|> section "server" server_codec ~enc:Fun.id
|> finish
)
(* Parse an INI file *)
let () =
let ini = {|
[server]
host = localhost
port = 8080
debug = yes
|} in
match Init_bytesrw.decode_string config_codec ini with
| Ok config ->
Printf.printf "Server: %s:%d (debug=%b)\n"
config.host config.port config.debug
| Error msg ->
Printf.printf "Error: %s\n" msg
Sub-libraries#
- init: Core codec combinators (no dependencies)
- init.bytesrw: Parsing and encoding using bytesrw
- init.eio: Eio file system integration
Documentation#
See the API documentation for complete reference.
License#
ISC License. See LICENSE for details.