Skip to content

Commit ca788a2

Browse files
committed
awtfdb-watcher: migrate to clap
1 parent ef2f239 commit ca788a2

File tree

1 file changed

+32
-21
lines changed

1 file changed

+32
-21
lines changed

src/rename_watcher_main.zig

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,14 @@ const std = @import("std");
22
const sqlite = @import("sqlite");
33
const manage_main = @import("main.zig");
44
const Context = manage_main.Context;
5+
const clap = @import("clap");
56
const ID = manage_main.ID;
67
const ExpiringHashMap = @import("expiring_hash_map").ExpiringHashMap;
78

89
const logger = std.log.scoped(.awtfdb_watcher);
910

1011
const VERSION = "0.0.1";
1112
const HELPTEXT =
12-
\\ awtfdb-watcher: watch the entire operating system for renames,
13-
\\ updating the database with such
14-
\\
15-
\\ currently only supports linux with bpftrace installed.
16-
\\
17-
\\ MUST be run as root.
1813
\\
1914
\\ usage:
2015
\\ awtfdb-watcher [options...] path_to_home_directory
@@ -534,8 +529,30 @@ pub fn main() anyerror!void {
534529
defer _ = gpa.deinit();
535530
const allocator = gpa.allocator();
536531

537-
var args_it = std.process.args();
538-
_ = args_it.skip();
532+
const params = comptime clap.parseParamsComptime(
533+
\\-h, --help display this help and exit.
534+
\\-V, --version print version and exit.
535+
\\-v, --verbose enable debug logs.
536+
\\<str> home path of the user to watch renames for
537+
\\
538+
\\ awtfdb-watcher: watch the entire operating system for renames,
539+
\\ updating the database with such
540+
\\
541+
\\ currently only supports linux with bpftrace installed.
542+
\\
543+
\\ MUST be run as root.
544+
);
545+
546+
var diag = clap.Diagnostic{};
547+
var res = clap.parse(clap.Help, &params, clap.parsers.default, .{
548+
.diagnostic = &diag,
549+
.allocator = allocator,
550+
}) catch |err| {
551+
// Report useful error and exit.
552+
diag.report(std.io.getStdErr().writer(), err) catch {};
553+
return err;
554+
};
555+
defer res.deinit();
539556

540557
const Args = struct {
541558
help: bool = false,
@@ -544,21 +561,15 @@ pub fn main() anyerror!void {
544561
};
545562

546563
var given_args = Args{};
547-
while (args_it.next()) |arg| {
548-
if (std.mem.eql(u8, arg, "-h")) {
549-
given_args.help = true;
550-
} else if (std.mem.eql(u8, arg, "-V")) {
551-
given_args.version = true;
552-
} else if (std.mem.eql(u8, arg, "-v")) {
553-
current_log_level = .debug;
554-
} else {
555-
given_args.home_path = arg;
556-
}
557-
}
558564

565+
given_args.help = res.args.help != 0;
566+
given_args.version = res.args.version != 0;
567+
if (res.args.verbose != 0)
568+
current_log_level = .debug;
569+
570+
given_args.home_path = res.positionals[0];
559571
if (given_args.help) {
560-
std.debug.print(HELPTEXT, .{});
561-
return;
572+
return clap.help(std.io.getStdErr().writer(), clap.Help, &params, .{});
562573
} else if (given_args.version) {
563574
std.debug.print("awtfdb-watcher {s}\n", .{VERSION});
564575
return;

0 commit comments

Comments
 (0)