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

help example doesn't show version #57

Closed
canadaduane opened this issue Nov 29, 2021 · 5 comments · Fixed by #59
Closed

help example doesn't show version #57

canadaduane opened this issue Nov 29, 2021 · 5 comments · Fixed by #59

Comments

@canadaduane
Copy link

canadaduane commented Nov 29, 2021

I'm trying to learn how zig-clap works, and I hoped the help example would be more illustrative:

$ ./zig-out/bin/help --help
	-h, --help   	Display this help and exit.
	-v, --version	Output version information and exit.
$ ./zig-out/bin/help -v
	-h, --help   	Display this help and exit.
	-v, --version	Output version information and exit.

Maybe this example should also show a simple version string output?

What I'm really looking for is an example where the args are parsed and then re-used as help output. i.e. I don't want to write out the arguments and their instructions only to re-write those arguments and their instructions in the "usage" block or "help" text.

Edit: I think I figured out what I was looking for:

    const params = comptime [_]clap.Param(clap.Help){
        clap.parseParam("-h, --help             Display this help and exit.") catch unreachable,
        clap.parseParam("-t, --trails <NUM>     Show trails per touch point.") catch unreachable,
    };

    var diag = clap.Diagnostic{};
    var args = clap.parse(clap.Help, &params, .{ .diagnostic = &diag }) catch |err| {
        diag.report(std.io.getStdErr().writer(), err) catch {};
        return err;
    };
    defer args.deinit();

    if (args.flag("--help")) {
        try clap.help(std.io.getStdErr().writer(), &params);  // <--- THIS is what I needed :)
        std.os.exit(1);
    }
    if (args.option("--trails")) |n| {
        std.debug.print("--trails = {s}\n", .{n});
        std.os.exit(1);
    }
@Hejsil
Copy link
Owner

Hejsil commented Nov 29, 2021

Yea, you seem to have figured it out, but I can see how one would expect the help example to be a program that actually only printed help when --help was passed. I might try to make this a little more clear later today if I get the time. PRs are always welcome if I don't get around to it.

@canadaduane
Copy link
Author

canadaduane commented Nov 29, 2021

Cool, thanks for the feedback. BTW is what I did in that code snippet "standard"? I couldn't find the pattern anywhere in the examples--but it seems like the "obvious" thing one would want to do when writing both an argument parser and its corresponding help documentation.

@Hejsil
Copy link
Owner

Hejsil commented Nov 29, 2021

Cool, thanks for the feedback. BTW is what I did in that code snippet "standard"? I couldn't find the pattern anywhere in the examples--but it seems like the "obvious" thing one would want to do when writing both an argument parser and its corresponding help documentation.

Yep, this is the "standard" way (aka the way I do it)

Hejsil added a commit that referenced this issue Nov 29, 2021
Instead of just calling these function, have the examples be small
programs that demonstrates how you would actually use them together with
argument parsing.

fixes #57
@Hejsil
Copy link
Owner

Hejsil commented Nov 29, 2021

Can you have a look here to see if this is a good enough improvement?
#59

Hejsil added a commit that referenced this issue Nov 29, 2021
Instead of just calling these function, have the examples be small
programs that demonstrates how you would actually use them together with
argument parsing.

fixes #57
@canadaduane
Copy link
Author

OMG that is PERFECT! Thank you :)

Hejsil added a commit that referenced this issue Nov 29, 2021
Instead of just calling these function, have the examples be small
programs that demonstrates how you would actually use them together with
argument parsing.

fixes #57
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

Successfully merging a pull request may close this issue.

2 participants