@@ -75,6 +75,7 @@ type Handler = (
7575type Option = {
7676 description : string ;
7777 handler : Handler ;
78+ alias ?: string ;
7879} ;
7980
8081type Command = {
@@ -117,13 +118,14 @@ export class Completion {
117118 command : string ,
118119 option : string ,
119120 description : string ,
120- handler : Handler
121+ handler : Handler ,
122+ alias ?: string
121123 ) {
122124 const cmd = this . commands . get ( command ) ;
123125 if ( ! cmd ) {
124126 throw new Error ( `Command ${ command } not found.` ) ;
125127 }
126- cmd . options . set ( option , { description, handler } ) ;
128+ cmd . options . set ( option , { description, handler, alias } ) ;
127129 return option ;
128130 }
129131
@@ -233,7 +235,12 @@ export class Completion {
233235 toComplete : string ,
234236 endsWithSpace : boolean
235237 ) : boolean {
236- return lastPrevArg ?. startsWith ( '--' ) || toComplete . startsWith ( '--' ) ;
238+ return (
239+ lastPrevArg ?. startsWith ( '--' ) ||
240+ lastPrevArg ?. startsWith ( '-' ) ||
241+ toComplete . startsWith ( '--' ) ||
242+ toComplete . startsWith ( '-' )
243+ ) ;
237244 }
238245
239246 private shouldCompleteCommands (
@@ -255,17 +262,29 @@ export class Completion {
255262 let valueToComplete = toComplete ;
256263
257264 if ( toComplete . includes ( '=' ) ) {
258- // Handle --flag=value case
265+ // Handle --flag=value or -f=value case
259266 const parts = toComplete . split ( '=' ) ;
260267 flagName = parts [ 0 ] ;
261268 valueToComplete = parts [ 1 ] || '' ;
262- } else if ( lastPrevArg ?. startsWith ( '-- ' ) ) {
263- // Handle --flag value case
269+ } else if ( lastPrevArg ?. startsWith ( '-' ) ) {
270+ // Handle --flag value or -f value case
264271 flagName = lastPrevArg ;
265272 }
266273
267274 if ( flagName ) {
268- const option = command . options . get ( flagName ) ;
275+ // Try to find the option by long name or alias
276+ let option = command . options . get ( flagName ) ;
277+ if ( ! option ) {
278+ // If not found by direct match, try to find by alias
279+ for ( const [ name , opt ] of command . options ) {
280+ if ( opt . alias && `-${ opt . alias } ` === flagName ) {
281+ option = opt ;
282+ flagName = name ; // Use the long name for completion
283+ break ;
284+ }
285+ }
286+ }
287+
269288 if ( option ) {
270289 const suggestions = await option . handler (
271290 previousArgs ,
@@ -286,9 +305,21 @@ export class Completion {
286305 }
287306
288307 // Handle flag name completion
289- if ( toComplete . startsWith ( '--' ) ) {
308+ if ( toComplete . startsWith ( '-' ) ) {
309+ const isShortFlag = toComplete . startsWith ( '-' ) && ! toComplete . startsWith ( '--' ) ;
310+
290311 for ( const [ name , option ] of command . options ) {
291- if ( name . startsWith ( toComplete ) ) {
312+ // For short flags (-), only show aliases
313+ if ( isShortFlag ) {
314+ if ( option . alias && `-${ option . alias } ` . startsWith ( toComplete ) ) {
315+ this . completions . push ( {
316+ value : `-${ option . alias } ` ,
317+ description : option . description ,
318+ } ) ;
319+ }
320+ }
321+ // For long flags (--), show the full names
322+ else if ( name . startsWith ( toComplete ) ) {
292323 this . completions . push ( {
293324 value : name ,
294325 description : option . description ,
0 commit comments