How to get access to command line arguments with Zig code?
When starting program like this:
$ path/my-program foo bar
I'd like to get strings path/my-program
, foo
and bar
in my code. Either as c-style strings or Zig slices.
std.os.argv provides access to command line arguments.
std.os.argv.len => 3
std.os.argv[0] => "path/my-program"
std.os.argv[1] => "foo"
std.os.argv[2] => "bar"
Likely you'll need convert the value from argv
to a Zig slice before using it.
const path_null_terminated: [*:0]u8 = std.os.argv[1];
const path: [:0]const u8 = std.mem.span(path_null_terminated);
const file = try std.fs.cwd().openFile(path, .{});
Above code is tested with Zig 0.13.0 in December 2024.