@@ -40,7 +40,7 @@ import {
40
40
isValidName ,
41
41
resolveArgv ,
42
42
resolveCommand ,
43
- resolveParametersBeforeFlag ,
43
+ stripFlags ,
44
44
} from "./utils" ;
45
45
import { mapParametersToArguments , parseParameters } from "./parameters" ;
46
46
import { locales } from "./locales" ;
@@ -76,30 +76,30 @@ export class Clerc<C extends Commands = {}> {
76
76
} ,
77
77
} ;
78
78
79
- private constructor ( name ?: string , description ?: string , version ?: string ) {
79
+ private constructor ( name ?: string , description ?: string , version ?: string ) {
80
80
this . #name = name || this . #name;
81
81
this . #description = description || this . #description;
82
82
this . #version = version || this . #version;
83
83
this . #locale = detectLocale ( ) ;
84
84
this . #addCoreLocales( ) ;
85
85
}
86
86
87
- get #hasRootOrAlias ( ) {
87
+ get #hasRootOrAlias( ) {
88
88
return this . #usedNames. has ( Root ) ;
89
89
}
90
90
91
- get #hasRoot ( ) {
91
+ get #hasRoot( ) {
92
92
return Object . prototype . hasOwnProperty . call ( this . _commands , Root ) ;
93
93
}
94
94
95
- get _name ( ) { return this . #name; }
96
- get _description ( ) { return this . #description; }
97
- get _version ( ) { return this . #version; }
98
- get _inspectors ( ) { return this . #inspectors; }
99
- get _commands ( ) { return this . #commands; }
95
+ get _name ( ) { return this . #name; }
96
+ get _description ( ) { return this . #description; }
97
+ get _version ( ) { return this . #version; }
98
+ get _inspectors ( ) { return this . #inspectors; }
99
+ get _commands ( ) { return this . #commands; }
100
100
101
- #addCoreLocales ( ) { this . i18n . add ( locales ) ; }
102
- #otherMethodCaled ( ) { this . #isOtherMethodCalled = true ; }
101
+ #addCoreLocales( ) { this . i18n . add ( locales ) ; }
102
+ #otherMethodCaled( ) { this . #isOtherMethodCalled = true ; }
103
103
104
104
/**
105
105
* Create a new cli
@@ -109,7 +109,7 @@ export class Clerc<C extends Commands = {}> {
109
109
* const cli = Clerc.create()
110
110
* ```
111
111
*/
112
- static create ( name ?: string , description ?: string , version ?: string ) {
112
+ static create ( name ?: string , description ?: string , version ?: string ) {
113
113
return new Clerc ( name , description , version ) ;
114
114
}
115
115
@@ -123,7 +123,7 @@ export class Clerc<C extends Commands = {}> {
123
123
* .name("test")
124
124
* ```
125
125
*/
126
- name ( name : string ) {
126
+ name ( name : string ) {
127
127
this . #otherMethodCaled( ) ;
128
128
this . #name = name ;
129
129
return this ;
@@ -139,7 +139,7 @@ export class Clerc<C extends Commands = {}> {
139
139
* .description("test cli")
140
140
* ```
141
141
*/
142
- description ( description : string ) {
142
+ description ( description : string ) {
143
143
this . #otherMethodCaled( ) ;
144
144
this . #description = description ;
145
145
return this ;
@@ -155,7 +155,7 @@ export class Clerc<C extends Commands = {}> {
155
155
* .version("1.0.0")
156
156
* ```
157
157
*/
158
- version ( version : string ) {
158
+ version ( version : string ) {
159
159
this . #otherMethodCaled( ) ;
160
160
this . #version = version ;
161
161
return this ;
@@ -173,7 +173,7 @@ export class Clerc<C extends Commands = {}> {
173
173
* .command(...)
174
174
* ```
175
175
*/
176
- locale ( locale : string ) {
176
+ locale ( locale : string ) {
177
177
if ( this . #isOtherMethodCalled) { throw new LocaleNotCalledFirstError ( this . i18n . t ) ; }
178
178
this . #locale = locale ;
179
179
return this ;
@@ -191,7 +191,7 @@ export class Clerc<C extends Commands = {}> {
191
191
* .command(...)
192
192
* ```
193
193
*/
194
- fallbackLocale ( fallbackLocale : string ) {
194
+ fallbackLocale ( fallbackLocale : string ) {
195
195
if ( this . #isOtherMethodCalled) { throw new LocaleNotCalledFirstError ( this . i18n . t ) ; }
196
196
this . #defaultLocale = fallbackLocale ;
197
197
return this ;
@@ -207,7 +207,7 @@ export class Clerc<C extends Commands = {}> {
207
207
* .errorHandler((err) => { console.log(err); })
208
208
* ```
209
209
*/
210
- errorHandler ( handler : ( err : any ) => void ) {
210
+ errorHandler ( handler : ( err : any ) => void ) {
211
211
this . #errorHandlers. push ( handler ) ;
212
212
return this ;
213
213
}
@@ -246,12 +246,12 @@ export class Clerc<C extends Commands = {}> {
246
246
*/
247
247
command < N extends string | RootType , O extends CommandOptions < [ ...P ] , A , F > , P extends string [ ] = string [ ] , A extends MaybeArray < string | RootType > = MaybeArray < string | RootType > , F extends Flags = Flags > ( c : CommandWithHandler < N , O & CommandOptions < [ ...P ] , A , F > > ) : this & Clerc < C & Record < N , Command < N , O > > > ;
248
248
command < N extends string | RootType , O extends CommandOptions < [ ...P ] , A , F > , P extends string [ ] = string [ ] , A extends MaybeArray < string | RootType > = MaybeArray < string | RootType > , F extends Flags = Flags > ( name : N , description : string , options ?: O & CommandOptions < [ ...P ] , A , F > ) : this & Clerc < C & Record < N , Command < N , O > > > ;
249
- command ( nameOrCommand : any , description ?: any , options : any = { } ) {
249
+ command ( nameOrCommand : any , description ?: any , options : any = { } ) {
250
250
this . #callWithErrorHandling( ( ) => this . #command( nameOrCommand , description , options ) ) ;
251
251
return this ;
252
252
}
253
253
254
- #command ( nameOrCommand : any , description ?: any , options : any = { } ) {
254
+ #command( nameOrCommand : any , description ?: any , options : any = { } ) {
255
255
this . #otherMethodCaled( ) ;
256
256
const { t } = this . i18n ;
257
257
const checkIsCommandObject = ( nameOrCommand : any ) : nameOrCommand is CommandWithHandler => ! ( typeof nameOrCommand === "string" || nameOrCommand === Root ) ;
@@ -329,7 +329,7 @@ export class Clerc<C extends Commands = {}> {
329
329
* })
330
330
* ```
331
331
*/
332
- inspector ( inspector : Inspector ) {
332
+ inspector ( inspector : Inspector ) {
333
333
this . #otherMethodCaled( ) ;
334
334
this . #inspectors. push ( inspector ) ;
335
335
return this ;
@@ -345,7 +345,7 @@ export class Clerc<C extends Commands = {}> {
345
345
* .parse(process.argv.slice(2)) // Optional
346
346
* ```
347
347
*/
348
- parse ( optionsOrArgv : string [ ] | ParseOptions = resolveArgv ( ) ) {
348
+ parse ( optionsOrArgv : string [ ] | ParseOptions = resolveArgv ( ) ) {
349
349
this . #otherMethodCaled( ) ;
350
350
const { argv, run } : ParseOptions = Array . isArray ( optionsOrArgv )
351
351
? {
@@ -364,7 +364,7 @@ export class Clerc<C extends Commands = {}> {
364
364
return this ;
365
365
}
366
366
367
- #validateMeta ( ) {
367
+ #validateMeta( ) {
368
368
const { t } = this . i18n ;
369
369
if ( ! this . #name) {
370
370
throw new NameNotSetError ( t ) ;
@@ -377,7 +377,7 @@ export class Clerc<C extends Commands = {}> {
377
377
}
378
378
}
379
379
380
- #getContext ( getCommand : ( ) => ReturnType < typeof resolveCommand > ) {
380
+ #getContext( getCommand : ( ) => ReturnType < typeof resolveCommand > ) {
381
381
const argv = this . #argv! ;
382
382
const { t } = this . i18n ;
383
383
const [ command , called ] = getCommand ( ) ;
@@ -433,7 +433,7 @@ export class Clerc<C extends Commands = {}> {
433
433
return context ;
434
434
}
435
435
436
- #callWithErrorHandling ( fn : ( ) => void ) {
436
+ #callWithErrorHandling( fn : ( ) => void ) {
437
437
try {
438
438
fn ( ) ;
439
439
} catch ( e ) {
@@ -445,22 +445,21 @@ export class Clerc<C extends Commands = {}> {
445
445
}
446
446
}
447
447
448
- #runMatchedCommand ( ) {
448
+ #runMatchedCommand( ) {
449
449
this . #otherMethodCaled( ) ;
450
450
const { t } = this . i18n ;
451
451
const argv = this . #argv;
452
452
if ( ! argv ) {
453
453
throw new Error ( t ( "core.cliParseMustBeCalled" ) ) ;
454
454
}
455
- const name = resolveParametersBeforeFlag ( argv ) ;
456
- const stringName = name . join ( " " ) ;
457
- const getCommand = ( ) => resolveCommand ( this . #commands, name , t ) ;
455
+ const getCommand = ( ) => resolveCommand ( this . #commands, argv ! , t ) ;
458
456
const getContext = ( ) => this . #getContext( getCommand ) ;
459
457
const emitHandler : Inspector = {
460
458
enforce : "post" ,
461
459
fn : ( ) => {
462
460
const [ command ] = getCommand ( ) ;
463
461
const handlerContext = getContext ( ) ;
462
+ const stringName = stripFlags ( argv ) . join ( " " ) ;
464
463
if ( ! command ) {
465
464
if ( stringName ) {
466
465
throw new NoSuchCommandError ( stringName , t ) ;
@@ -473,7 +472,7 @@ export class Clerc<C extends Commands = {}> {
473
472
} ;
474
473
const inspectors = [ ...this . #inspectors, emitHandler ] ;
475
474
const callInspector = compose ( inspectors ) ;
476
- callInspector ( getContext ) ;
475
+ callInspector ( getContext ( ) ) ;
477
476
}
478
477
479
478
/**
@@ -486,7 +485,7 @@ export class Clerc<C extends Commands = {}> {
486
485
* .runMatchedCommand()
487
486
* ```
488
487
*/
489
- runMatchedCommand ( ) {
488
+ runMatchedCommand ( ) {
490
489
this . #callWithErrorHandling( ( ) => this . #runMatchedCommand( ) ) ;
491
490
process . title = this . #name;
492
491
return this ;
0 commit comments