@@ -26,11 +26,9 @@ export default async function tab(
2626 instance : CAC ,
2727 completionConfig ?: CompletionConfig
2828) : Promise < RootCommand > {
29- // Add all commands and their options
3029 for ( const cmd of [ instance . globalCommand , ...instance . commands ] ) {
31- if ( cmd . name === 'complete' ) continue ; // Skip completion command
30+ if ( cmd . name === 'complete' ) continue ;
3231
33- // Get positional args info from command usage
3432 const args = ( cmd . rawName . match ( / \[ .* ?\] | < .* ?> / g) || [ ] ) . map ( ( arg ) =>
3533 arg . startsWith ( '[' )
3634 ) ; // true if optional (wrapped in [])
@@ -40,22 +38,21 @@ export default async function tab(
4038 ? completionConfig
4139 : completionConfig ?. subCommands ?. [ cmd . name ] ;
4240
43- // Add command to completion using t.ts API
41+ // command
4442 const commandName = isRootCommand ? '' : cmd . name ;
4543 const command = isRootCommand
4644 ? t
4745 : t . command ( commandName , cmd . description || '' ) ;
4846
49- // Set args for the command
47+ // args (if has positional arguments)
5048 if ( command ) {
51- // Extract argument names from command usage
5249 const argMatches =
5350 cmd . rawName . match ( / < ( [ ^ > ] + ) > | \[ \. \. \. ( [ ^ \] ] + ) \] / g) || [ ] ;
5451 const argNames = argMatches . map ( ( match ) => {
5552 if ( match . startsWith ( '<' ) && match . endsWith ( '>' ) ) {
56- return match . slice ( 1 , - 1 ) ; // Remove < >
53+ return match . slice ( 1 , - 1 ) ;
5754 } else if ( match . startsWith ( '[...' ) && match . endsWith ( ']' ) ) {
58- return match . slice ( 4 , - 1 ) ; // Remove [... ]
55+ return match . slice ( 4 , - 1 ) ;
5956 }
6057 return match ;
6158 } ) ;
@@ -71,18 +68,17 @@ export default async function tab(
7168 } ) ;
7269 }
7370
74- // Add command options
71+ // options
7572 for ( const option of [ ...instance . globalCommand . options , ...cmd . options ] ) {
76- // Extract short flag from the rawName if it exists (e.g., "-c, --config" -> "c" )
73+ // short flag ( if exists)
7774 const shortFlag = option . rawName . match ( / ^ - ( [ a - z A - Z ] ) , - - / ) ?. [ 1 ] ;
78- const argName = option . name ; // option.name is already clean (e.g., "config")
75+ const argName = option . name ;
7976
80- // Add option using t.ts API
8177 const targetCommand = isRootCommand ? t : command ;
8278 if ( targetCommand ) {
8379 const handler = commandCompletionConfig ?. options ?. [ argName ] ;
8480
85- // Check if option takes a value (has <> or [] in rawName, or is marked as required)
81+ // takes value (if has <> or [] in rawName, or is marked as required)
8682 const takesValue =
8783 option . required || VALUE_OPTION_RE . test ( option . rawName ) ;
8884
@@ -98,12 +94,12 @@ export default async function tab(
9894 targetCommand . option ( argName , option . description || '' , handler ) ;
9995 }
10096 } else if ( takesValue ) {
101- // Takes value but no custom handler = value option with no completions
97+ // value option (if takes value but no custom handler)
10298 if ( shortFlag ) {
10399 targetCommand . option (
104100 argName ,
105101 option . description || '' ,
106- async ( ) => [ ] , // Empty completions
102+ async ( ) => [ ] ,
107103 shortFlag
108104 ) ;
109105 } else {
@@ -114,7 +110,7 @@ export default async function tab(
114110 ) ;
115111 }
116112 } else {
117- // No custom handler and doesn't take value = boolean flag
113+ // boolean flag (if no custom handler and doesn't take value)
118114 if ( shortFlag ) {
119115 targetCommand . option ( argName , option . description || '' , shortFlag ) ;
120116 } else {
@@ -153,13 +149,12 @@ export default async function tab(
153149 const args : string [ ] = extra [ '--' ] || [ ] ;
154150 instance . showHelpOnExit = false ;
155151
156- // Parse current command context
152+ // command context
157153 instance . unsetMatchedCommand ( ) ;
158154 instance . parse ( [ execPath , processArgs [ 0 ] , ...args ] , {
159155 run : false ,
160156 } ) ;
161157
162- // Use t.ts parse method instead of completion.parse
163158 return t . parse ( args ) ;
164159 }
165160 }
0 commit comments