@@ -823,3 +823,159 @@ test('tokens: strict:false with -- --', () => {
823
823
const { tokens } = parseArgs ( { strict : false , args, tokens : true } ) ;
824
824
assert . deepStrictEqual ( tokens , expectedTokens ) ;
825
825
} ) ;
826
+
827
+ test ( 'default must be a boolean when option type is boolean' , ( ) => {
828
+ const args = [ ] ;
829
+ const options = { alpha : { type : 'boolean' , default : 'not a boolean' } } ;
830
+ assert . throws ( ( ) => {
831
+ parseArgs ( { args, options } ) ;
832
+ } , `TypeError [ERR_INVALID_ARG_TYPE]: The "options.alpha.default" property must be an instance of Array. Received type string ('not an array')`
833
+ ) ;
834
+ } ) ;
835
+
836
+ test ( 'default must be a boolean array when option type is boolean and multiple' , ( ) => {
837
+ const args = [ ] ;
838
+ const options = { alpha : { type : 'boolean' , multiple : true , default : 'not an array' } } ;
839
+ assert . throws ( ( ) => {
840
+ parseArgs ( { args, options } ) ;
841
+ } , `TypeError [ERR_INVALID_ARG_TYPE]: The "options.alpha.default" property must be an instance of Array. Received type string ('not an array')`
842
+ ) ;
843
+ } ) ;
844
+
845
+ test ( 'default must be a boolean array when option type is string and multiple is true' , ( ) => {
846
+ const args = [ ] ;
847
+ const options = { alpha : { type : 'boolean' , multiple : true , default : [ true , true , 42 ] } } ;
848
+ assert . throws ( ( ) => {
849
+ parseArgs ( { args, options } ) ;
850
+ } , `TypeError [ERR_INVALID_ARG_TYPE]: The "options.alpha.default[2]" property must be of type boolean. Received type number (42)`
851
+ ) ;
852
+ } ) ;
853
+
854
+ test ( 'default must be a string when option type is string' , ( ) => {
855
+ const args = [ ] ;
856
+ const options = { alpha : { type : 'string' , default : true } } ;
857
+ assert . throws ( ( ) => {
858
+ parseArgs ( { args, options } ) ;
859
+ } , `TypeError [ERR_INVALID_ARG_TYPE]: The "options.alpha.default" property must be of type string. Received type boolean (true)`
860
+ ) ;
861
+ } ) ;
862
+
863
+ test ( 'default must be an array when option type is string and multiple is true' , ( ) => {
864
+ const args = [ ] ;
865
+ const options = { alpha : { type : 'string' , multiple : true , default : 'not an array' } } ;
866
+ assert . throws ( ( ) => {
867
+ parseArgs ( { args, options } ) ;
868
+ } , `TypeError [ERR_INVALID_ARG_TYPE]: The "options.alpha.default" property must be an instance of Array. Received type string ('not an array')`
869
+ ) ;
870
+ } ) ;
871
+
872
+ test ( 'default must be a string array when option type is string and multiple is true' , ( ) => {
873
+ const args = [ ] ;
874
+ const options = { alpha : { type : 'string' , multiple : true , default : [ 'str' , 42 ] } } ;
875
+ assert . throws ( ( ) => {
876
+ parseArgs ( { args, options } ) ;
877
+ } , `TypeError [ERR_INVALID_ARG_TYPE]: The "options.alpha.default[1]" property must be of type string. Received type number (42)`
878
+ ) ;
879
+ } ) ;
880
+
881
+ test ( 'default accepted input when multiple is true' , ( ) => {
882
+ const args = [ '--inputStringArr' , 'c' , '--inputStringArr' , 'd' , '--inputBoolArr' , '--inputBoolArr' ] ;
883
+ const options = {
884
+ inputStringArr : { type : 'string' , multiple : true , default : [ 'a' , 'b' ] } ,
885
+ emptyStringArr : { type : 'string' , multiple : true , default : [ ] } ,
886
+ fullStringArr : { type : 'string' , multiple : true , default : [ 'a' , 'b' ] } ,
887
+ inputBoolArr : { type : 'boolean' , multiple : true , default : [ false , true , false ] } ,
888
+ emptyBoolArr : { type : 'boolean' , multiple : true , default : [ ] } ,
889
+ fullBoolArr : { type : 'boolean' , multiple : true , default : [ false , true , false ] } ,
890
+ } ;
891
+ const expected = { values : { __proto__ : null ,
892
+ inputStringArr : [ 'c' , 'd' ] ,
893
+ inputBoolArr : [ true , true ] ,
894
+ emptyStringArr : [ ] ,
895
+ fullStringArr : [ 'a' , 'b' ] ,
896
+ emptyBoolArr : [ ] ,
897
+ fullBoolArr : [ false , true , false ] } ,
898
+ positionals : [ ] } ;
899
+ const result = parseArgs ( { args, options } ) ;
900
+ assert . deepStrictEqual ( result , expected ) ;
901
+ } ) ;
902
+
903
+ test ( 'when default is set, the option must be added as result' , ( ) => {
904
+ const args = [ ] ;
905
+ const options = {
906
+ a : { type : 'string' , default : 'HELLO' } ,
907
+ b : { type : 'boolean' , default : false } ,
908
+ c : { type : 'boolean' , default : true }
909
+ } ;
910
+ const expected = { values : { __proto__ : null , a : 'HELLO' , b : false , c : true } , positionals : [ ] } ;
911
+
912
+ const result = parseArgs ( { args, options } ) ;
913
+ assert . deepStrictEqual ( result , expected ) ;
914
+ } ) ;
915
+
916
+ test ( 'when default is set, the args value takes precedence' , ( ) => {
917
+ const args = [ '--a' , 'WORLD' , '--b' , '-c' ] ;
918
+ const options = {
919
+ a : { type : 'string' , default : 'HELLO' } ,
920
+ b : { type : 'boolean' , default : false } ,
921
+ c : { type : 'boolean' , default : true }
922
+ } ;
923
+ const expected = { values : { __proto__ : null , a : 'WORLD' , b : true , c : true } , positionals : [ ] } ;
924
+
925
+ const result = parseArgs ( { args, options } ) ;
926
+ assert . deepStrictEqual ( result , expected ) ;
927
+ } ) ;
928
+
929
+ test ( 'tokens should not include the default options' , ( ) => {
930
+ const args = [ ] ;
931
+ const options = {
932
+ a : { type : 'string' , default : 'HELLO' } ,
933
+ b : { type : 'boolean' , default : false } ,
934
+ c : { type : 'boolean' , default : true }
935
+ } ;
936
+
937
+ const expectedTokens = [ ] ;
938
+
939
+ const { tokens } = parseArgs ( { args, options, tokens : true } ) ;
940
+ assert . deepStrictEqual ( tokens , expectedTokens ) ;
941
+ } ) ;
942
+
943
+ test ( 'tokens:true should not include the default options after the args input' , ( ) => {
944
+ const args = [ '--z' , 'zero' , 'positional-item' ] ;
945
+ const options = {
946
+ z : { type : 'string' } ,
947
+ a : { type : 'string' , default : 'HELLO' } ,
948
+ b : { type : 'boolean' , default : false } ,
949
+ c : { type : 'boolean' , default : true }
950
+ } ;
951
+
952
+ const expectedTokens = [
953
+ { kind : 'option' , name : 'z' , rawName : '--z' , index : 0 , value : 'zero' , inlineValue : false } ,
954
+ { kind : 'positional' , index : 2 , value : 'positional-item' } ,
955
+ ] ;
956
+
957
+ const { tokens } = parseArgs ( { args, options, tokens : true , allowPositionals : true } ) ;
958
+ assert . deepStrictEqual ( tokens , expectedTokens ) ;
959
+ } ) ;
960
+
961
+ test ( 'proto as default value must be ignored' , ( ) => {
962
+ const args = [ ] ;
963
+ const options = Object . create ( null ) ;
964
+
965
+ // eslint-disable-next-line no-proto
966
+ options . __proto__ = { type : 'string' , default : 'HELLO' } ;
967
+
968
+ const result = parseArgs ( { args, options, allowPositionals : true } ) ;
969
+ const expected = { values : { __proto__ : null } , positionals : [ ] } ;
970
+ assert . deepStrictEqual ( result , expected ) ;
971
+ } ) ;
972
+
973
+
974
+ test ( 'multiple as false should expect a String' , ( ) => {
975
+ const args = [ ] ;
976
+ const options = { alpha : { type : 'string' , multiple : false , default : [ 'array' ] } } ;
977
+ assert . throws ( ( ) => {
978
+ parseArgs ( { args, options } ) ;
979
+ } , `TypeError [ERR_INVALID_ARG_TYPE]: The "options.alpha.default" property must be of type string. Received an instance of Array`
980
+ ) ;
981
+ } ) ;
0 commit comments