Add per-subcommand help via help or --help argument

`lightpanda <subcommand> help`, `lightpanda <subcommand> --help`
now print only the relevant subcommand options plus common options,
instead of the full text.

`lightpanda help <subcommand>` is also supported
(and that's what use internally).
This commit is contained in:
Francis Bouvier
2026-05-11 22:16:52 +02:00
parent e56bd0862a
commit 31ef5246bc
2 changed files with 74 additions and 15 deletions

View File

@@ -201,7 +201,7 @@ const Commands = cli.Builder(.{
.shared_options = CommonOptions,
},
.{ .name = "version", .options = .{} },
.{ .name = "help", .options = .{} },
.{ .name = "help", .positional = .{ .name = "subcommand", .type = ?[]const u8 }, .options = .{} },
});
pub const RunMode = Commands.Enum;
@@ -623,11 +623,7 @@ pub fn printUsageAndExit(self: *const Config, success: bool) void {
;
// MAX_HELP_LEN|
const usage =
\\usage: {0s} command [options] [URL]
\\
\\Command can be either 'fetch', 'serve', 'mcp' or 'help'
\\
const fetch_options =
\\fetch command
\\Fetches the specified URL
\\Example: {0s} fetch --dump html https://lightpanda.io/
@@ -686,11 +682,12 @@ pub fn printUsageAndExit(self: *const Config, success: bool) void {
\\ Defaults to no cookie loading.
\\
\\--cookie-jar Path to a JSON file to save cookies to on exit (write-only).
\\ Available for fetch and mcp commands.
\\ Defaults to no cookie saving.
\\
++ common_options ++
\\
++ common_options;
// MAX_HELP_LEN|
const serve_options =
\\serve command
\\Starts a websocket CDP server
\\Example: {0s} serve --host 127.0.0.1 --port 9222
@@ -718,20 +715,37 @@ pub fn printUsageAndExit(self: *const Config, success: bool) void {
\\--cookie Path to a JSON file to load cookies from (read-only).
\\ Defaults to no cookie loading.
\\
++ common_options ++
\\
++ common_options;
// MAX_HELP_LEN|
const mcp_options =
\\mcp command
\\Starts an MCP (Model Context Protocol) server over stdio
\\Example: {0s} mcp
\\
\\Options:
\\--cookie Path to a JSON file to load cookies from (read-only).
\\ Defaults to no cookie loading.
\\
\\--cookie-jar Path to a JSON file to save cookies to on exit (write-only).
\\ Available for fetch and mcp commands.
\\ Defaults to no cookie saving.
\\
++ common_options ++
++ common_options;
// MAX_HELP_LEN|
const usage =
\\usage: {0s} command [options] [URL]
\\
\\Command can be either 'fetch', 'serve', 'mcp' or 'help'
\\
++ fetch_options ++
\\
\\
++ serve_options ++
\\
\\
++ mcp_options ++
\\
\\
\\version command
\\Displays the version of {0s}
@@ -740,6 +754,26 @@ pub fn printUsageAndExit(self: *const Config, success: bool) void {
\\Displays this message
\\
;
// When called with a subcommand argument,
// print only the relevant subcommand section instead of the full help.
switch (self.mode) {
.help => |h| if (h.subcommand) |sub| {
if (std.mem.eql(u8, sub, "fetch")) {
std.debug.print(fetch_options ++ "\n", .{self.exec_name});
} else if (std.mem.eql(u8, sub, "serve")) {
std.debug.print(serve_options ++ "\n", .{self.exec_name});
} else if (std.mem.eql(u8, sub, "mcp")) {
std.debug.print(mcp_options ++ "\n", .{self.exec_name});
} else {
std.debug.print(usage, .{self.exec_name});
}
if (success) return std.process.cleanExit();
std.process.exit(1);
},
else => {},
}
std.debug.print(usage, .{self.exec_name});
if (success) {
return std.process.cleanExit();