Hejsil / zig-clap

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

Enum parser #78

Closed tealsnow closed 2 years ago

tealsnow commented 2 years ago

The ability to specify an enum as a possible values parser for an option.

const MyEnum = enum {
    foo,
    bar,
};

const params = comptime clap.parseParamsComptime(
    \\-E, --my_enum <MyEnum>
);

const parsers = comptime .{
    .MyEnum = clap.parsers.enumParser(MyEnum),
};

var res = clap.parse(clap.Help, &params, parsers, .{}) catch unreachable;
defer res.deinit();

if (res.args.my_enum) |e| {
    const my_enum: MyEnum = e;
}

// usage:
// --my_enum foo --my_enum=bar
// -Efoo -E bar