import type { FlagDef } from '../plugins.js' import type { CommandRegistry, RegisteredCommand } from './command-registry.js' const DOCS_URL = 'https://chkit.obsessiondb.com/' export function formatGlobalHelp(registry: CommandRegistry, version: string): string { const lines: string[] = [] lines.push(`chkit v${version} - ClickHouse toolkit`) lines.push('') lines.push('Usage:') lines.push(' chkit [options]') lines.push('') lines.push('Commands:') lines.push(` ${'init'.padEnd(14)} Scaffold a new project with config and example schema`) lines.push(` ${'add'.padEnd(14)} Add an app's editable schema and ingestion code to the project`) lines.push(` ${'registry'.padEnd(14)} Browse apps and integration guides, inspect sync coverage, or build templates`) lines.push(` ${'skills'.padEnd(14)} Manage agent skills (proxies to the \`skills\` CLI)`) const coreCommands = registry.commands.filter((c) => c.pluginName === 'core') const pluginCommands = registry.commands.filter((c) => c.pluginName !== 'core') for (const cmd of coreCommands) { lines.push(` ${cmd.name.padEnd(14)} ${cmd.description}`) } if (pluginCommands.length > 0) { lines.push('') lines.push('Plugin commands:') for (const cmd of pluginCommands) { if (cmd.subcommands) { lines.push(` ${cmd.name.padEnd(14)} ${cmd.description}`) for (const sub of cmd.subcommands) { lines.push(` ${sub.name.padEnd(12)} ${sub.description}`) } } else { lines.push(` ${cmd.name.padEnd(14)} ${cmd.description}`) } } } lines.push('') lines.push('Global options:') for (const flag of registry.globalFlags) { lines.push(formatFlagLine(flag)) } lines.push(formatFlagLine({ name: '-h, --help', type: 'boolean', description: 'Show help' })) lines.push(formatFlagLine({ name: '-v, -V, --version', type: 'boolean', description: 'Show version' })) appendDocsHelp(lines) return lines.join('\n') } export function formatCommandHelp(command: RegisteredCommand, globalFlags: readonly FlagDef[], subcommandName?: string): string { const lines: string[] = [] const subcommand = command.subcommands?.find((sub) => sub.name === subcommandName) if (subcommand) { lines.push(`chkit ${command.name} ${subcommand.name} [options]`) lines.push(` ${subcommand.description}`) } else if (command.subcommands) { lines.push(`chkit ${command.name} [options]`) lines.push(` ${command.description}`) lines.push('') lines.push('Commands:') for (const sub of command.subcommands) { lines.push(` ${sub.name.padEnd(14)} ${sub.description}`) } } else { lines.push(`chkit ${command.name} [options]`) lines.push(` ${command.description}`) } const flags = subcommand?.flags ?? command.flags if (flags.length > 0) { lines.push('') lines.push('Options:') for (const flag of flags) { lines.push(formatFlagLine(flag)) } } for (const pf of command.pluginFlags) { lines.push('') lines.push(`Plugin: ${pf.pluginName}`) for (const flag of pf.flags) { lines.push(formatFlagLine(flag)) } } lines.push('') lines.push('Global options:') for (const flag of globalFlags) { lines.push(formatFlagLine(flag)) } lines.push(formatFlagLine({ name: '-h, --help', type: 'boolean', description: 'Show help; use --help-plugins to include configured extensions' })) appendDocsHelp(lines) return lines.join('\n') } function appendDocsHelp(lines: string[]): void { lines.push('') lines.push('Documentation:') lines.push(` Full documentation: ${DOCS_URL}`) lines.push(' Agent-readable markdown: fetch any docs page with Accept: text/markdown') } function formatFlagLine(flag: FlagDef & { name?: string }): string { const nameStr = flag.short ? `${flag.short}, ${flag.name}` : (flag.name ?? '') const placeholder = flag.placeholder ? ` ${flag.placeholder}` : '' const label = `${nameStr}${placeholder}` if (label.length >= 22) { return ` ${label}\n${''.padEnd(24)}${flag.description}` } return ` ${label.padEnd(22)} ${flag.description}` }