From e6044793ef7047f334e993726a67ffab0f5ce962 Mon Sep 17 00:00:00 2001 From: openhands Date: Sat, 23 May 2026 15:08:05 +0000 Subject: [PATCH] fix: add Commands: section header and reorganize tests - Added missing 'Commands:' title before subcommands section - Split tests into separate files: - tests/parsing.test.ts - runtime parsing tests - tests/help.test.ts - help system tests - tests/opts-types.test-d.ts - type definition tests --- src/help.ts | 3 +- tests/help.test.ts | 89 ++++++++++++++++++++++ tests/{command.test.ts => parsing.test.ts} | 0 3 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 tests/help.test.ts rename tests/{command.test.ts => parsing.test.ts} (100%) diff --git a/src/help.ts b/src/help.ts index 2d69379..c299f9b 100644 --- a/src/help.ts +++ b/src/help.ts @@ -277,10 +277,11 @@ export function formatHelp(config: { const groupedCommands = groupItems(sorted, groupOrder); const sortedCommandGroups = sortGroups(groupedCommands, groupOrder); + lines.push(styles.title('Commands:')); + for (const { group, items } of sortedCommandGroups) { if (group) { lines.push(styles.groupTitle(group)); - lines.push(''); } // Calculate width for command names + usage diff --git a/tests/help.test.ts b/tests/help.test.ts new file mode 100644 index 0000000..855d160 --- /dev/null +++ b/tests/help.test.ts @@ -0,0 +1,89 @@ +import { describe, expect, it } from 'vitest'; +import { Command } from '../src'; + +describe('Command - help system', () => { + it('shows brief help on -h (no Arguments, no option hints)', async () => { + const subCmd = new Command('sub') + .description('A subcommand') + .argument('', 'environment', { choices: ['dev', 'prod'] }) + .option('-v, --verbose', 'verbose output'); + + const cmd = new Command('test') + .description('A test command') + .argument('', 'environment', { choices: ['dev', 'prod'] }) + .option('-p, --port ', 'port number', { default: 3000 }) + .addCommand(subCmd); + + const briefHelp = cmd.helpText({ detailed: false }); + // Brief mode should NOT include Arguments section + expect(briefHelp).not.toContain('Arguments:'); + // Brief mode should NOT include option hints (default, choices, etc) + expect(briefHelp).not.toContain('default:'); + expect(briefHelp).not.toContain('choices:'); + // Brief mode should still include Options + expect(briefHelp).toContain('Options:'); + // Brief mode should include command name (without usage in brief mode) + expect(briefHelp).toContain('sub'); + // Brief mode: sub command should NOT have [options] or args + expect(briefHelp).toContain('sub A subcommand'); + // Brief hint should use -h + expect(briefHelp).toContain('[command] -h'); + }); + + it('shows detailed help on --help (with Arguments and option hints)', async () => { + const subCmd = new Command('sub') + .description('A subcommand') + .argument('', 'environment', { choices: ['dev', 'prod'] }) + .option('-v, --verbose', 'verbose output'); + + const cmd = new Command('test') + .description('A test command') + .argument('', 'environment', { choices: ['dev', 'prod'] }) + .option('-p, --port ', 'port number', { default: 3000 }) + .addCommand(subCmd); + + const detailedHelp = cmd.helpText({ detailed: true }); + // Detailed mode should include Arguments section + expect(detailedHelp).toContain('Arguments:'); + // Detailed mode should include option hints + expect(detailedHelp).toContain('default:'); + expect(detailedHelp).toContain('choices:'); + // Detailed mode should include Options + expect(detailedHelp).toContain('Options:'); + // Detailed mode should show command with usage snippet (using subcommand's own options/args) + expect(detailedHelp).toContain('sub [options] '); + // Detailed hint should use --help + expect(detailedHelp).toContain('[command] --help'); + }); + + it('groups options by group property', async () => { + const cmd = new Command('test') + .option('-p, --port ', 'port', { group: 'Server' }) + .option('--host ', 'host', { group: 'Server' }) + .option('-v, --verbose', 'verbose', { group: 'Output' }) + .configureHelp({ groupOrder: ['Server', 'Output'] }); + + const help = cmd.helpText({ detailed: true }); + expect(help).toContain('Server'); + expect(help).toContain('Output'); + }); + + it('adds group to arguments', async () => { + const cmd = new Command('test') + .argument('', 'input file', { group: 'Input' }) + .argument('[out]', 'output file', { group: 'Output' }) + .configureHelp({ groupOrder: ['Input', 'Output'] }); + + const help = cmd.helpText({ detailed: true }); + expect(help).toContain('Input'); + expect(help).toContain('Output'); + }); + + it('shows Commands: section header', async () => { + const cmd = new Command('test') + .addCommand(new Command('sub').description('A subcommand')); + + const help = cmd.helpText({ detailed: true }); + expect(help).toContain('Commands:'); + }); +}); \ No newline at end of file diff --git a/tests/command.test.ts b/tests/parsing.test.ts similarity index 100% rename from tests/command.test.ts rename to tests/parsing.test.ts