Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support for slice and/or array types - multiple values on one option #8

Open
ktiy opened this issue Nov 26, 2020 · 2 comments
Open

Comments

@ktiy
Copy link

ktiy commented Nov 26, 2020

not sure how to word this one;
it would be cool to have support for slice and/or array types for multiple values on one option

const options = try argsParser.parse(struct {
    outputs: ?[][]const u8 = null, // slice, "--outputs output1 output2 etc."
    resolution: ?[2]u32 = null, // array, "--resolution 1920 1080"

    pub const shorthands = .{
        .r = "resolution",
        .o = "outputs",
    };
}, &args, argsAllocator);

im not sure how difficult this would be to implement though ^_^;

@ikskuh
Copy link
Owner

ikskuh commented Nov 26, 2020

Unlimited slice lengths are kinda weird, but what i wanted to support anyways at a point are custom types that have a parseCliOption function, so you can do this:

const options = try argsParser.parse(struct {
    outputs: ?OutputList = null, // slice, "--outputs output1,output2,output3 etc."
    resolution: ?Size = null, // array, "--resolution 1920x1080"

    pub const shorthands = .{
        .r = "resolution",
        .o = "outputs",
    };
}, &args, argsAllocator);

im not sure how difficult this would be to implement though ^_^;

The problem is that it's not unique anymore:

--outputs a b c d

Which of a, b, c, d is a positional argument, which one belongs to outputs?

One option would be to allow this:

--outputs a b c -- d

Where -- terminates the list.

The stuff in the first section is definitly planned (custom types), but let me think a bit about the second one (variadic length)

@ikskuh
Copy link
Owner

ikskuh commented Mar 20, 2024

Here's a decision for this:

We use a custom "list" type from args:

var cli = try args.parse(struct {
    item: args.Repeatable(i32) = .{}, // must be default constructible
    list: args.List(i32) = .{}, 
}, …);

const items: []const i32 = cli.options.item.get();
std.debug.print("items={any}\n", .{ items });

const list: []const i32 = cli.options.list.get();
std.debug.print("list={any}\n", .{ list });

which can be invoked like this:

[user@host] ~ $ ./demo --item 1 --item 2 --item 3 --list 4 5 6 7 -- --item 8
items={ 1, 2, 3, 8 }
list={ 4, 5, 6, 7 }

--item can be specified more than once, --list can only be specified once and must be terminated with --

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants