get: start work on get command

This commit is contained in:
Pierre Tachoire
2023-12-20 11:08:17 +01:00
parent 1642741027
commit df2e6dcfc0
2 changed files with 76 additions and 7 deletions

57
src/main_get.zig Normal file
View File

@@ -0,0 +1,57 @@
const std = @import("std");
const b = @import("browser/browser.zig");
pub const std_options = struct {
pub const log_level = .debug;
};
const usage =
\\usage: {s} [options] <url>
\\ request the url with the browser
\\
\\ -h, --help Print this help message and exit.
\\
;
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer {
const check = gpa.deinit();
if (check == .leak) {
std.log.warn("leaks detected\n", .{});
}
}
const allocator = gpa.allocator();
var args = try std.process.argsWithAllocator(allocator);
defer args.deinit();
const execname = args.next().?;
var url: []const u8 = "";
while (args.next()) |arg| {
if (std.mem.eql(u8, "-h", arg) or std.mem.eql(u8, "--help", arg)) {
try std.io.getStdErr().writer().print(usage, .{execname});
std.os.exit(0);
}
// allow only one url
if (url.len != 0) {
try std.io.getStdErr().writer().print(usage, .{execname});
std.os.exit(1);
}
url = arg;
}
if (url.len == 0) {
try std.io.getStdErr().writer().print(usage, .{execname});
std.os.exit(1);
}
var browser = b.Browser.init(allocator);
defer browser.deinit();
var page = try browser.currentSession().createPage();
defer page.deinit();
try page.navigate(url);
}