@@ -20,7 +20,8 @@ export type OptionHandler = (
2020 options : OptionsMap
2121) => void ;
2222
23- export const noopHandler : OptionHandler = function ( ) { } ;
23+ // Default no-op handler for options (internal)
24+ const noopHandler : OptionHandler = function ( ) { } ;
2425
2526// Completion result types
2627export type Completion = {
@@ -65,7 +66,7 @@ export class Option {
6566 command : Command ,
6667 value : string ,
6768 description : string ,
68- handler : OptionHandler = noopHandler ,
69+ handler ? : OptionHandler ,
6970 alias ?: string ,
7071 isBoolean ?: boolean
7172 ) {
@@ -90,20 +91,64 @@ export class Command {
9091 this . description = description ;
9192 }
9293
94+ // Function overloads for better UX
95+ option ( value : string , description : string ) : Command ;
96+ option ( value : string , description : string , alias : string ) : Command ;
9397 option (
9498 value : string ,
9599 description : string ,
96- handler : OptionHandler = noopHandler ,
97- alias ?: string ,
100+ alias : string ,
101+ isBoolean : boolean
102+ ) : Command ;
103+ option ( value : string , description : string , handler : OptionHandler ) : Command ;
104+ option (
105+ value : string ,
106+ description : string ,
107+ handler : OptionHandler ,
108+ alias : string
109+ ) : Command ;
110+ option (
111+ value : string ,
112+ description : string ,
113+ handler : OptionHandler ,
114+ alias : string ,
115+ isBoolean : boolean
116+ ) : Command ;
117+ option (
118+ value : string ,
119+ description : string ,
120+ handlerOrAlias ?: OptionHandler | string ,
121+ aliasOrIsBoolean ?: string | boolean ,
98122 isBoolean ?: boolean
99- ) {
123+ ) : Command {
124+ let handler : OptionHandler = noopHandler ;
125+ let alias : string | undefined ;
126+ let isBooleanFlag : boolean | undefined ;
127+
128+ // Parse arguments based on types
129+ if ( typeof handlerOrAlias === 'function' ) {
130+ // handler provided
131+ handler = handlerOrAlias ;
132+ alias = aliasOrIsBoolean as string ;
133+ isBooleanFlag = isBoolean ;
134+ } else if ( typeof handlerOrAlias === 'string' ) {
135+ // alias provided (no handler)
136+ alias = handlerOrAlias ;
137+ isBooleanFlag = aliasOrIsBoolean as boolean ;
138+ } else if ( handlerOrAlias === undefined ) {
139+ // neither handler nor alias provided
140+ if ( typeof aliasOrIsBoolean === 'boolean' ) {
141+ isBooleanFlag = aliasOrIsBoolean ;
142+ }
143+ }
144+
100145 const option = new Option (
101146 this ,
102147 value ,
103148 description ,
104149 handler ,
105150 alias ,
106- isBoolean
151+ isBooleanFlag
107152 ) ;
108153 this . options . set ( value , option ) ;
109154 return this ;
@@ -279,9 +324,9 @@ export class RootCommand extends Command {
279324
280325 this . completions = toComplete . includes ( '=' )
281326 ? suggestions . map ( ( s ) => ( {
282- value : `${ optionName } =${ s . value } ` ,
283- description : s . description ,
284- } ) )
327+ value : `${ optionName } =${ s . value } ` ,
328+ description : s . description ,
329+ } ) )
285330 : suggestions ;
286331 }
287332 return ;
@@ -492,9 +537,9 @@ export class RootCommand extends Command {
492537 setup ( name : string , executable : string , shell : string ) {
493538 assert (
494539 shell === 'zsh' ||
495- shell === 'bash' ||
496- shell === 'fish' ||
497- shell === 'powershell' ,
540+ shell === 'bash' ||
541+ shell === 'fish' ||
542+ shell === 'powershell' ,
498543 'Unsupported shell'
499544 ) ;
500545
0 commit comments