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

Custom argument to value parsers #3

Closed
Hejsil opened this issue Apr 16, 2018 · 1 comment
Closed

Custom argument to value parsers #3

Hejsil opened this issue Apr 16, 2018 · 1 comment
Labels
enhancement New feature or request

Comments

@Hejsil
Copy link
Owner

Hejsil commented Apr 16, 2018

Currently, we are able to parse strings to a few different types depending on the type of the fields we are trying to set.

I don't think we should try to support more than the basic types (and []const u8), but the user should be able to provide custom parser:

const Options = struct {
    a: std.Arraylist([]const u8),
};

fn addToArraylist(comptime T: type, arraylist: &T, arg: []const u8) !void {
    try arraylist.append(arg);
}

const parser = comptime Clap(Options).Builder
    .init(
        Options {
            .a = std.Arraylist([]const u8).init(std.debug.global_allocator),
        }
    )
    .command(
        Command.Builder
            .init("command")
            .arguments(
                []Argument {
                    Argument.Builder
                        .init("a")
                        .help("Add to 'a'")
                        .short('a')
                        .takesValue(addToArraylist)
                        .build(),
                }
            )
            .build()
    )
    .build();

This would allow command -a something -a "something else" to save both values passed to a.

This does not go against the goal of being a non-allocating clap. zig-clap will not allocate by default, but we can't stop users from allocating in custom argument parsing function.

@Hejsil Hejsil added the enhancement New feature or request label Apr 16, 2018
@Hejsil
Copy link
Owner Author

Hejsil commented Apr 16, 2018

We could replace the current strToValue function, with a set of standard parsers provided by zig-clap.

@Hejsil Hejsil closed this as completed in 5ed6f74 Apr 26, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant