Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

✨ feat(cli): add generated man pages (#13200)

Innei6e3053fcb3f5
diff --git a/apps/cli/man/man1/lh.1 b/apps/cli/man/man1/lh.1
new file mode 100644
index 00000000000..f95314d1eff
--- /dev/null
+++ b/apps/cli/man/man1/lh.1
@@ -0,0 +1,160 @@
+.\" Code generated by `npm run man:generate`; DO NOT EDIT.
+.\" Manual command details come from the Commander command tree.
+.TH LH 1 "" "@lobehub/cli 0.0.1\-canary.12" "User Commands"
+.SH NAME
+lh \- LobeHub CLI \- manage and connect to LobeHub services
+.SH SYNOPSIS
+.B lh
+[\fIOPTION\fR]...
+[\fICOMMAND\fR]
+.br
+.B lobe
+[\fIOPTION\fR]...
+[\fICOMMAND\fR]
+.br
+.B lobehub
+[\fIOPTION\fR]...
+[\fICOMMAND\fR]
+.SH DESCRIPTION
+lh is the command\-line interface for LobeHub. It provides authentication, device gateway connectivity, content generation, resource search, and management commands for agents, files, models, providers, plugins, knowledge bases, threads, topics, and related resources.
+.PP
+For command-specific manuals, use the built-in manual command:
+.PP
+.RS
+.B lh man
+[\fICOMMAND\fR]...
+.RE
+.SH COMMANDS
+.TP
+.B login
+Log in to LobeHub via browser (Device Code Flow)
+.TP
+.B logout
+Log out and remove stored credentials
+.TP
+.B completion
+Output shell completion script
+.TP
+.B man
+Show a manual page for the CLI or a subcommand
+.TP
+.B connect
+Connect to the device gateway and listen for tool calls
+.TP
+.B device
+Manage connected devices
+.TP
+.B status
+Check if gateway connection can be established
+.TP
+.B doc
+Manage documents
+.TP
+.B search
+Search across local resources or the web
+.TP
+.B kb
+Manage knowledge bases, folders, documents, and files
+.TP
+.B memory
+Manage user memories
+.TP
+.B agent
+Manage agents
+.TP
+.B agent\-group
+Manage agent groups
+.TP
+.B bot
+Manage bot integrations
+.TP
+.B cron
+Manage agent cron jobs
+.TP
+.B generate
+Generate content (text, image, video, speech) Alias: gen.
+.TP
+.B file
+Manage files
+.TP
+.B skill
+Manage agent skills
+.TP
+.B session\-group
+Manage agent session groups
+.TP
+.B thread
+Manage message threads
+.TP
+.B topic
+Manage conversation topics
+.TP
+.B message
+Manage messages
+.TP
+.B model
+Manage AI models
+.TP
+.B provider
+Manage AI providers
+.TP
+.B plugin
+Manage plugins
+.TP
+.B user
+Manage user account and settings
+.TP
+.B whoami
+Display current user information
+.TP
+.B usage
+View usage statistics
+.TP
+.B eval
+Manage evaluation workflows
+.SH OPTIONS
+.TP
+.B \-V, \-\-version
+output the version number
+.TP
+.B \-h, \-\-help
+display help for command
+.SH FILES
+.TP
+.I ~/.lobehub/credentials.json
+Encrypted access and refresh tokens.
+.TP
+.I ~/.lobehub/settings.json
+CLI settings such as server and gateway URLs.
+.TP
+.I ~/.lobehub/daemon.pid
+Background daemon PID file.
+.TP
+.I ~/.lobehub/daemon.status
+Background daemon status metadata.
+.TP
+.I ~/.lobehub/daemon.log
+Background daemon log output.
+.PP
+The base directory can be overridden with the
+.B LOBEHUB_CLI_HOME
+environment variable.
+.SH EXAMPLES
+.TP
+.B lh login
+Start interactive login in the browser.
+.TP
+.B lh connect \-\-daemon
+Start the device gateway connection in the background.
+.TP
+.B lh search \-q "gpt\-5"
+Search local resources for a query.
+.TP
+.B lh generate text "Write release notes"
+Generate text from a prompt.
+.TP
+.B lh man generate
+Show the built\-in manual for the generate command group.
+.SH SEE ALSO
+.BR lobe (1),
+.BR lobehub (1)
diff --git a/apps/cli/man/man1/lobe.1 b/apps/cli/man/man1/lobe.1
new file mode 100644
index 00000000000..7f278c9c25d
--- /dev/null
+++ b/apps/cli/man/man1/lobe.1
@@ -0,0 +1 @@
+.so man1/lh.1
diff --git a/apps/cli/man/man1/lobehub.1 b/apps/cli/man/man1/lobehub.1
new file mode 100644
index 00000000000..7f278c9c25d
--- /dev/null
+++ b/apps/cli/man/man1/lobehub.1
@@ -0,0 +1 @@
+.so man1/lh.1
diff --git a/apps/cli/package.json b/apps/cli/package.json
index 6e3fad7b221..898404b1227 100644
--- a/apps/cli/package.json
+++ b/apps/cli/package.json
@@ -7,15 +7,22 @@
"lobe": "./dist/index.js",
"lobehub": "./dist/index.js"
},
+ "man": [
+ "./man/man1/lh.1",
+ "./man/man1/lobe.1",
+ "./man/man1/lobehub.1"
+ ],
"files": [
- "dist"
+ "dist",
+ "man"
],
"scripts": {
"build": "tsdown",
"cli:link": "bun link",
"cli:unlink": "bun unlink",
"dev": "LOBEHUB_CLI_HOME=.lobehub-dev bun src/index.ts",
- "prepublishOnly": "npm run build",
+ "man:generate": "bun src/man/generate.ts",
+ "prepublishOnly": "npm run build && npm run man:generate",
"test": "bunx vitest run --config vitest.config.mts --silent='passed-only'",
"test:coverage": "bunx vitest run --config vitest.config.mts --coverage",
"type-check": "tsc --noEmit"
diff --git a/apps/cli/src/commands/man.test.ts b/apps/cli/src/commands/man.test.ts
new file mode 100644
index 00000000000..d844afb65fa
--- /dev/null
+++ b/apps/cli/src/commands/man.test.ts
@@ -0,0 +1,85 @@
+import { Command } from 'commander';
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
+
+import { registerManCommand } from './man';
+
+describe('man command', () => {
+ let consoleSpy: ReturnType<typeof vi.spyOn>;
+
+ beforeEach(() => {
+ consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
+ });
+
+ afterEach(() => {
+ consoleSpy.mockRestore();
+ });
+
+ function createProgram() {
+ const program = new Command();
+
+ program.name('lh').description('Sample CLI').version('1.0.0');
+
+ const generate = program
+ .command('generate')
+ .alias('gen')
+ .description('Generate content')
+ .option('-m, --model <model>', 'Model to use');
+
+ generate
+ .command('text <prompt>')
+ .description('Generate text from a prompt')
+ .option('--json', 'Output raw JSON');
+
+ program.command('login').description('Log in to LobeHub');
+
+ registerManCommand(program);
+ program.exitOverride();
+
+ return program;
+ }
+
+ it('renders a manual page for the root command', async () => {
+ const program = createProgram();
+
+ await program.parseAsync(['node', 'test', 'man']);
+
+ const output = consoleSpy.mock.calls.at(0)?.[0];
+
+ expect(output).toContain('LH(1)');
+ expect(output).toContain('NAME\n lh - Sample CLI');
+ expect(output).toContain('ALIASES\n lobe, lobehub');
+ expect(output).toContain('SYNOPSIS\n lh [options] [command]');
+ expect(output).toContain('generate|gen [options] [command]');
+ expect(output).toContain('man [options] [command...]');
+ });
+
+ it('renders a manual page for a command with subcommands', async () => {
+ const program = createProgram();
+
+ await program.parseAsync(['node', 'test', 'man', 'generate']);
+
+ const output = consoleSpy.mock.calls.at(0)?.[0];
+
+ expect(output).toContain('LH-GENERATE(1)');
+ expect(output).toContain('NAME\n lh generate - Generate content');
+ expect(output).toContain('ALIASES\n gen');
+ expect(output).toContain('SYNOPSIS\n lh generate [options] [command]');
+ expect(output).toContain('text [options] <prompt>');
+ expect(output).toContain('-m, --model <model>');
+ });
+
+ it('renders arguments for a leaf command', async () => {
+ const program = createProgram();
+
+ await program.parseAsync(['node', 'test', 'man', 'generate', 'text']);
+
+ const output = consoleSpy.mock.calls.at(0)?.[0];
+
+ expect(output).toContain('LH-GENERATE-TEXT(1)');
+ expect(output).toContain('NAME\n lh generate text - Generate text from a prompt');
+ expect(output).toContain('ARGUMENTS');
+ expect(output).toContain('<prompt>');
+ expect(output).toContain('Required argument');
+ expect(output).toContain('SEE ALSO');
+ });
+});
diff --git a/apps/cli/src/commands/man.ts b/apps/cli/src/commands/man.ts
new file mode 100644
index 00000000000..6bfd31696b5
--- /dev/null
+++ b/apps/cli/src/commands/man.ts
@@ -0,0 +1,212 @@
+import type { Argument, Command } from 'commander';
+
+const ROOT_ALIASES = ['lobe', 'lobehub'];
+const HELP_COMMAND_NAME = 'help';
+
+interface DefinitionItem {
+ description: string;
+ term: string;
+}
+
+interface ResolutionResult {
+ command?: Command;
+ error?: string;
+}
+
+export function registerManCommand(program: Command) {
+ program
+ .command('man [command...]')
+ .description('Show a manual page for the CLI or a subcommand')
+ .action((commandPath: string[] | undefined) => {
+ const segments = commandPath ?? [];
+ const resolution = resolveCommandPath(program, segments);
+
+ if (!resolution.command) {
+ program.error(resolution.error || 'Unknown command path.');
+ return;
+ }
+
+ console.log(renderManualPage(program, resolution.command));
+ });
+}
+
+function resolveCommandPath(root: Command, segments: string[]): ResolutionResult {
+ let current = root;
+
+ for (const segment of segments) {
+ const next = getVisibleCommands(current).find(
+ (command) => command.name() === segment || command.aliases().includes(segment),
+ );
+
+ if (!next) {
+ const currentPath = buildCommandPath(current).join(' ');
+ const available = getVisibleCommands(current)
+ .map((command) => command.name())
+ .join(', ');
+
+ return {
+ error: `Unknown command "${segment}" under "${currentPath}". Available: ${available || 'none'}.`,
+ };
+ }
+
+ current = next;
+ }
+
+ return { command: current };
+}
+
+function renderManualPage(root: Command, command: Command) {
+ const sections = [
+ formatManualHeader(command),
+ formatNameSection(command),
+ formatSynopsisSection(root, command),
+ formatAliasesSection(command),
+ formatDescriptionSection(command),
+ formatArgumentsSection(command),
+ formatCommandsSection(command),
+ formatOptionsSection(command),
+ formatSeeAlsoSection(root, command),
+ ].filter(Boolean);
+
+ return sections.join('\n\n');
+}
+
+function formatManualHeader(command: Command) {
+ return `${buildCommandPath(command).join('-').toUpperCase()}(1)`;
+}
+
+function formatNameSection(command: Command) {
+ return ['NAME', ` ${buildCommandPath(command).join(' ')} - ${command.description()}`].join('\n');
+}
+
+function formatSynopsisSection(root: Command, command: Command) {
+ return ['SYNOPSIS', ` ${buildSynopsis(root, command)}`].join('\n');
+}
+
+function formatAliasesSection(command: Command) {
+ const aliases = command.parent ? command.aliases() : ROOT_ALIASES;
+
+ if (aliases.length === 0) return '';
+
+ return ['ALIASES', ` ${aliases.join(', ')}`].join('\n');
+}
+
+function formatDescriptionSection(command: Command) {
+ const description = command.description() || 'No description available.';
+
+ return ['DESCRIPTION', ` ${description}`].join('\n');
+}
+
+function formatArgumentsSection(command: Command) {
+ if (command.registeredArguments.length === 0) return '';
+
+ const items = command.registeredArguments.map((argument) => ({
+ description: describeArgument(argument),
+ term: formatArgumentTerm(argument),
+ }));
+
+ return ['ARGUMENTS', ...formatDefinitionList(items)].join('\n');
+}
+
+function formatCommandsSection(command: Command) {
+ const help = command.createHelp();
+ const items = getVisibleCommands(command).map((subcommand) => ({
+ description: help.subcommandDescription(subcommand),
+ term: buildSubcommandTerm(subcommand),
+ }));
+
+ if (items.length === 0) return '';
+
+ return ['COMMANDS', ...formatDefinitionList(items)].join('\n');
+}
+
+function formatOptionsSection(command: Command) {
+ const help = command.createHelp();
+ const items = help.visibleOptions(command).map((option) => ({
+ description: help.optionDescription(option),
+ term: help.optionTerm(option),
+ }));
+
+ if (items.length === 0) return '';
+
+ return ['OPTIONS', ...formatDefinitionList(items)].join('\n');
+}
+
+function formatSeeAlsoSection(root: Command, command: Command) {
+ const items = new Set<string>();
+ const currentPath = buildCommandPath(command);
+
+ items.add(`${currentPath.join(' ')} --help`);
+
+ const parent = command.parent;
+ if (parent) {
+ const parentPath = buildCommandPath(parent).slice(1).join(' ');
+ items.add(parentPath ? `lh man ${parentPath}` : 'lh man');
+ }
+
+ for (const subcommand of getVisibleCommands(command).slice(0, 5)) {
+ items.add(`lh man ${buildCommandPath(subcommand).slice(1).join(' ')}`);
+ }
+
+ return ['SEE ALSO', ...Array.from(items).map((item) => ` ${item}`)].join('\n');
+}
+
+function getVisibleCommands(command: Command) {
+ const help = command.createHelp();
+
+ return help
+ .visibleCommands(command)
+ .filter((subcommand) => subcommand.name() !== HELP_COMMAND_NAME);
+}
+
+function buildSynopsis(root: Command, command: Command) {
+ const path = buildCommandPath(command);
+
+ if (command === root) {
+ return `${path[0]} ${command.usage()}`.trim();
+ }
+
+ return `${path.join(' ')} ${command.usage()}`.trim();
+}
+
+function buildCommandPath(command: Command): string[] {
+ const path: string[] = [];
+ let current: Command | null = command;
+
+ while (current) {
+ path.unshift(current.name());
+ current = current.parent || null;
+ }
+
+ return path;
+}
+
+function buildSubcommandTerm(command: Command) {
+ const name = [command.name(), ...command.aliases()].join('|');
+ const usage = command.usage();
+
+ return usage ? `${name} ${usage}` : name;
+}
+
+function formatDefinitionList(items: DefinitionItem[]) {
+ const width = Math.max(...items.map((item) => item.term.length));
+
+ return items.map((item) => ` ${item.term.padEnd(width)} ${item.description}`);
+}
+
+function formatArgumentTerm(argument: Argument) {
+ const name = argument.name();
+
+ if (argument.required) {
+ return argument.variadic ? `<${name}...>` : `<${name}>`;
+ }
+
+ return argument.variadic ? `[${name}...]` : `[${name}]`;
+}
+
+function describeArgument(argument: Argument) {
+ const required = argument.required ? 'Required' : 'Optional';
+ const variadic = argument.variadic ? 'variadic ' : '';
+
+ return `${required} ${variadic}argument`;
+}
diff --git a/apps/cli/src/index.ts b/apps/cli/src/index.ts
index 1b9de4d2491..004bf1cbe78 100644
--- a/apps/cli/src/index.ts
+++ b/apps/cli/src/index.ts
@@ -1,71 +1,3 @@
-import { createRequire } from 'node:module';
+import { createProgram } from './program';
-import { Command } from 'commander';
-
-import { registerAgentCommand } from './commands/agent';
-import { registerAgentGroupCommand } from './commands/agent-group';
-import { registerBotCommand } from './commands/bot';
-import { registerCompletionCommand } from './commands/completion';
-import { registerConfigCommand } from './commands/config';
-import { registerConnectCommand } from './commands/connect';
-import { registerCronCommand } from './commands/cron';
-import { registerDeviceCommand } from './commands/device';
-import { registerDocCommand } from './commands/doc';
-import { registerEvalCommand } from './commands/eval';
-import { registerFileCommand } from './commands/file';
-import { registerGenerateCommand } from './commands/generate';
-import { registerKbCommand } from './commands/kb';
-import { registerLoginCommand } from './commands/login';
-import { registerLogoutCommand } from './commands/logout';
-import { registerMemoryCommand } from './commands/memory';
-import { registerMessageCommand } from './commands/message';
-import { registerModelCommand } from './commands/model';
-import { registerPluginCommand } from './commands/plugin';
-import { registerProviderCommand } from './commands/provider';
-import { registerSearchCommand } from './commands/search';
-import { registerSessionGroupCommand } from './commands/session-group';
-import { registerSkillCommand } from './commands/skill';
-import { registerStatusCommand } from './commands/status';
-import { registerThreadCommand } from './commands/thread';
-import { registerTopicCommand } from './commands/topic';
-import { registerUserCommand } from './commands/user';
-
-const require = createRequire(import.meta.url);
-const { version } = require('../package.json');
-
-const program = new Command();
-
-program
- .name('lh')
- .description('LobeHub CLI - manage and connect to LobeHub services')
- .version(version);
-
-registerLoginCommand(program);
-registerLogoutCommand(program);
-registerCompletionCommand(program);
-registerConnectCommand(program);
-registerDeviceCommand(program);
-registerStatusCommand(program);
-registerDocCommand(program);
-registerSearchCommand(program);
-registerKbCommand(program);
-registerMemoryCommand(program);
-registerAgentCommand(program);
-registerAgentGroupCommand(program);
-registerBotCommand(program);
-registerCronCommand(program);
-registerGenerateCommand(program);
-registerFileCommand(program);
-registerSkillCommand(program);
-registerSessionGroupCommand(program);
-registerThreadCommand(program);
-registerTopicCommand(program);
-registerMessageCommand(program);
-registerModelCommand(program);
-registerProviderCommand(program);
-registerPluginCommand(program);
-registerUserCommand(program);
-registerConfigCommand(program);
-registerEvalCommand(program);
-
-program.parse();
+createProgram().parse();
diff --git a/apps/cli/src/man/generate.ts b/apps/cli/src/man/generate.ts
new file mode 100644
index 00000000000..26b3e33f3c0
--- /dev/null
+++ b/apps/cli/src/man/generate.ts
@@ -0,0 +1,17 @@
+import { mkdir, writeFile } from 'node:fs/promises';
+import { fileURLToPath } from 'node:url';
+
+import { cliVersion, createProgram } from '../program';
+import { generateAliasManPage, generateRootManPage } from './roff';
+
+const outputDir = fileURLToPath(new URL('../../man/man1/', import.meta.url));
+
+await mkdir(outputDir, { recursive: true });
+
+const program = createProgram();
+
+await Promise.all([
+ writeFile(`${outputDir}lh.1`, generateRootManPage(program, cliVersion)),
+ writeFile(`${outputDir}lobe.1`, generateAliasManPage('lh')),
+ writeFile(`${outputDir}lobehub.1`, generateAliasManPage('lh')),
+]);
diff --git a/apps/cli/src/man/roff.test.ts b/apps/cli/src/man/roff.test.ts
new file mode 100644
index 00000000000..620f3ecaa42
--- /dev/null
+++ b/apps/cli/src/man/roff.test.ts
@@ -0,0 +1,28 @@
+import { Command } from 'commander';
+import { describe, expect, it } from 'vitest';
+
+import { generateAliasManPage, generateRootManPage } from './roff';
+
+describe('roff manual generator', () => {
+ it('renders a root man page from the command tree', () => {
+ const program = new Command();
+
+ program.name('lh').description('Sample CLI').version('1.0.0');
+
+ program.command('generate').alias('gen').description('Generate content');
+ program.command('login').description('Log in');
+
+ const output = generateRootManPage(program, '1.2.3');
+
+ expect(output).toContain('.TH LH 1 "" "@lobehub/cli 1.2.3" "User Commands"');
+ expect(output).toContain('.SH COMMANDS');
+ expect(output).toContain('.B generate');
+ expect(output).toContain('Generate content Alias: gen.');
+ expect(output).toContain('.B login');
+ expect(output).toContain('.SH OPTIONS');
+ });
+
+ it('renders alias man pages as so links', () => {
+ expect(generateAliasManPage('lh')).toBe('.so man1/lh.1\n');
+ });
+});
diff --git a/apps/cli/src/man/roff.ts b/apps/cli/src/man/roff.ts
new file mode 100644
index 00000000000..acbe282773b
--- /dev/null
+++ b/apps/cli/src/man/roff.ts
@@ -0,0 +1,148 @@
+import type { Command } from 'commander';
+
+const ROOT_ALIASES = ['lobe', 'lobehub'];
+const HELP_COMMAND_NAME = 'help';
+
+interface RoffDefinition {
+ description: string;
+ term: string;
+}
+
+const FILE_ENTRIES = [
+ {
+ description: 'Encrypted access and refresh tokens.',
+ path: '~/.lobehub/credentials.json',
+ },
+ {
+ description: 'CLI settings such as server and gateway URLs.',
+ path: '~/.lobehub/settings.json',
+ },
+ {
+ description: 'Background daemon PID file.',
+ path: '~/.lobehub/daemon.pid',
+ },
+ {
+ description: 'Background daemon status metadata.',
+ path: '~/.lobehub/daemon.status',
+ },
+ {
+ description: 'Background daemon log output.',
+ path: '~/.lobehub/daemon.log',
+ },
+] as const;
+
+const EXAMPLES = [
+ {
+ command: 'lh login',
+ description: 'Start interactive login in the browser.',
+ },
+ {
+ command: 'lh connect --daemon',
+ description: 'Start the device gateway connection in the background.',
+ },
+ {
+ command: 'lh search -q "gpt-5"',
+ description: 'Search local resources for a query.',
+ },
+ {
+ command: 'lh generate text "Write release notes"',
+ description: 'Generate text from a prompt.',
+ },
+ {
+ command: 'lh man generate',
+ description: 'Show the built-in manual for the generate command group.',
+ },
+] as const;
+
+export function generateRootManPage(program: Command, version: string) {
+ const help = program.createHelp();
+ const commands = getVisibleCommands(program).map((command) => ({
+ description: formatCommandDescription(help.subcommandDescription(command), command.aliases()),
+ term: command.name(),
+ }));
+ const options = help.visibleOptions(program).map((option) => ({
+ description: help.optionDescription(option),
+ term: help.optionTerm(option),
+ }));
+
+ const lines = [
+ '.\\" Code generated by `npm run man:generate`; DO NOT EDIT.',
+ '.\\" Manual command details come from the Commander command tree.',
+ `.TH LH 1 "" "${escapeRoff(`@lobehub/cli ${version}`)}" "User Commands"`,
+ '.SH NAME',
+ `lh \\- ${escapeRoff(program.description() || 'LobeHub CLI')}`,
+ '.SH SYNOPSIS',
+ ...formatSynopsisLines(),
+ '.SH DESCRIPTION',
+ escapeRoff(
+ `${program.name()} is the command-line interface for LobeHub. It provides authentication, device gateway connectivity, content generation, resource search, and management commands for agents, files, models, providers, plugins, knowledge bases, threads, topics, and related resources.`,
+ ),
+ '.PP',
+ 'For command-specific manuals, use the built-in manual command:',
+ '.PP',
+ '.RS',
+ '.B lh man',
+ '[\\fICOMMAND\\fR]...',
+ '.RE',
+ '.SH COMMANDS',
+ ...formatDefinitionSection(commands, 'B'),
+ '.SH OPTIONS',
+ ...formatDefinitionSection(options, 'B'),
+ '.SH FILES',
+ ...FILE_ENTRIES.flatMap((entry) => [
+ '.TP',
+ `.I ${escapeRoff(entry.path)}`,
+ escapeRoff(entry.description),
+ ]),
+ '.PP',
+ 'The base directory can be overridden with the',
+ '.B LOBEHUB_CLI_HOME',
+ 'environment variable.',
+ '.SH EXAMPLES',
+ ...EXAMPLES.flatMap((example) => [
+ '.TP',
+ `.B ${escapeRoff(example.command)}`,
+ escapeRoff(example.description),
+ ]),
+ '.SH SEE ALSO',
+ '.BR lobe (1),',
+ '.BR lobehub (1)',
+ ];
+
+ return `${lines.join('\n')}\n`;
+}
+
+export function generateAliasManPage(target: string) {
+ return `.so man1/${target}.1\n`;
+}
+
+function formatSynopsisLines() {
+ return ['lh', ...ROOT_ALIASES]
+ .flatMap((binary) => [`.B ${binary}`, '[\\fIOPTION\\fR]...', '[\\fICOMMAND\\fR]', '.br'])
+ .slice(0, -1);
+}
+
+function getVisibleCommands(command: Command) {
+ return command
+ .createHelp()
+ .visibleCommands(command)
+ .filter((subcommand) => subcommand.name() !== HELP_COMMAND_NAME);
+}
+
+function formatCommandDescription(description: string, aliases: string[]) {
+ if (aliases.length === 0) return description;
+
+ return `${description} Alias: ${aliases.join(', ')}.`;
+}
+
+function formatDefinitionSection(items: RoffDefinition[], macro: 'B' | 'I') {
+ return items.flatMap((item) => [
+ '.TP',
+ `.${macro} ${escapeRoff(item.term)}`,
+ escapeRoff(item.description),
+ ]);
+}
+
+function escapeRoff(value: string) {
+ return value.replaceAll('\\', '\\\\').replaceAll('-', '\\-');
+}
diff --git a/apps/cli/src/program.ts b/apps/cli/src/program.ts
new file mode 100644
index 00000000000..06ce62cf62d
--- /dev/null
+++ b/apps/cli/src/program.ts
@@ -0,0 +1,77 @@
+import { createRequire } from 'node:module';
+
+import { Command } from 'commander';
+
+import { registerAgentCommand } from './commands/agent';
+import { registerAgentGroupCommand } from './commands/agent-group';
+import { registerBotCommand } from './commands/bot';
+import { registerCompletionCommand } from './commands/completion';
+import { registerConfigCommand } from './commands/config';
+import { registerConnectCommand } from './commands/connect';
+import { registerCronCommand } from './commands/cron';
+import { registerDeviceCommand } from './commands/device';
+import { registerDocCommand } from './commands/doc';
+import { registerEvalCommand } from './commands/eval';
+import { registerFileCommand } from './commands/file';
+import { registerGenerateCommand } from './commands/generate';
+import { registerKbCommand } from './commands/kb';
+import { registerLoginCommand } from './commands/login';
+import { registerLogoutCommand } from './commands/logout';
+import { registerManCommand } from './commands/man';
+import { registerMemoryCommand } from './commands/memory';
+import { registerMessageCommand } from './commands/message';
+import { registerModelCommand } from './commands/model';
+import { registerPluginCommand } from './commands/plugin';
+import { registerProviderCommand } from './commands/provider';
+import { registerSearchCommand } from './commands/search';
+import { registerSessionGroupCommand } from './commands/session-group';
+import { registerSkillCommand } from './commands/skill';
+import { registerStatusCommand } from './commands/status';
+import { registerThreadCommand } from './commands/thread';
+import { registerTopicCommand } from './commands/topic';
+import { registerUserCommand } from './commands/user';
+
+const require = createRequire(import.meta.url);
+const { version } = require('../package.json');
+
+export function createProgram() {
+ const program = new Command();
+
+ program
+ .name('lh')
+ .description('LobeHub CLI - manage and connect to LobeHub services')
+ .version(version);
+
+ registerLoginCommand(program);
+ registerLogoutCommand(program);
+ registerCompletionCommand(program);
+ registerManCommand(program);
+ registerConnectCommand(program);
+ registerDeviceCommand(program);
+ registerStatusCommand(program);
+ registerDocCommand(program);
+ registerSearchCommand(program);
+ registerKbCommand(program);
+ registerMemoryCommand(program);
+ registerAgentCommand(program);
+ registerAgentGroupCommand(program);
+ registerBotCommand(program);
+ registerCronCommand(program);
+ registerGenerateCommand(program);
+ registerFileCommand(program);
+ registerSkillCommand(program);
+ registerSessionGroupCommand(program);
+ registerThreadCommand(program);
+ registerTopicCommand(program);
+ registerMessageCommand(program);
+ registerModelCommand(program);
+ registerProviderCommand(program);
+ registerPluginCommand(program);
+ registerUserCommand(program);
+ registerConfigCommand(program);
+ registerEvalCommand(program);
+
+ return program;
+}
+
+export { version as cliVersion };