Accidentally Made a Zig dotenv Parser How I ended up creating a dotenv parser while working on a CLI argument parser Recently I’ve made a Zig based CLI argument parser called argh I’ve even written myself a roadmap I’d like to follow with this project. And so far I have been following it pretty well. However last week I began working on the next item on my roadmap which was to add support for environment variables to the arg parser, where you could set an environment variable to set the value of a flag. Not many use cases for it I know it’s more of an edge case but I thought it would be a fun exercise to implement that. So I started working on it. I carefully scaffolded out the feature what functions I’d like to see in it how they should behave what guard rails I’d like to set to ensure type safety, memory safety, and so on. As I was implementing all the things I scaffolded out one by one I realized that I was basically writing a fully fledged dotenv parser. And I thought to myself “hey this is pretty cool I should probably make this a separate library so other people can use it too” and it’s not deeply coupled to the arg parser so it makes sense to do that. So I did. I extracted the code out into its own repository called zdotenv. The Library The dotenv parser itself is pretty simple to use and I’ve even provided a basic example and tests in my repository that you can check out to see how it works. But regardless here’s a quick example of how to use it: const std = @import("std"); const Dotenv = @import("dotenv").Dotenv; pub fn main() !void { const allocator = std.heap.page_allocator; var dotenv = Dotenv.init(allocator); defer dotenv.deinit(); try dotenv.load(&[_][]const u8{"./examples/example.env"}); if (dotenv.get("FOO")) |val| { std.debug.print("FOO={s}\n", .{val}); } else { std.debug.print("FOO not found\n", .{}); } } This will load the environment variables from the specified file and you can then access them using the get method. The load method takes an arra...
First seen: 2025-10-13 23:26
Last seen: 2025-10-17 00:50