diff --git a/src/command.ts b/src/command.ts index 62e95a2..39ad858 100644 --- a/src/command.ts +++ b/src/command.ts @@ -702,7 +702,9 @@ export class Command< description: cmd._summary || cmd._description, aliases: cmd._aliases, hidden: cmd._hidden, - group: cmd._helpConfig.groupOrder ? undefined : undefined, // Groups for subcommands would be configured separately + group: cmd._helpConfig.groupOrder ? undefined : undefined, + options: cmd._options, + args: cmd._args, }); } diff --git a/src/help.ts b/src/help.ts index 1e03abb..2d69379 100644 --- a/src/help.ts +++ b/src/help.ts @@ -93,6 +93,10 @@ export interface SubcommandInfo { aliases: string[]; hidden: boolean; group?: string; + /** Subcommand's own options for generating usage snippet */ + options: OptionDef[]; + /** Subcommand's own arguments for generating usage snippet */ + args: ArgDef[]; } /** Group items by their group property, returning ordered sections */ @@ -300,11 +304,13 @@ export function formatHelp(config: { const aliasPart = cmd.aliases.length > 0 ? `|${cmd.aliases.join('|')}` : ''; const cmdName = cmd.name + aliasPart; - // Auto-generate usage snippet in detailed mode + // Auto-generate usage snippet in detailed mode using subcommand's own options/args let usagePart = ''; if (detailed) { - const optionPart = options.length > 0 ? ' [options]' : ''; - const argPart = args.length > 0 ? ' ' + args.map(a => a.name).join(' ') : ''; + const hasOptions = cmd.options && cmd.options.length > 0; + const hasArgs = cmd.args && cmd.args.length > 0; + const optionPart = hasOptions ? ' [options]' : ''; + const argPart = hasArgs ? ' ' + cmd.args!.map(a => a.name).join(' ') : ''; usagePart = optionPart + argPart; } diff --git a/tests/command.test.ts b/tests/command.test.ts index 51f6652..5419a95 100644 --- a/tests/command.test.ts +++ b/tests/command.test.ts @@ -105,11 +105,16 @@ describe('Command - runtime parsing', () => { 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(new Command('sub').description('A subcommand')); + .addCommand(subCmd); const briefHelp = cmd.helpText({ detailed: false }); // Brief mode should NOT include Arguments section @@ -119,18 +124,25 @@ describe('Command - help system', () => { expect(briefHelp).not.toContain('choices:'); // Brief mode should still include Options expect(briefHelp).toContain('Options:'); - // Brief mode should include command name + // 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(new Command('sub').description('A subcommand')); + .addCommand(subCmd); const detailedHelp = cmd.helpText({ detailed: true }); // Detailed mode should include Arguments section @@ -140,7 +152,7 @@ describe('Command - help system', () => { expect(detailedHelp).toContain('choices:'); // Detailed mode should include Options expect(detailedHelp).toContain('Options:'); - // Detailed mode should show command with usage snippet + // 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');