Creating a `std.process.Args.Iterator` in testing

Hello everyone!

Making my parsing library I’ve decided to have two parse functions

  1. parseArgs(gpa: Allocator, definition: anytype, args: []const []const u8, ...) for a very freestyle parsing. As I have to iterate over the arguments lots of times, the function requires args to be provided as in const args = init.minimal.args.toSlice(init.gpa). To test it is no problem, I just declare a const args = &[_][]u8{ "utility", "arg1", "--flag" } and I test it.
  2. A POSIX style parseArgsPosix(definition: anytype, args: *Args.Iterator, ...). Here is the question:

Is there someway to create the Args.Iterator object in the test itself given a string as it would be obtained from argv? Like this:

test "posix parses test" {
    const def = .{
        .options = .{ Opt(u64, "port", "p", 8080, "Port to connect") },
        .flags = .{ Flag("verbose", "v", "Print details") },
    };
    const args = createIterator("utility -v -p 100");
    const arguments = parseValuePosix(def, args, nullout, nullout);

    // your tests here
}

I’ve take a look at the std.process.Args, and I’ve found the IteratorGeneral, but that’s not a *Args.Iterator type so it’s not exactly what I am looking for, and if would like to avoid changing the type of args to anytype and maybe both would work? Also, if to ask for an Iterator is not what I should do, feel free to tell me :slight_smile:

Thank you!

Actually, I’ve managed it on my own by reading the std with a little bit more of attention. If someone is interested this works:

test "alone" {
    const def = .{
        .required = .{ Arg(u32, "count", "The number of items") },
        .flags = .{ Flag("verbose", "v", "Enable verbose output") },
        .options = .{ Opt([]const u8, "mode", "m", "default", "Operation mode") },
    };


    const fake_argv = &[_][*:0]const u8{ "utility", "-v", "-p", "diablo", "500"};
    const args = Args{ .vector = fake_argv };
    var iter = Args.Iterator.init(args);
    
    const result = try parseArgsPosix(def, &iter, nullout, nullout);

    try testing.expectEqual(@as(u32, 500), result.count);
    try testing.expectEqual(true, result.verbose);
    try testing.expectEqualStrings("diablo", result.mode);
}