@@ -3,7 +3,8 @@ import * as bash from './bash';
33import * as fish from './fish' ;
44import * as powershell from './powershell' ;
55import type { CAC } from 'cac' ;
6- import { Completion } from './' ;
6+ import { Completion } from './index' ;
7+ import { CompletionConfig , noopHandler } from './shared' ;
78
89const execPath = process . execPath ;
910const processArgs = process . argv . slice ( 1 ) ;
@@ -17,7 +18,10 @@ function quoteIfNeeded(path: string): string {
1718 return path . includes ( ' ' ) ? `'${ path } '` : path ;
1819}
1920
20- export default function tab ( instance : CAC ) : Completion {
21+ export default async function tab (
22+ instance : CAC ,
23+ completionConfig ?: CompletionConfig
24+ ) {
2125 const completion = new Completion ( ) ;
2226
2327 // Add all commands and their options
@@ -29,24 +33,30 @@ export default function tab(instance: CAC): Completion {
2933 arg . startsWith ( '[' )
3034 ) ; // true if optional (wrapped in [])
3135
36+ const isRootCommand = cmd . name === '@@global@@' ;
37+ const commandCompletionConfig = isRootCommand
38+ ? completionConfig
39+ : completionConfig ?. subCommands ?. [ cmd . name ] ;
40+
3241 // Add command to completion
3342 const commandName = completion . addCommand (
34- cmd . name === '@@global@@' ? '' : cmd . name ,
43+ isRootCommand ? '' : cmd . name ,
3544 cmd . description || '' ,
3645 args ,
37- async ( ) => [ ]
46+ commandCompletionConfig ?. handler ?? noopHandler
3847 ) ;
3948
4049 // Add command options
4150 for ( const option of [ ...instance . globalCommand . options , ...cmd . options ] ) {
4251 // Extract short flag from the name if it exists (e.g., "-c, --config" -> "c")
4352 const shortFlag = option . name . match ( / ^ - ( [ a - z A - Z ] ) , - - / ) ?. [ 1 ] ;
53+ const argName = option . name . replace ( / ^ - [ a - z A - Z ] , - - / , '' ) ;
4454
4555 completion . addOption (
4656 commandName ,
47- `--${ option . name . replace ( / ^ - [ a - z A - Z ] , - - / , '' ) } ` , // Remove the short flag part if it exists
57+ `--${ argName } ` , // Remove the short flag part if it exists
4858 option . description || '' ,
49- async ( ) => [ ] ,
59+ commandCompletionConfig ?. options ?. [ argName ] ?. handler ?? noopHandler ,
5060 shortFlag
5161 ) ;
5262 }
0 commit comments