@@ -992,3 +992,63 @@ test('multiple as false should expect a String', () => {
992992 } , / " o p t i o n s \. a l p h a \. d e f a u l t " p r o p e r t y m u s t b e o f t y p e s t r i n g /
993993 ) ;
994994} ) ;
995+
996+ // test "--no-" prefix
997+ test ( 'args are passed `type: "string"` and start with "--no-"' , ( ) => {
998+ const args = [ '--no-alpha' ] ;
999+ const options = { alpha : { type : 'string' } } ;
1000+ assert . throws ( ( ) => {
1001+ parseArgs ( { args, options } ) ;
1002+ } , {
1003+ code : 'ERR_PARSE_ARGS_UNKNOWN_OPTION'
1004+ } ) ;
1005+ } ) ;
1006+
1007+ test ( 'args are passed `type: "boolean"` and start with "--no-"' , ( ) => {
1008+ const args = [ '--no-alpha' ] ;
1009+ const options = { alpha : { type : 'boolean' } } ;
1010+ const expected = { values : { __proto__ : null , alpha : false } , positionals : [ ] } ;
1011+ assert . deepStrictEqual ( parseArgs ( { args, options } ) , expected ) ;
1012+ } ) ;
1013+
1014+ test ( 'args start with "--no-" and passed `default: "true"`' , ( ) => {
1015+ const args = [ '--no-alpha' ] ;
1016+ const options = { alpha : { type : 'boolean' , default : true } } ;
1017+ const expected = { values : { __proto__ : null , alpha : false } , positionals : [ ] } ;
1018+ assert . deepStrictEqual ( parseArgs ( { args, options } ) , expected ) ;
1019+ } ) ;
1020+
1021+ test ( 'args start with "--no-" and passed `default: "false"`' , ( ) => {
1022+ const args = [ '--no-alpha' ] ;
1023+ const options = { alpha : { type : 'boolean' , default : false } } ;
1024+ const expected = { values : { __proto__ : null , alpha : true } , positionals : [ ] } ;
1025+ assert . deepStrictEqual ( parseArgs ( { args, options } ) , expected ) ;
1026+ } ) ;
1027+
1028+ test ( 'args start with "--no-" and multiple as true' , ( ) => {
1029+ const args = [ '--no-alpha' , '--alpha' , '--no-alpha' ] ;
1030+ const options = { alpha : { type : 'boolean' , multiple : true } } ;
1031+ const expected = { values : { __proto__ : null , alpha : [ false , true , false ] } , positionals : [ ] } ;
1032+ assert . deepStrictEqual ( parseArgs ( { args, options } ) , expected ) ;
1033+ } ) ;
1034+
1035+ test ( 'args start with "--no-" and passed multiple arguments' , ( ) => {
1036+ const args = [ '--alpha' , '--no-alpha' , '--alpha' ] ;
1037+ const options = { alpha : { type : 'boolean' } } ;
1038+ const expected = { values : { __proto__ : null , alpha : true } , positionals : [ ] } ;
1039+ assert . deepStrictEqual ( parseArgs ( { args, options } ) , expected ) ;
1040+ } ) ;
1041+
1042+ test ( 'correct default args with prefix "--no-" when normal arguments' , ( ) => {
1043+ const holdArgv = process . argv ;
1044+ process . argv = [ process . argv0 , 'script.js' , '--no-foo' ] ;
1045+ const holdExecArgv = process . execArgv ;
1046+ process . execArgv = [ ] ;
1047+ const result = parseArgs ( { strict : false } ) ;
1048+
1049+ const expected = { values : { __proto__ : null , foo : false } ,
1050+ positionals : [ ] } ;
1051+ assert . deepStrictEqual ( result , expected ) ;
1052+ process . argv = holdArgv ;
1053+ process . execArgv = holdExecArgv ;
1054+ } ) ;
0 commit comments