1- import { z } from " zod"
2- import { isZodError , ParseError , ValidationError } from " ./error"
3- import { HelpGenerator } from " ./help"
1+ import { z } from ' zod'
2+ import { isZodError , ParseError , ValidationError } from ' ./error'
3+ import { HelpGenerator } from ' ./help'
44import MassargOption , {
55 MassargFlag ,
66 OptionConfig ,
77 TypedOptionConfig ,
88 MassargHelpFlag ,
9- } from " ./option"
10- import { setOrPush } from " ./utils"
9+ } from ' ./option'
10+ import { setOrPush } from ' ./utils'
1111
1212export const CommandConfig = < RunArgs extends z . ZodType > ( args : RunArgs ) =>
1313 z . object ( {
@@ -93,9 +93,9 @@ export default class MassargCommand<Args extends ArgsObject = ArgsObject> {
9393 }
9494 }
9595
96- flag ( config : Omit < OptionConfig < boolean > , " parse" > ) : MassargCommand < Args >
96+ flag ( config : Omit < OptionConfig < boolean > , ' parse' > ) : MassargCommand < Args >
9797 flag ( config : MassargFlag ) : MassargCommand < Args >
98- flag ( config : Omit < OptionConfig < boolean > , " parse" > | MassargFlag ) : MassargCommand < Args > {
98+ flag ( config : Omit < OptionConfig < boolean > , ' parse' > | MassargFlag ) : MassargCommand < Args > {
9999 try {
100100 const flag = config instanceof MassargFlag ? config : new MassargFlag ( config )
101101 this . options . push ( flag as MassargOption )
@@ -138,39 +138,16 @@ export default class MassargCommand<Args extends ArgsObject = ArgsObject> {
138138 }
139139
140140 parse ( argv : string [ ] , args ?: Partial < Args > , parent ?: MassargCommand < Args > ) : Promise < void > | void {
141- this . args ??= { }
142- this . args = { ...this . args , ...args }
143- let _argv = [ ...argv ]
144- while ( _argv . length ) {
145- const arg = _argv . shift ( ) !
146- const found = this . options . some ( ( o ) => o . _isOption ( arg ) )
147- if ( found ) {
148- _argv = this . parseOption ( arg , _argv )
149- continue
150- }
151-
152- const command = this . commands . find ( ( c ) => c . name === arg || c . aliases . includes ( arg ) )
153- if ( command ) {
154- return command . parse ( _argv , this . args , parent ?? this )
155- }
156- const defaultOption = this . options . find ( ( o ) => o . isDefault )
157- if ( defaultOption ) {
158- _argv = this . parseOption ( `--${ defaultOption . name } ` , [ arg , ..._argv ] )
159- continue
160- }
161- }
162- if ( this . _run ) {
163- this . _run ( { ...args , ...this . args } as Args , parent ?? this )
164- }
141+ this . getArgs ( argv , args , parent , true )
165142 }
166143
167144 private parseOption ( arg : string , argv : string [ ] ) {
168145 const option = this . options . find ( ( o ) => o . _match ( arg ) )
169146 if ( ! option ) {
170147 throw new ValidationError ( {
171148 path : [ MassargOption . getName ( arg ) ] ,
172- code : " unknown_option" ,
173- message : " Unknown option" ,
149+ code : ' unknown_option' ,
150+ message : ' Unknown option' ,
174151 } )
175152 }
176153 const res = option . _parseDetails ( [ arg , ...argv ] )
@@ -182,9 +159,27 @@ export default class MassargCommand<Args extends ArgsObject = ArgsObject> {
182159 return res . argv
183160 }
184161
185- getArgs ( argv : string [ ] ) : Args {
186- let args : Args = { } as Args
162+ getArgs (
163+ argv : string [ ] ,
164+ __args ?: Partial < Args > ,
165+ parent ?: MassargCommand < any > ,
166+ parseCommands ?: false ,
167+ ) : Promise < void > | void
168+ getArgs (
169+ argv : string [ ] ,
170+ __args ?: Partial < Args > ,
171+ parent ?: MassargCommand < any > ,
172+ parseCommands ?: true ,
173+ ) : Args
174+ getArgs (
175+ argv : string [ ] ,
176+ args ?: Partial < Args > ,
177+ parent ?: MassargCommand < any > ,
178+ parseCommands = false ,
179+ ) : Args | Promise < void > | void {
180+ let _args : Args = { ...this . args , ...args } as Args
187181 let _argv = [ ...argv ]
182+ const _a = this . args as Record < string , string [ ] >
188183 while ( _argv . length ) {
189184 const arg = _argv . shift ( ) !
190185 const found = this . options . some ( ( o ) => o . _isOption ( arg ) )
@@ -193,25 +188,41 @@ export default class MassargCommand<Args extends ArgsObject = ArgsObject> {
193188 if ( ! option ) {
194189 throw new ValidationError ( {
195190 path : [ MassargOption . getName ( arg ) ] ,
196- code : " unknown_option" ,
197- message : " Unknown option" ,
191+ code : ' unknown_option' ,
192+ message : ' Unknown option' ,
198193 } )
199194 }
200195 const res = option . _parseDetails ( argv )
201- args [ res . key as keyof Args ] = setOrPush < Args [ keyof Args ] > (
196+ _args [ res . key as keyof Args ] = setOrPush < Args [ keyof Args ] > (
202197 res . value ,
203- args [ res . key as keyof Args ] ,
198+ _args [ res . key as keyof Args ] ,
204199 option . isArray ,
205200 )
206201 continue
207202 }
208203
209204 const command = this . commands . find ( ( c ) => c . name === arg || c . aliases . includes ( arg ) )
210205 if ( command ) {
211- break
206+ if ( ! parseCommands ) {
207+ break
208+ }
209+ return command . parse ( _argv , this . args , parent ?? this )
212210 }
211+ const defaultOption = this . options . find ( ( o ) => o . isDefault )
212+ if ( defaultOption ) {
213+ _argv = this . parseOption ( `--${ defaultOption . name } ` , [ arg , ..._argv ] )
214+ continue
215+ }
216+ _a . extra ??= [ ]
217+ _a . extra . push ( arg )
218+ }
219+ if ( ! parseCommands ) {
220+ return _args
221+ }
222+ this . args = { ...this . args , ..._args }
223+ if ( this . _run ) {
224+ this . _run ( this . args , parent ?? this )
213225 }
214- return args
215226 }
216227
217228 helpString ( ) : string {
@@ -224,11 +235,11 @@ export default class MassargCommand<Args extends ArgsObject = ArgsObject> {
224235}
225236
226237export class MassargHelpCommand < T extends ArgsObject = ArgsObject > extends MassargCommand < T > {
227- constructor ( config : Partial < Omit < CommandConfig < T > , " run" > > = { } ) {
238+ constructor ( config : Partial < Omit < CommandConfig < T > , ' run' > > = { } ) {
228239 super ( {
229- name : " help" ,
230- aliases : [ "h" ] ,
231- description : " Print help for this command, or a subcommand if specified" ,
240+ name : ' help' ,
241+ aliases : [ 'h' ] ,
242+ description : ' Print help for this command, or a subcommand if specified' ,
232243 // argsHint: "[command]",
233244 run : ( args , parent ) => {
234245 if ( args . command ) {
@@ -238,9 +249,9 @@ export class MassargHelpCommand<T extends ArgsObject = ArgsObject> extends Massa
238249 return
239250 } else {
240251 throw new ParseError ( {
241- path : [ " command" ] ,
242- code : " unknown_command" ,
243- message : " Unknown command" ,
252+ path : [ ' command' ] ,
253+ code : ' unknown_command' ,
254+ message : ' Unknown command' ,
244255 received : args . command ,
245256 } )
246257 }
@@ -250,9 +261,9 @@ export class MassargHelpCommand<T extends ArgsObject = ArgsObject> extends Massa
250261 ...config ,
251262 } )
252263 this . option ( {
253- name : " command" ,
254- aliases : [ "c" ] ,
255- description : " Command to print help for" ,
264+ name : ' command' ,
265+ aliases : [ 'c' ] ,
266+ description : ' Command to print help for' ,
256267 isDefault : true ,
257268 } )
258269 }
0 commit comments