Zenvars, a zig module for parsing .env files into zig structs is now at version 1.0.0.
Zenvars supports datatypes like floats, ints, strings, enums and bools
Usage:
Create a .env file or similar a place
name=Maria
age=32
male= # this is optional to have. Default will be used if value or key+value is missing
pi=3.14
In your zig codebase:
// Default values are necessary
pub const EnvVars = struct {
name: []const u8 = "none",
age: i32 = 0,
male: bool = false,
pi: f32 = 3.0,
};
pub fn main() !void {
// IMPORTANT: An arena allocator is needed for now
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer arena.deinit();
const alloc = arena.allocator();
const envs = try zenvars.parse(alloc, EnvVars, .{.filepath="/path/to/your/.env"});
// Or you might want to let the program find it
_ = try zenvars.parse(alloc, EnvVars, .{});
// You can even show the path if you'd like
_ = try zenvars.parse(alloc, EnvVars, .{ .show_path = true });
std.debug.print("name={s} age={d} male={} pi={d}\n", .{ p.name, p.age, p.male, p.pi });
}