Hi all, I want learn my first bits of zig by writing some simple programs that read the command line arguments and I can’t figure out how to read some ints.
Following this post Read command line arguments I tried using std.os.argv which seems the less verbose one and:
But parseInt is expecting a []const u8 and std.os.arvg.[1] has type [*:0]u8 so I get an error. Is there a way to parse some value of the latter type directly? Or do I have to instantiate an array? How is that done? Or there is another way suggested for doing this?
There’s also std.mem.span which is like a specialized version of sliceTo. So the call would be std.mem.span(std.os.argv[1]). I hadn’t run into this situation with args since I’m used to using std.process.args which returns an iterator over the args as strings:
var args_iter = std.process.args();
_ = args_iter.next(); // skip program name
const second_arg = args_iter.next() orelse print_usage_and_exit();
const n: i32 = try std.fmt.parseInt(i32, second_arg, 10);