zig-clap icon indicating copy to clipboard operation
zig-clap copied to clipboard

Support default values for short and long parameters

Open tensorush opened this issue 9 months ago • 4 comments

It's a very minor nitpick, but I found myself trying to communicate to the user the default values of different parameters in their descriptions, and then reusing those same values when parsing:

const PARAMS = clap.parseParamsComptime(
    \\-o, --opt <u8>   Optional argument (default: 3)
    \\
);

pub fn main() !void {
    ...
    const opt_arg = cli.args.opt orelse 3;
    ...
}

It's not very error-prone as is, since default values don't usually change much, but it would be nicer to have a way of specifying default values and having them parsed automatically.

tensorush avatar Feb 23 '25 08:02 tensorush

Yea, that does seem worth while. I'm not sure it is something I'll really get to doing as I'm not using zig-clap that much myself anymore.

Hejsil avatar Feb 23 '25 11:02 Hejsil

What are you using then? If I may ask

Mario-SO avatar Mar 07 '25 23:03 Mario-SO

What are you using then? If I may ask

Currently, I'm testing out something simpler:

https://github.com/Hejsil/dipm/blob/4b24131e2b010ef8577fb650ca39d2305e6b3b4e/src/ArgParser.zig Usage: https://github.com/Hejsil/dipm/blob/4b24131e2b010ef8577fb650ca39d2305e6b3b4e/src/main.zig#L125

I just have a copy of this file in a few projects

Hejsil avatar Mar 07 '25 23:03 Hejsil

Btw, I've rewritten structopt to support ZON-like structs, no fancy parser, but pretty nice to use (will be even nicer after we're able to import ZON files without result types):

https://codeberg.org/tensorush/argzon

tensorush avatar Mar 08 '25 12:03 tensorush