@@ -436,3 +436,137 @@ test('null prototype: when --toString then values.toString is true', () => {
436436 const result = parseArgs ( { args, options } ) ;
437437 assert . deepStrictEqual ( result , expectedResult ) ;
438438} ) ;
439+
440+ const candidateGreedyOptions = [
441+ '' ,
442+ '-' ,
443+ '--' ,
444+ 'abc' ,
445+ '123' ,
446+ '-s' ,
447+ '--foo' ,
448+ ] ;
449+
450+ candidateGreedyOptions . forEach ( ( value ) => {
451+ test ( `greedy: when short option with value '${ value } ' then eaten` , ( ) => {
452+ const args = [ '-w' , value ] ;
453+ const options = { with : { type : 'string' , short : 'w' } } ;
454+ const expectedResult = { values : { __proto__ : null , with : value } , positionals : [ ] } ;
455+
456+ const result = parseArgs ( { args, options, strict : false } ) ;
457+ assert . deepStrictEqual ( result , expectedResult ) ;
458+ } ) ;
459+
460+ test ( `greedy: when long option with value '${ value } ' then eaten` , ( ) => {
461+ const args = [ '--with' , value ] ;
462+ const options = { with : { type : 'string' , short : 'w' } } ;
463+ const expectedResult = { values : { __proto__ : null , with : value } , positionals : [ ] } ;
464+
465+ const result = parseArgs ( { args, options, strict : false } ) ;
466+ assert . deepStrictEqual ( result , expectedResult ) ;
467+ } ) ;
468+ } ) ;
469+
470+ test ( 'strict: when candidate option value is plain text then does not throw' , ( ) => {
471+ const args = [ '--with' , 'abc' ] ;
472+ const options = { with : { type : 'string' } } ;
473+ const expectedResult = { values : { __proto__ : null , with : 'abc' } , positionals : [ ] } ;
474+
475+ const result = parseArgs ( { args, options, strict : true } ) ;
476+ assert . deepStrictEqual ( result , expectedResult ) ;
477+ } ) ;
478+
479+ test ( "strict: when candidate option value is '-' then does not throw" , ( ) => {
480+ const args = [ '--with' , '-' ] ;
481+ const options = { with : { type : 'string' } } ;
482+ const expectedResult = { values : { __proto__ : null , with : '-' } , positionals : [ ] } ;
483+
484+ const result = parseArgs ( { args, options, strict : true } ) ;
485+ assert . deepStrictEqual ( result , expectedResult ) ;
486+ } ) ;
487+
488+ test ( "strict: when candidate option value is '--' then throws" , ( ) => {
489+ const args = [ '--with' , '--' ] ;
490+ const options = { with : { type : 'string' } } ;
491+
492+ assert . throws ( ( ) => {
493+ parseArgs ( { args, options } ) ;
494+ } , {
495+ code : 'ERR_PARSE_ARGS_INVALID_OPTION_VALUE'
496+ } ) ;
497+ } ) ;
498+
499+ test ( 'strict: when candidate option value is short option then throws' , ( ) => {
500+ const args = [ '--with' , '-a' ] ;
501+ const options = { with : { type : 'string' } } ;
502+
503+ assert . throws ( ( ) => {
504+ parseArgs ( { args, options } ) ;
505+ } , {
506+ code : 'ERR_PARSE_ARGS_INVALID_OPTION_VALUE'
507+ } ) ;
508+ } ) ;
509+
510+ test ( 'strict: when candidate option value is short option digit then throws' , ( ) => {
511+ const args = [ '--with' , '-1' ] ;
512+ const options = { with : { type : 'string' } } ;
513+
514+ assert . throws ( ( ) => {
515+ parseArgs ( { args, options } ) ;
516+ } , {
517+ code : 'ERR_PARSE_ARGS_INVALID_OPTION_VALUE'
518+ } ) ;
519+ } ) ;
520+
521+ test ( 'strict: when candidate option value is long option then throws' , ( ) => {
522+ const args = [ '--with' , '--foo' ] ;
523+ const options = { with : { type : 'string' } } ;
524+
525+ assert . throws ( ( ) => {
526+ parseArgs ( { args, options } ) ;
527+ } , {
528+ code : 'ERR_PARSE_ARGS_INVALID_OPTION_VALUE'
529+ } ) ;
530+ } ) ;
531+
532+ test ( 'strict: when short option and suspect value then throws with short option in error message' , ( ) => {
533+ const args = [ '-w' , '--foo' ] ;
534+ const options = { with : { type : 'string' , short : 'w' } } ;
535+
536+ assert . throws ( ( ) => {
537+ parseArgs ( { args, options } ) ;
538+ } , / f o r ' - w ' /
539+ ) ;
540+ } ) ;
541+
542+ test ( 'strict: when long option and suspect value then throws with long option in error message' , ( ) => {
543+ const args = [ '--with' , '--foo' ] ;
544+ const options = { with : { type : 'string' } } ;
545+
546+ assert . throws ( ( ) => {
547+ parseArgs ( { args, options } ) ;
548+ } , / f o r ' - - w i t h ' /
549+ ) ;
550+ } ) ;
551+
552+ test ( 'strict: when short option and suspect value then throws with whole expected message' , ( ) => {
553+ const args = [ '-w' , '--foo' ] ;
554+ const options = { with : { type : 'string' , short : 'w' } } ;
555+
556+ assert . throws ( ( ) => {
557+ parseArgs ( { args, options } ) ;
558+ // eslint-disable-next-line max-len
559+ } , / E r r o r : O p t i o n ' - w ' a r g u m e n t i s a m b i g u o u s \. \n D i d y o u f o r g e t t o s p e c i f y t h e o p t i o n a r g u m e n t f o r ' - w ' \? \n O r t o s p e c i f y a n o p t i o n a r g u m e n t s t a r t i n g w i t h a d a s h u s e ' - - w i t h = - X Y Z ' o r ' - w - X Y Z ' \. /
560+ ) ;
561+ } ) ;
562+
563+ test ( 'strict: when long option and suspect value then throws with whole expected message' , ( ) => {
564+ const args = [ '--with' , '--foo' ] ;
565+ const options = { with : { type : 'string' , short : 'w' } } ;
566+
567+ assert . throws ( ( ) => {
568+ parseArgs ( { args, options } ) ;
569+ // eslint-disable-next-line max-len
570+ } , / E r r o r : O p t i o n ' - - w i t h ' a r g u m e n t i s a m b i g u o u s \. \n D i d y o u f o r g e t t o s p e c i f y t h e o p t i o n a r g u m e n t f o r ' - - w i t h ' \? \n O r t o s p e c i f y a n o p t i o n a r g u m e n t s t a r t i n g w i t h a d a s h u s e ' - - w i t h = - X Y Z ' \. /
571+ ) ;
572+ } ) ;
0 commit comments