Get an int value from args

Hi all, absolute zig noobie here, I would like to parse an integer from command line args on Linux , I know how to iterate over them and print them as strings thanks to a tutorial from dude the builder but I can’t figure out how to convert a string to an int

Hope that makes sense

TIA

The std.fmt.parseInt() function is likely what you’re looking for. The linked docs page has a few examples that will help you use it in your code.

1 Like

Thanks @00JCIV00 It’s confusing because I’m trying to use an iterator

var args_no_alloc_iter = process.args();
_ args_no_alloc_iter.next();

const second_arg = args_no_alloc_iter.next();
const num = try std.fmt.parseInt(i32, second_arg, 10); This bombs 
src/main.zig:18:44: error: expected type '[]const u8', found '?[:0]const u8'
const num2 = try std.fmt.parseInt(i32, second_arg, 10);

The examples you linked to all use hard coded vars so no use really, I should have mentioned I’m on Linux

src/main.zig:18:44: error: expected type '[]const u8', found '?[:0]const u8'

The call to args_no_alloc_iter.next() returns an optional (note the ? in the found '?[:0]const u8' part), which you need to unwrap before use (it may be null). For your quick example, you might want to do something like this:

var args_no_alloc_iter = process.args();
_ args_no_alloc_iter.next();

const second_arg = args_no_alloc_iter.next() orelse {
	std.log.err("Second arg is missing!", .{});
	return;
};
const num = try std.fmt.parseInt(i32, second_arg, 10);

The orelse will check if the optional is null - if it is, it will do what’s in the block (which ends with a return, so it’ll jump out of the current function); if it isn’t null, the block is skipped and you carry onto the const num = ... assignment.

2 Likes

Thanks @spiffyk I’ll give it a go

@spiffyk That worked a treat thank you very much :+1:

@spiffyk Hope you don’t mind if i bombard you with another question ?

I want a fn that accepts a string and also returns a string this is what I’ve tried unsuccessfuly

pub fn hello(name: []u8) []u8 
{
   return "Hello " ++ name;
}

There are a few things to know:

  • ++ can only be used to concatenate comptime strings, and name: []u8 is a runtime string.
  • You can allocate a runtime string on the heap that is the size you need, then copy the two strings into it using slices and @memcpy. You’ll need an Allocator and the caller will own the returned string, so the caller will eventually need to free the string using the Allocator.
  • You can pass a buffer (declared on the stack, for example) down to the hello method as a parameter, and then use @memcpy to copy the two strings into the buffer. Then you can return a slice of the buffer. The caller owns the memory because it owns the buffer. This can sometimes be used to avoid heap allocation.

EDIT: I suggest reading the Memory section of the docs:

2 Likes

The ++ operator is only available if both arguments have a length known at compile time, which is not the case for your function argument.

Edit: I was too late

1 Like

@jumpnbrownweasel
Thanks I will read that

You can use std.fmt.allocPrint(). It combines string in the way you want it.

const new_string = try std.fmt.allocPrint( gpa, "Hello {s}", .{name});