Hejsil / zig-clap

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

How to deal with InvalidaArgument error #105

Closed bq-wrongway closed 1 year ago

Hejsil commented 1 year ago

I need a little more information about what you are trying to do. What is the code? What are you passing as arguments to your program?

bq-wrongway commented 1 year ago

Sorry, i wanted to ask question, then i dont have an issue with software but mostyly i am not sure how to handle an error

Thank you for a reply, and if you care to help here is code snipped

    var diag = clap.Diagnostic{};
    var res = clap.parse(clap.Help, &params, parsers, .{
        .diagnostic = &diag,
    }) catch |err| {
        // Report useful error and exit
        diag.report(io.getStdErr().writer(), err) catch {};
        return err;
    };

I mapped for example, s, h and l, and if someone passes those values i provide feedback. But if someone provides p as a value i return an errorstack. How could i print out in that case : The passed argument is not a commend please type --help to see more info.

Hejsil commented 1 year ago

I see. You can do whatever you want in the catch |err| block. The error stack trace is a Zig feature and will be printed when an error is returned from main. To do something else, you have main return u8 and return 1:

...

pub fn main() u8 {
    ...

    var diag = clap.Diagnostic{};
    var res = clap.parse(clap.Help, &params, parsers, .{
        .diagnostic = &diag,
    }) catch |err| {
        const stderr = std.io.getStdErr().writer();
        diag.report(stderr, err) catch {};
        stderr.print("Use --help to see more info.\n");
        return 1;
    };

    ...
}
bq-wrongway commented 1 year ago

Yes this worked. From my point you can close this, and thank you very much!