Skip to content

Commit 9ab6d91

Browse files
authored
zig std accepts --port and --no-open-browser (#19559)
1 parent 637b1d6 commit 9ab6d91

File tree

1 file changed

+50
-10
lines changed

1 file changed

+50
-10
lines changed

lib/compiler/std-docs.zig

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,24 @@
11
const builtin = @import("builtin");
22
const std = @import("std");
3+
const mem = std.mem;
4+
const io = std.io;
35
const Allocator = std.mem.Allocator;
46
const assert = std.debug.assert;
57

8+
fn usage() noreturn {
9+
io.getStdOut().writeAll(
10+
\\Usage: zig std [options]
11+
\\
12+
\\Options:
13+
\\ -h, --help Print this help and exit
14+
\\ -p [port], --port [port] Port to listen on. Default is 0, meaning an ephemeral port chosen by the system.
15+
\\ --[no-]open-browser Force enabling or disabling opening a browser tab to the served website.
16+
\\ By default, enabled unless a port is specified.
17+
\\
18+
) catch {};
19+
std.process.exit(1);
20+
}
21+
622
pub fn main() !void {
723
var arena_instance = std.heap.ArenaAllocator.init(std.heap.page_allocator);
824
defer arena_instance.deinit();
@@ -11,23 +27,47 @@ pub fn main() !void {
1127
var general_purpose_allocator: std.heap.GeneralPurposeAllocator(.{}) = .{};
1228
const gpa = general_purpose_allocator.allocator();
1329

14-
const args = try std.process.argsAlloc(arena);
15-
const zig_lib_directory = args[1];
16-
const zig_exe_path = args[2];
17-
const global_cache_path = args[3];
30+
var argv = try std.process.argsWithAllocator(arena);
31+
defer argv.deinit();
32+
assert(argv.skip());
33+
const zig_lib_directory = argv.next().?;
34+
const zig_exe_path = argv.next().?;
35+
const global_cache_path = argv.next().?;
1836

1937
var lib_dir = try std.fs.cwd().openDir(zig_lib_directory, .{});
2038
defer lib_dir.close();
2139

22-
const listen_port: u16 = 0;
40+
var listen_port: u16 = 0;
41+
var force_open_browser: ?bool = null;
42+
while (argv.next()) |arg| {
43+
if (mem.eql(u8, arg, "-h") or mem.eql(u8, arg, "--help")) {
44+
usage();
45+
} else if (mem.eql(u8, arg, "-p") or mem.eql(u8, arg, "--port")) {
46+
listen_port = std.fmt.parseInt(u16, argv.next() orelse usage(), 10) catch |err| {
47+
std.log.err("expected port number: {}", .{err});
48+
usage();
49+
};
50+
} else if (mem.eql(u8, arg, "--open-browser")) {
51+
force_open_browser = true;
52+
} else if (mem.eql(u8, arg, "--no-open-browser")) {
53+
force_open_browser = false;
54+
} else {
55+
std.log.err("unrecognized argument: {s}", .{arg});
56+
usage();
57+
}
58+
}
59+
const should_open_browser = force_open_browser orelse (listen_port == 0);
60+
2361
const address = std.net.Address.parseIp("127.0.0.1", listen_port) catch unreachable;
2462
var http_server = try address.listen(.{});
2563
const port = http_server.listen_address.in.getPort();
26-
const url = try std.fmt.allocPrint(arena, "http://127.0.0.1:{d}/\n", .{port});
27-
std.io.getStdOut().writeAll(url) catch {};
28-
openBrowserTab(gpa, url[0 .. url.len - 1 :'\n']) catch |err| {
29-
std.log.err("unable to open browser: {s}", .{@errorName(err)});
30-
};
64+
const url_with_newline = try std.fmt.allocPrint(arena, "http://127.0.0.1:{d}/\n", .{port});
65+
std.io.getStdOut().writeAll(url_with_newline) catch {};
66+
if (should_open_browser) {
67+
openBrowserTab(gpa, url_with_newline[0 .. url_with_newline.len - 1 :'\n']) catch |err| {
68+
std.log.err("unable to open browser: {s}", .{@errorName(err)});
69+
};
70+
}
3171

3272
var context: Context = .{
3373
.gpa = gpa,

0 commit comments

Comments
 (0)