Hejsil / zig-clap

Command line argument parsing library
MIT License
939 stars 67 forks source link

Support for more complex parameters #35

Open maringuu opened 3 years ago

maringuu commented 3 years ago

I'd like to parse something like --mode <width>x<height>[@<refresh>Hz]. To my from my current understanding this is not possible.

I intended to write it as follows:

    const params = comptime [_]clap.Param(clap.Help) {
        .{
            .id = .{.msg = "defines the output mode", .value = "<width>x<height>[@<refresh>Hz]"},
            .names = .{ .short = 'm', .long = "mode" },
            .takes_value = .One,
        },
    };

    var args = try clap.parse(clap.Help, &params, std.heap.page_allocator, null);
    defer args.deinit();

    if (args.option("--mode")) |n| {
        // Parse width,height and refresh manually
    }

This would work but is to some extend a hack. The main problem with this is that all help* functions become useless because they put <> around the value string.

A simple fix would be to make the formatting in parseParam accept a custom formatstring. But I am not sure that this is the right solution.

When this usecase is considered valid then I'll spend some time thinking about a good solution.

Hejsil commented 3 years ago

Aah, so this is a request for improving parseParam and help to support more exotic help "value descriptions" per say. Yea, if we can come up with something good, then I'm all for making this improvement :+1:

Hejsil commented 3 years ago

Ofc, you'll still need to handle parsing this format outside the library, but as far as I understand, that is your intention.