@@ -1063,6 +1063,16 @@ test('auto-detect --no-foo as negated when strict:false and allowNegative', () =
1063
1063
process . execArgv = holdExecArgv ;
1064
1064
} ) ;
1065
1065
1066
+ test ( 'help arg value config must be a string' , ( ) => {
1067
+ const args = [ '-f' , 'bar' ] ;
1068
+ const options = { foo : { type : 'string' , short : 'f' , help : 'help text' } } ;
1069
+ const help = true ;
1070
+ assert . throws ( ( ) => {
1071
+ parseArgs ( { args, options, help } ) ;
1072
+ } , / T h e " h e l p " a r g u m e n t m u s t b e o f t y p e s t r i n g /
1073
+ ) ;
1074
+ } ) ;
1075
+
1066
1076
test ( 'help value for option must be a string' , ( ) => {
1067
1077
const args = [ ] ;
1068
1078
const options = { alpha : { type : 'string' , help : true } } ;
@@ -1072,158 +1082,111 @@ test('help value for option must be a string', () => {
1072
1082
) ;
1073
1083
} ) ;
1074
1084
1075
- test ( 'when help value for lone short option is added, then add help text' , ( ) => {
1085
+ test ( 'when help arg with help value for lone short option is added, then add help text' , ( ) => {
1076
1086
const args = [ '-f' , 'bar' ] ;
1077
1087
const options = { foo : { type : 'string' , short : 'f' , help : 'help text' } } ;
1078
- const printUsage = '-f, --foo <arg> help text' ;
1088
+ const help = 'Description for some awesome stuff:' ;
1089
+ const printUsage = help + '\n-f, --foo <arg> help text' ;
1079
1090
const expected = { values : { __proto__ : null , foo : 'bar' } , positionals : [ ] , printUsage } ;
1080
- const result = parseArgs ( { args, options, allowPositionals : true } ) ;
1091
+ const result = parseArgs ( { args, options, allowPositionals : true , help } ) ;
1081
1092
assert . deepStrictEqual ( result , expected ) ;
1082
1093
} ) ;
1083
1094
1084
- test ( 'when help value for short group option is added, then add help text' , ( ) => {
1095
+ test ( 'when help arg with help value for short group option is added, then add help text' , ( ) => {
1085
1096
const args = [ '-fm' , 'bar' ] ;
1086
1097
const options = { foo : { type : 'boolean' , short : 'f' , help : 'help text' } ,
1087
1098
moo : { type : 'string' , short : 'm' , help : 'help text' } } ;
1088
- const printUsage = '-f, --foo help text\n-m, --moo <arg> help text' ;
1099
+ const help = 'Description for some awesome stuff:' ;
1100
+ const printUsage = help + '\n-f, --foo help text\n-m, --moo <arg> help text' ;
1089
1101
const expected = { values : { __proto__ : null , foo : true , moo : 'bar' } , positionals : [ ] , printUsage } ;
1090
- const result = parseArgs ( { args, options, allowPositionals : true } ) ;
1102
+ const result = parseArgs ( { args, options, allowPositionals : true , help } ) ;
1091
1103
assert . deepStrictEqual ( result , expected ) ;
1092
1104
} ) ;
1093
1105
1094
- test ( 'when help value for short option and value is added, then add help text' , ( ) => {
1106
+ test ( 'when help arg with help value for short option and value is added, then add help text' , ( ) => {
1095
1107
const args = [ '-fFILE' ] ;
1096
1108
const options = { foo : { type : 'string' , short : 'f' , help : 'help text' } } ;
1097
- const printUsage = '-f, --foo <arg> help text' ;
1109
+ const help = 'Description for some awesome stuff:' ;
1110
+ const printUsage = help + '\n-f, --foo <arg> help text' ;
1098
1111
const expected = { values : { __proto__ : null , foo : 'FILE' } , positionals : [ ] , printUsage } ;
1099
- const result = parseArgs ( { args, options, allowPositionals : true } ) ;
1112
+ const result = parseArgs ( { args, options, allowPositionals : true , help } ) ;
1100
1113
assert . deepStrictEqual ( result , expected ) ;
1101
1114
} ) ;
1102
1115
1103
- test ( 'when help value for lone long option is added, then add help text' , ( ) => {
1116
+ test ( 'when help arg with help value for lone long option is added, then add help text' , ( ) => {
1104
1117
const args = [ '--foo' , 'bar' ] ;
1105
1118
const options = { foo : { type : 'string' , help : 'help text' } } ;
1106
- const printUsage = '--foo <arg> help text' ;
1119
+ const help = 'Description for some awesome stuff:' ;
1120
+ const printUsage = help + '\n--foo <arg> help text' ;
1107
1121
const expected = { values : { __proto__ : null , foo : 'bar' } , positionals : [ ] , printUsage } ;
1108
- const result = parseArgs ( { args, options, allowPositionals : true } ) ;
1122
+ const result = parseArgs ( { args, options, allowPositionals : true , help } ) ;
1109
1123
assert . deepStrictEqual ( result , expected ) ;
1110
1124
} ) ;
1111
1125
1112
- test ( 'when help value for lone long option and value is added, then add help text' , ( ) => {
1126
+ test ( 'when help arg with help value for lone long option and value is added, then add help text' , ( ) => {
1113
1127
const args = [ '--foo=bar' ] ;
1114
1128
const options = { foo : { type : 'string' , help : 'help text' } } ;
1115
- const printUsage = '--foo <arg> help text' ;
1116
- const expected = { values : { __proto__ : null , foo : 'bar' } , positionals : [ ] , printUsage } ;
1117
- const result = parseArgs ( { args, options, allowPositionals : true } ) ;
1118
- assert . deepStrictEqual ( result , expected ) ;
1119
- } ) ;
1120
-
1121
- test ( 'help value config must be a string' , ( ) => {
1122
- const args = [ '-f' , 'bar' ] ;
1123
- const options = { foo : { type : 'string' , short : 'f' , help : 'help text' } } ;
1124
- const help = true ;
1125
- assert . throws ( ( ) => {
1126
- parseArgs ( { args, options, help } ) ;
1127
- } , / T h e " h e l p " a r g u m e n t m u s t b e o f t y p e s t r i n g /
1128
- ) ;
1129
- } ) ;
1130
-
1131
- test ( 'when help value is added, then add initial help text' , ( ) => {
1132
- const args = [ '-f' , 'bar' ] ;
1133
- const options = { foo : { type : 'string' , short : 'f' , help : 'help text' } } ;
1134
1129
const help = 'Description for some awesome stuff:' ;
1135
- const printUsage = help + '\n-f, -- foo <arg> help text' ;
1130
+ const printUsage = help + '\n-- foo <arg> help text' ;
1136
1131
const expected = { values : { __proto__ : null , foo : 'bar' } , positionals : [ ] , printUsage } ;
1137
- const result = parseArgs ( { args, options, help } ) ;
1132
+ const result = parseArgs ( { args, options, allowPositionals : true , help } ) ;
1138
1133
assert . deepStrictEqual ( result , expected ) ;
1139
1134
} ) ;
1140
1135
1141
- function setupConsoleAndExit ( ) {
1142
- const originalLog = console . log ;
1143
- const originalExit = process . exit ;
1144
-
1145
- let output = '' ;
1146
- let exitCode = null ;
1147
-
1148
- console . log = ( message ) => {
1149
- output += message + '\n' ;
1150
- } ;
1151
-
1152
- process . exit = ( code ) => {
1153
- exitCode = code ;
1136
+ test ( 'when help arg with help values and without explicit help texts, then add help text' , ( ) => {
1137
+ const args = [
1138
+ '-h' , '-a' , 'val1' ,
1139
+ ] ;
1140
+ const options = {
1141
+ help : { type : 'boolean' , short : 'h' , help : 'Prints command line options' } ,
1142
+ alpha : { type : 'string' , short : 'a' , help : 'Alpha option help' } ,
1143
+ beta : { type : 'boolean' , short : 'b' , help : 'Beta option help' } ,
1144
+ charlie : { type : 'string' , short : 'c' } ,
1145
+ delta : { type : 'string' , help : 'Delta option help' } ,
1146
+ echo : { type : 'boolean' , short : 'e' , help : 'Echo option help' } ,
1147
+ foxtrot : { type : 'string' , help : 'Foxtrot option help' } ,
1148
+ golf : { type : 'boolean' , help : 'Golf option help' } ,
1149
+ hotel : { type : 'string' , help : 'Hotel option help' } ,
1150
+ india : { type : 'string' } ,
1151
+ juliet : { type : 'boolean' , short : 'j' , help : 'Juliet option help' } ,
1152
+ looooooooooooooongHelpText : {
1153
+ type : 'string' ,
1154
+ short : 'L' ,
1155
+ help : 'Very long option help text for demonstration purposes'
1156
+ }
1154
1157
} ;
1158
+ const help = 'Description for some awesome stuff:' ;
1155
1159
1156
- function restore ( ) {
1157
- console . log = originalLog ;
1158
- process . exit = originalExit ;
1159
- }
1160
-
1161
- return { getOutput : ( ) => output , getExitCode : ( ) => exitCode , restore } ;
1162
- }
1163
-
1164
- test ( 'when --help flag is present with help arg, prints all help text and exit' , ( ) => {
1165
- const { getOutput, getExitCode, restore } = setupConsoleAndExit ( ) ;
1166
-
1167
- try {
1168
- const args = [
1169
- '-h' , '-a' , 'val1' ,
1170
- ] ;
1171
- const options = {
1172
- help : { type : 'boolean' , short : 'h' , help : 'Prints command line options' } ,
1173
- alpha : { type : 'string' , short : 'a' , help : 'Alpha option help' } ,
1174
- beta : { type : 'boolean' , short : 'b' , help : 'Beta option help' } ,
1175
- charlie : { type : 'string' , short : 'c' } ,
1176
- delta : { type : 'string' , help : 'Delta option help' } ,
1177
- echo : { type : 'boolean' , short : 'e' , help : 'Echo option help' } ,
1178
- foxtrot : { type : 'string' , help : 'Foxtrot option help' } ,
1179
- golf : { type : 'boolean' , help : 'Golf option help' } ,
1180
- hotel : { type : 'string' , help : 'Hotel option help' } ,
1181
- india : { type : 'string' } ,
1182
- juliet : { type : 'boolean' , short : 'j' , help : 'Juliet option help' } ,
1183
- looooooooooooooongHelpText : {
1184
- type : 'string' ,
1185
- short : 'L' ,
1186
- help : 'Very long option help text for demonstration purposes'
1187
- }
1188
- } ;
1189
- const help = 'Description for some awesome stuff:' ;
1190
-
1191
- parseArgs ( { args, options, help } ) ;
1192
- } finally {
1193
- restore ( ) ;
1194
- }
1195
-
1196
- const expectedOutput =
1160
+ const result = parseArgs ( { args, options, help } ) ;
1161
+ const printUsage =
1197
1162
'Description for some awesome stuff:\n' +
1198
1163
'-h, --help Prints command line options\n' +
1199
1164
'-a, --alpha <arg> Alpha option help\n' +
1200
1165
'-b, --beta Beta option help\n' +
1166
+ '-c, --charlie <arg>\n' +
1201
1167
'--delta <arg> Delta option help\n' +
1202
1168
'-e, --echo Echo option help\n' +
1203
1169
'--foxtrot <arg> Foxtrot option help\n' +
1204
1170
'--golf Golf option help\n' +
1205
1171
'--hotel <arg> Hotel option help\n' +
1172
+ '--india <arg>\n' +
1206
1173
'-j, --juliet Juliet option help\n' +
1207
1174
'-L, --looooooooooooooongHelpText <arg>\n' +
1208
- ' Very long option help text for demonstration purposes\n ' ;
1175
+ ' Very long option help text for demonstration purposes' ;
1209
1176
1210
- assert . strictEqual ( getExitCode ( ) , 0 ) ;
1211
- assert . strictEqual ( getOutput ( ) , expectedOutput ) ;
1177
+ assert . strictEqual ( result . printUsage , printUsage ) ;
1212
1178
} ) ;
1213
1179
1214
- test ( 'when --help flag is present with help arg but no help text is available, prints help text and exit' , ( ) => {
1215
- const { getOutput, getExitCode, restore } = setupConsoleAndExit ( ) ;
1216
-
1217
- try {
1218
- const args = [ '-a' , 'val1' , '--help' ] ;
1219
- const help = 'Description for some awesome stuff:' ;
1220
- const options = { alpha : { type : 'string' , short : 'a' } , help : { type : 'boolean' } } ;
1180
+ test ( 'when help arg but no help text is available, then add help text' , ( ) => {
1181
+ const args = [ '-a' , 'val1' , '--help' ] ;
1182
+ const help = 'Description for some awesome stuff:' ;
1183
+ const options = { alpha : { type : 'string' , short : 'a' } , help : { type : 'boolean' } } ;
1184
+ const printUsage =
1185
+ 'Description for some awesome stuff:\n' +
1186
+ '-a, --alpha <arg>\n' +
1187
+ '--help' ;
1221
1188
1222
- parseArgs ( { args, options, help } ) ;
1223
- } finally {
1224
- restore ( ) ;
1225
- }
1189
+ const result = parseArgs ( { args, options, help } ) ;
1226
1190
1227
- assert . strictEqual ( getExitCode ( ) , 0 ) ;
1228
- assert . strictEqual ( getOutput ( ) , 'Description for some awesome stuff:\n' ) ;
1191
+ assert . strictEqual ( result . printUsage , printUsage ) ;
1229
1192
} ) ;
0 commit comments