@@ -199,3 +199,221 @@ describe('parseAsync parameter is treated as readonly, per TypeScript declaratio
199
199
expect ( program . rawArgs ) . toEqual ( oldRawArgs ) ;
200
200
} ) ;
201
201
} ) ;
202
+
203
+ describe ( '.parse() called multiple times' , ( ) => {
204
+ test ( 'when use boolean options then option values reset' , ( ) => {
205
+ const program = new commander . Command ( ) . option ( '--black' ) . option ( '--white' ) ;
206
+
207
+ program . parse ( [ '--black' ] , { from : 'user' } ) ;
208
+ expect ( program . opts ( ) ) . toEqual ( { black : true } ) ;
209
+
210
+ program . parse ( [ '--white' ] , { from : 'user' } ) ;
211
+ expect ( program . opts ( ) ) . toEqual ( { white : true } ) ;
212
+ } ) ;
213
+
214
+ test ( 'when use options with option-argument then option values and sources reset' , ( ) => {
215
+ const program = new commander . Command ( )
216
+ . option ( '-f, --foo <value>' )
217
+ . option ( '-b, --bar <value>' ) ;
218
+
219
+ program . parse ( [ '--foo' , 'FOO' ] , { from : 'user' } ) ;
220
+ expect ( program . opts ( ) ) . toEqual ( { foo : 'FOO' } ) ;
221
+ expect ( program . getOptionValueSource ( 'foo' ) ) . toEqual ( 'cli' ) ;
222
+ expect ( program . getOptionValueSource ( 'bar' ) ) . toBeUndefined ( ) ;
223
+
224
+ program . parse ( [ '--bar' , 'BAR' ] , { from : 'user' } ) ;
225
+ expect ( program . opts ( ) ) . toEqual ( { bar : 'BAR' } ) ;
226
+ expect ( program . getOptionValueSource ( 'foo' ) ) . toBeUndefined ( ) ;
227
+ expect ( program . getOptionValueSource ( 'bar' ) ) . toEqual ( 'cli' ) ;
228
+ } ) ;
229
+
230
+ test ( 'when use options with option-argument and default then option values and sources reset' , ( ) => {
231
+ const program = new commander . Command ( )
232
+ . option ( '-f, --foo <value>' , 'description' , 'default-FOO' )
233
+ . option ( '-b, --bar <value>' , 'description' , 'default-BAR' ) ;
234
+
235
+ program . parse ( [ '--foo' , 'FOO' ] , { from : 'user' } ) ;
236
+ expect ( program . opts ( ) ) . toEqual ( { foo : 'FOO' , bar : 'default-BAR' } ) ;
237
+ expect ( program . getOptionValueSource ( 'foo' ) ) . toEqual ( 'cli' ) ;
238
+ expect ( program . getOptionValueSource ( 'bar' ) ) . toEqual ( 'default' ) ;
239
+
240
+ program . parse ( [ '--bar' , 'BAR' ] , { from : 'user' } ) ;
241
+ expect ( program . opts ( ) ) . toEqual ( { foo : 'default-FOO' , bar : 'BAR' } ) ;
242
+ expect ( program . getOptionValueSource ( 'foo' ) ) . toEqual ( 'default' ) ;
243
+ expect ( program . getOptionValueSource ( 'bar' ) ) . toEqual ( 'cli' ) ;
244
+ } ) ;
245
+
246
+ test ( 'when use negated options then option values reset' , ( ) => {
247
+ const program = new commander . Command ( )
248
+ . option ( '--no-foo' )
249
+ . option ( '--no-bar' ) ;
250
+
251
+ program . parse ( [ '--no-foo' ] , { from : 'user' } ) ;
252
+ expect ( program . opts ( ) ) . toEqual ( { foo : false , bar : true } ) ;
253
+
254
+ program . parse ( [ '--no-bar' ] , { from : 'user' } ) ;
255
+ expect ( program . opts ( ) ) . toEqual ( { foo : true , bar : false } ) ;
256
+ } ) ;
257
+
258
+ test ( 'when use variadic option then option values reset' , ( ) => {
259
+ const program = new commander . Command ( ) . option ( '--var <items...>' ) ;
260
+
261
+ program . parse ( [ '--var' , 'a' , 'b' ] , { from : 'user' } ) ;
262
+ expect ( program . opts ( ) ) . toEqual ( { var : [ 'a' , 'b' ] } ) ;
263
+
264
+ program . parse ( [ '--var' , 'c' ] , { from : 'user' } ) ;
265
+ expect ( program . opts ( ) ) . toEqual ( { var : [ 'c' ] } ) ;
266
+ } ) ;
267
+
268
+ test ( 'when use collect example then option value resets' , ( ) => {
269
+ function collect ( value , previous ) {
270
+ return previous . concat ( [ value ] ) ;
271
+ }
272
+ const program = new commander . Command ( ) ;
273
+ program . option ( '-c, --collect <value>' , 'repeatable value' , collect , [ ] ) ;
274
+
275
+ program . parse ( [ '-c' , 'a' , '-c' , 'b' ] , { from : 'user' } ) ;
276
+ expect ( program . opts ( ) ) . toEqual ( { collect : [ 'a' , 'b' ] } ) ;
277
+
278
+ program . parse ( [ '-c' , 'c' ] , { from : 'user' } ) ;
279
+ expect ( program . opts ( ) ) . toEqual ( { collect : [ 'c' ] } ) ;
280
+ } ) ;
281
+
282
+ test ( 'when use increaseVerbosity example then option value resets' , ( ) => {
283
+ function increaseVerbosity ( dummyValue , previous ) {
284
+ return previous + 1 ;
285
+ }
286
+ const program = new commander . Command ( ) ;
287
+ program . option (
288
+ '-v, --verbose' ,
289
+ 'verbosity that can be increased' ,
290
+ increaseVerbosity ,
291
+ 0 ,
292
+ ) ;
293
+
294
+ program . parse ( [ '-vvv' ] , { from : 'user' } ) ;
295
+ expect ( program . opts ( ) ) . toEqual ( { verbose : 3 } ) ;
296
+ program . parse ( [ '-vv' ] , { from : 'user' } ) ;
297
+
298
+ expect ( program . opts ( ) ) . toEqual ( { verbose : 2 } ) ;
299
+ program . parse ( [ ] , { from : 'user' } ) ;
300
+ expect ( program . opts ( ) ) . toEqual ( { verbose : 0 } ) ;
301
+ } ) ;
302
+
303
+ test ( 'when use parse and parseAsync then option values reset' , async ( ) => {
304
+ const program = new commander . Command ( ) . option ( '--black' ) . option ( '--white' ) ;
305
+
306
+ program . parse ( [ '--black' ] , { from : 'user' } ) ;
307
+ expect ( program . opts ( ) ) . toEqual ( { black : true } ) ;
308
+ await program . parseAsync ( [ '--white' ] , { from : 'user' } ) ;
309
+ expect ( program . opts ( ) ) . toEqual ( { white : true } ) ;
310
+ } ) ;
311
+
312
+ test ( 'when call subcommand then option values reset (program and subcommand)' , ( ) => {
313
+ const program = new commander . Command ( ) . option ( '--black' ) . option ( '--white' ) ;
314
+ const subcommand = program . command ( 'sub' ) . option ( '--red' ) . option ( '--green' ) ;
315
+
316
+ program . parse ( [ '--black' , 'sub' , '--red' ] , { from : 'user' } ) ;
317
+ expect ( subcommand . optsWithGlobals ( ) ) . toEqual ( { black : true , red : true } ) ;
318
+
319
+ program . parse ( [ '--white' , 'sub' , '--green' ] , { from : 'user' } ) ;
320
+ expect ( subcommand . optsWithGlobals ( ) ) . toEqual ( { white : true , green : true } ) ;
321
+ } ) ;
322
+
323
+ test ( 'when call different subcommand then no reset because lazy' , ( ) => {
324
+ // This is not a required behaviour, but is the intended behaviour.
325
+ const program = new commander . Command ( ) ;
326
+ const sub1 = program . command ( 'sub1' ) . option ( '--red' ) ;
327
+ const sub2 = program . command ( 'sub2' ) . option ( '--green' ) ;
328
+
329
+ program . parse ( [ 'sub1' , '--red' ] , { from : 'user' } ) ;
330
+ expect ( sub1 . opts ( ) ) . toEqual ( { red : true } ) ;
331
+ expect ( sub2 . opts ( ) ) . toEqual ( { } ) ;
332
+
333
+ program . parse ( [ 'sub2' , '--green' ] , { from : 'user' } ) ;
334
+ expect ( sub1 . opts ( ) ) . toEqual ( { red : true } ) ;
335
+ expect ( sub2 . opts ( ) ) . toEqual ( { green : true } ) ;
336
+ } ) ;
337
+
338
+ test ( 'when parse with different implied program name then name changes' , ( ) => {
339
+ const program = new commander . Command ( ) ;
340
+
341
+ program . parse ( [ 'node' , 'script1.js' ] ) ;
342
+ expect ( program . name ( ) ) . toEqual ( 'script1' ) ;
343
+
344
+ program . parse ( [ 'electron' , 'script2.js' ] ) ;
345
+ expect ( program . name ( ) ) . toEqual ( 'script2' ) ;
346
+ } ) ;
347
+
348
+ test ( 'when parse with different arguments then args change' , ( ) => {
349
+ // weak test, would work without store/reset!
350
+ const program = new commander . Command ( )
351
+ . argument ( '<first>' )
352
+ . argument ( '[second]' ) ;
353
+
354
+ program . parse ( [ 'one' , 'two' ] , { from : 'user' } ) ;
355
+ expect ( program . args ) . toEqual ( [ 'one' , 'two' ] ) ;
356
+
357
+ program . parse ( [ 'alpha' ] , { from : 'user' } ) ;
358
+ expect ( program . args ) . toEqual ( [ 'alpha' ] ) ;
359
+ } ) ;
360
+
361
+ test ( 'when parse with different arguments then rawArgs change' , ( ) => {
362
+ // weak test, would work without store/reset!
363
+ const program = new commander . Command ( )
364
+ . argument ( '<first>' )
365
+ . option ( '--white' )
366
+ . option ( '--black' ) ;
367
+
368
+ program . parse ( [ '--white' , 'one' ] , { from : 'user' } ) ;
369
+ expect ( program . rawArgs ) . toEqual ( [ '--white' , 'one' ] ) ;
370
+
371
+ program . parse ( [ '--black' , 'two' ] , { from : 'user' } ) ;
372
+ expect ( program . rawArgs ) . toEqual ( [ '--black' , 'two' ] ) ;
373
+ } ) ;
374
+
375
+ test ( 'when parse with different arguments then processedArgs change' , ( ) => {
376
+ // weak test, would work without store/reset!
377
+ const program = new commander . Command ( ) . argument (
378
+ '<first>' ,
379
+ 'first arg' ,
380
+ parseFloat ,
381
+ ) ;
382
+
383
+ program . parse ( [ 123 ] , { from : 'user' } ) ;
384
+ expect ( program . processedArgs ) . toEqual ( [ 123 ] ) ;
385
+
386
+ program . parse ( [ 456 ] , { from : 'user' } ) ;
387
+ expect ( program . processedArgs ) . toEqual ( [ 456 ] ) ;
388
+ } ) ;
389
+
390
+ test ( 'when parse subcommand then reset state before preSubcommand hook called' , ( ) => {
391
+ let hookCalled = false ;
392
+ const program = new commander . Command ( ) . hook (
393
+ 'preSubcommand' ,
394
+ ( thisCommand , subcommand ) => {
395
+ hookCalled = true ;
396
+ expect ( subcommand . opts ( ) ) . toEqual ( { } ) ;
397
+ } ,
398
+ ) ;
399
+ const subcommand = program . command ( 'sub' ) . option ( '--red' ) . option ( '--green' ) ;
400
+
401
+ hookCalled = false ;
402
+ program . parse ( [ 'sub' , '--red' ] , { from : 'user' } ) ;
403
+ expect ( hookCalled ) . toBe ( true ) ;
404
+ expect ( subcommand . opts ( ) ) . toEqual ( { red : true } ) ;
405
+
406
+ hookCalled = false ;
407
+ program . parse ( [ 'sub' , '--green' ] , { from : 'user' } ) ;
408
+ expect ( hookCalled ) . toBe ( true ) ;
409
+ expect ( subcommand . opts ( ) ) . toEqual ( { green : true } ) ;
410
+ } ) ;
411
+
412
+ test ( 'when using storeOptionsAsProperties then throw on second parse' , ( ) => {
413
+ const program = new commander . Command ( ) . storeOptionsAsProperties ( ) ;
414
+ program . parse ( ) ;
415
+ expect ( ( ) => {
416
+ program . parse ( ) ;
417
+ } ) . toThrow ( ) ;
418
+ } ) ;
419
+ } ) ;
0 commit comments