Hejsil / zig-clap

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

`help` example doesn't show version #57

Closed canadaduane closed 2 years ago

canadaduane commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

Can you have a look here to see if this is a good enough improvement? https://github.com/Hejsil/zig-clap/pull/59

canadaduane commented 2 years ago

OMG that is PERFECT! Thank you :)