#arg cmp str
1 messages · Page 1 of 1 (latest)
you need to first run std.mem.span on the arg to get a slice
it's stored as a null terminated pointer
why
a null terminated pointer is a pointer with a null value (value of 0) at the end
this is used to indicate the length
i know
whereas a Zig slice has a ptr + a separate numeric length
you need to convert between the two
span will find the sentinel and produce a slice with that found length
because that's what the value is stored as 🤷♂️
it's equivalent to C's argv
yes
std.process.argsAlloc for example
or std.mem.span as said earlier to convert from [*:0] -> []
I'd strongly recommend argsAlloc or a close sibling though
std.os.argv is not cross-platform (windows specifically comes to mind)
why cant you iterate using a for loop
std.process.args()
well that's what argsAlloc is
it just takes an allocator because a memory allocation is potentially required to produce the argument list :)
yeah, argsAlloc literally just wraps the ArgIterator
oh i didn't know that, so yeah the allocator is 100% required then lol
i wonder if it could be done inplace where the os allocs the args on the stack
i think you're confused here because you assume you can do for (arg_iter) which is not true. we dont have implicit iterators. you will need to do the while (arg_iter.next()) pattern
that's what happens on posix kind of
but not on windows (and wasi ig)
thus the alloc variants
then for loop isnt a for loop?
std.os.argv is precisely a pointer to the arguments on the stack, which is why it can't contain slices
yea could it be convcerted into slices inplace
need to offset the chars by one and place the length
there is no space to store the length
You can, but process arguments aren't zig strings (except on some theoretical zig operating system).
is there a way to check if im missing defer deinit
expected type 'mem.Allocator', found 'type'
you need to do std.heap.GeneralPurposeAllocator(.{}){} because it's a function that returns a type that needs to be initialized.
comptime var alloc = std.heap.GeneralPurposeAllocator(.{});
var args = try std.process.argsWithAllocator(alloc);
const std = @import("std");
pub fn main() !void {
var alloc = std.heap.GeneralPurposeAllocator(.{}){};
var args = try std.process.argsWithAllocator(alloc.allocator());
while (args.next()) |arg| {
if (std.mem.eql(u8, arg, "--help")) {
std.debug.print("{s}\n", .{"Usage: echo [STRING]..."});
return;
}
std.debug.print("{s}\n", .{arg});
}
_ = alloc.detectLeaks();
}
its not dping nyaything
zig build run -- --help
are you adding the args to to the run step in the build.zig?
whats a build.zig
it's the zig file used for managing the build system. if you dont know what that is, then you're running it wrong.
you want to use zig run file.zig -- --help for example, or zig build-exe...
oh sure then
that's because when you have then --help arg, you're returning
and it never reaches the detect leaks
ah copilot messed it up
um its still not working
does detect leaks need to be called outside main function?
no
you're doing it right
on an OS that supports std.os.argv there is no allocation that happens
so there's nothing to leak
you should still run deinit, to be cross platform
does it not have to convert to slices?
so?
that requiers alloc right?
no it doesn't
argv is c strings right
sure
can defer deinit be enforced?
no