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

Parse int larger than u8 #103

Closed
rupurt opened this issue Sep 10, 2023 · 4 comments
Closed

Parse int larger than u8 #103

rupurt opened this issue Sep 10, 2023 · 4 comments

Comments

@rupurt
Copy link

rupurt commented Sep 10, 2023

Currently it looks like you can only parse integer arguments represented as u8.

I get the following error when trying to use u16:

    const params = comptime clap.parseParamsComptime(
        \\-h, --help                       Display this help and exit.
        \\-l, --line-buffer <LINEBUFFER>   An option parameter, which takes a value.
        \\<FILE1>                          The base file
        \\<FILE2>                          The file to compare
        \\
    );
    const parsers = comptime .{
        .LINEBUFFER = clap.parsers.int(u16, 1024),
        .FILE1 = clap.parsers.string,
        .FILE2 = clap.parsers.string,
    };
src/main.zig:17:45: error: type 'u8' cannot represent integer value '1024'

The tests only cover u8 https://github.com/Hejsil/zig-clap/blob/master/clap/parsers.zig#L39

@Hejsil
Copy link
Owner

Hejsil commented Sep 10, 2023

The second argument to clap.parsers.int is the radix aka, the base in which we parse the number. For 10, this means that "10" => 10. For 2 this means "10" => 2. I don't think the standard library parseInt supports base 1024.

@Hejsil
Copy link
Owner

Hejsil commented Sep 10, 2023

I could rename it from radix to base to make this more clear. I think the standard library used to call it radix, but changed to base at some point after I wrote that function. Consistency is hard :)

@Hejsil
Copy link
Owner

Hejsil commented Sep 10, 2023

This seems to be a misunderstanding of what the second argument of clap.parsers.int does. I've added some documentation to the function in this PR. Hopefully that should avoid future confusion

@rupurt
Copy link
Author

rupurt commented Sep 11, 2023

Ah, gotcha!

Thanks for the clarification. That's just me being a newb in zig... More docs are always welcome though 😄 Thanks for the useful library!

@Hejsil Hejsil closed this as completed Sep 11, 2023
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