Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
}

Expand Down
12 changes: 9 additions & 3 deletions src/help.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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;
}

Expand Down
20 changes: 16 additions & 4 deletions tests/command.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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('<env>', 'environment', { choices: ['dev', 'prod'] })
.option('-v, --verbose', 'verbose output');

const cmd = new Command('test')
.description('A test command')
.argument('<env>', 'environment', { choices: ['dev', 'prod'] })
.option('-p, --port <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
Expand All @@ -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('<env>', 'environment', { choices: ['dev', 'prod'] })
.option('-v, --verbose', 'verbose output');

const cmd = new Command('test')
.description('A test command')
.argument('<env>', 'environment', { choices: ['dev', 'prod'] })
.option('-p, --port <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
Expand All @@ -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] <env>');
// Detailed hint should use --help
expect(detailedHelp).toContain('[command] --help');
Expand Down
Loading