1- import type { Clerc , ClercFlagsDefinition , Command } from "@clerc/core" ;
1+ import type {
2+ Clerc ,
3+ ClercFlagsDefinition ,
4+ Command ,
5+ CommandsMap ,
6+ } from "@clerc/core" ;
27import {
38 formatFlagName ,
49 formatVersion ,
@@ -165,9 +170,46 @@ export class HelpRenderer {
165170 } ;
166171 }
167172
173+ private getSubcommands ( parentCommandName : string ) : CommandsMap {
174+ const subcommands = new Map < string , Command > ( ) ;
175+
176+ if ( ! parentCommandName ) {
177+ return subcommands ;
178+ }
179+
180+ const prefix = `${ parentCommandName } ` ;
181+
182+ for ( const [ name , command ] of this . _cli . _commands ) {
183+ if ( name . startsWith ( prefix ) && name !== parentCommandName ) {
184+ const subcommandName = name . slice ( prefix . length ) ;
185+ subcommands . set ( subcommandName , command ) ;
186+ }
187+ }
188+
189+ return subcommands ;
190+ }
191+
168192 private renderCommands ( ) {
169193 const commands = this . _cli . _commands ;
170- if ( this . _command || commands . size === 0 ) {
194+
195+ // If a command is selected, show its subcommands
196+ let commandsToShow : CommandsMap ;
197+ let title = "Commands" ;
198+ let prefix = "" ;
199+
200+ if ( this . _command ) {
201+ prefix = this . _command . name ? `${ this . _command . name } ` : "" ;
202+ title = "Subcommands" ;
203+ commandsToShow = this . getSubcommands ( this . _command . name ) ;
204+
205+ if ( commandsToShow . size === 0 ) {
206+ return ;
207+ }
208+ } else {
209+ commandsToShow = commands ;
210+ }
211+
212+ if ( commandsToShow . size === 0 ) {
171213 return ;
172214 }
173215
@@ -176,15 +218,17 @@ export class HelpRenderer {
176218 const defaultCommands : string [ ] [ ] = [ ] ;
177219 let rootCommand : string [ ] = [ ] ;
178220
179- for ( const command of commands . values ( ) ) {
221+ for ( const command of commandsToShow . values ( ) ) {
180222 if ( ( command as any ) . __isAlias || command . help ?. show === false ) {
181223 continue ;
182224 }
183225
184226 const group = command . help ?. group ;
185227 validateGroup ( group , this . _commandGroups , "command" , command . name ) ;
186228
187- const commandName = yc . cyan ( formatCommandName ( command . name ) ) ;
229+ const commandName = yc . cyan (
230+ formatCommandName ( command . name . slice ( prefix . length ) ) ,
231+ ) ;
188232 const aliases = command . alias
189233 ? ` (${ toArray ( command . alias ) . join ( ", " ) } )`
190234 : "" ;
@@ -233,7 +277,7 @@ export class HelpRenderer {
233277 }
234278
235279 return {
236- title : "Commands" ,
280+ title,
237281 body,
238282 } ;
239283 }
0 commit comments