#Is there a working example on how to get CL arguments?

1 messages · Page 1 of 1 (latest)

nocturne kernel
#

The "Fahrenheit to Celsius" example does not work because it seems to be outdated, and other code using std.heap.GeneralPurposeAllocator(.{}){}; I found on the internet does not work either. So what's the current way to create CLI programs?

calm patrol
#

on 0.16+

pub fn main(init: std.process.Init) !void {
    var args_iterator = try init.minimal.args.iterateAllocator(init.gpa);
    defer args_iterator.deinit();

    var i: usize = 0;
    while (args_iter.next()) |arg| {
        std.debug.print("arg {d}: \"{s}\"\n", .{ i, arg });
        i += 1;
    }
}
nocturne kernel
#

awesome, this works. someone should update the example ...

calm patrol
#

if you're talking about zig.guide, it seems to lag behind breaking changes

nocturne kernel
#

yeah, that

calm patrol
#

the best reference for the language is really just the stdlib docs. things get changed quite a bit still right now so anything older than a few months is probably outdated

nocturne kernel
#

okay

#

so is there a way now to find the amount of args before iterating over them?

calm patrol
#

you could either do two passes, counting on the first pass, or use args.toSlice(init.arena.allocator()), and take the .len of that slice