flags: rewrite to allow [:0]const u8 arguments

This also cleans up the code by using @Type(), eliminating the need
for the argFlag() and boolFlag() functions.

Allowing [:0]const u8 arguments makes this parser useful for
river-control commands as well.
This commit is contained in:
Isaac Freund
2022-12-28 19:47:09 +01:00
parent ad1dbb1180
commit 16cbe5f469
4 changed files with 99 additions and 104 deletions

View File

@ -65,18 +65,18 @@ pub fn main() !void {
fn _main() !void {
// This line is here because of https://github.com/ziglang/zig/issues/7807
const argv: [][*:0]const u8 = os.argv;
const result = flags.parse(argv[1..], &[_]flags.Flag{
const result = flags.parser([*:0]const u8, &.{
.{ .name = "-h", .kind = .boolean },
.{ .name = "-version", .kind = .boolean },
}) catch {
}).parse(argv[1..]) catch {
try io.getStdErr().writeAll(usage);
os.exit(1);
};
if (result.boolFlag("-h")) {
if (result.flags.@"-h") {
try io.getStdOut().writeAll(usage);
os.exit(0);
}
if (result.boolFlag("-version")) {
if (result.flags.@"-version") {
try io.getStdOut().writeAll(@import("build_options").version ++ "\n");
os.exit(0);
}