Error: unrecognized file extension of parameter

Hi there, Ziggers!

I’m trying to simply read a command line argument. What sounds simple doesn’t seem to be so in reality while trying.

const std = @import("std");

pub fn main() void {
    var args = try std.process.argsWithAllocator();
    defer args.deinit();
    _ = args.skip();
    std.debug.print(args.next());
    std.debug.print("12th fibonacci number is {d}", .{fibonacci(12)});
}

I’m getting this error when I’m doing zig run. Have read other discussions and googled enough about this, but no luck. Can someone hint me in the right direction please? I’m on Windows.

image

I think there is some special syntax for passing arguments ? I don’t remember but when I do zig build run I have to do zig build run – argument. So maybe you also need a double dash like this ?

1 Like
zig run main.zig -- 12

Here’s a big clue that helped me, zig run, zig test, and others work on individual files (I think mostly) and you can get help with zig run --help.

zig build run and zig build test are NOT the same thing. zig build compiles and runs build.zig and run and test are part of that build program. If you look in build.zig you will see the test and run steps defined (and they can be defined however you want: zig build test could rm -rf / for all it cares.

For zig run main.zig you need the double slashes so it knows the arguments are to main.zig and not zig run. If you did a build and ran it main 12 would work fine.

4 Likes

To save future confusion, there’s also a couple problems with your program:

  1. argsWithAllocator needs you to pass in a chosen allocator to use, you can use the general purpose allocator for example:

    pub fn main() void {
     var gpa = std.heap.GeneralPurposeAllocator(.{}){};
     defer _ = gpa.deinit();
     var args = try std.process.argsWithAllocator(gpa.allocator())
    
  2. std.debug.print expects a format string as it’s first arguments, so your first print needs fixing:

    pub fn main() void {
        // ...
        // `?s` format specifier means "optional string" as args.next() may return null.
        std.debug.print("{?s}\n", .{args.next()});
        // ...
    }
    
2 Likes

Thanks for all your answers, it was a combination of all of them that helped, but there’s one more I found out:

I had to change the function signature from void to !void as using try implied the calling function returns an error too.

1 Like