Skip to content

Commit 57bd5de

Browse files
authored
fix: pretty message for invalid choices (#805)
1 parent 2895963 commit 57bd5de

File tree

1 file changed

+40
-3
lines changed

1 file changed

+40
-3
lines changed

src/entrypoints/_shared.ts

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ export const cli = yargs()
5050
other: 'NOT_ENOUGH_NON_OPTION_ARGUMENTS_INPUT {"got":%s,"need":%s}',
5151
},
5252
'Arguments %s and %s are mutually exclusive': 'ARGUMENTS_ARE_MUTUALLY_EXCLUSIVE_INPUT ["%s","%s"]',
53+
'Invalid values:': 'INVALID_VALUES_INPUT',
54+
'Argument: %s, Given: %s, Choices: %s': 'INVALID_ARGUMENT_OPTION {"argument":"%s","given":%s,"choices":[%s]}',
5355
})
5456
.option('help', {
5557
boolean: true,
@@ -82,7 +84,7 @@ export async function runCLI(entrypoint: string) {
8284
if (rawError) {
8385
cliDebugPrint('RunCLIError', { type: 'parsed', error: rawError?.message, parsed });
8486

85-
const errorMessageSplit = rawError.message.split(' ');
87+
const errorMessageSplit = rawError.message.split(' ').map((part) => part.trim());
8688

8789
const possibleCommands = [
8890
//
@@ -196,6 +198,41 @@ export async function runCLI(entrypoint: string) {
196198
break;
197199
}
198200

201+
// @ts-expect-error Intentional fallthrough
202+
case 'INVALID_VALUES_INPUT': {
203+
if (errorMessageSplit[2] === 'INVALID_ARGUMENT_OPTION') {
204+
const jsonPart = errorMessageSplit.slice(3).join(' ');
205+
206+
try {
207+
const json = JSON.parse(jsonPart) as { argument: string; given: string; choices: string[] };
208+
209+
const type = commandFlags.some((flag) => flag.matches(json.argument)) ? 'flag' : 'argument';
210+
211+
const representation = type === 'flag' ? `--${json.argument}` : json.argument;
212+
213+
error({
214+
message: [
215+
`Expected ${representation} to have one of the following values: ${json.choices.join(', ')}`,
216+
` ${chalk.red('>')} See more help with --help`,
217+
].join('\n'),
218+
});
219+
220+
break;
221+
} catch {
222+
cliDebugPrint('RunCLIError', {
223+
type: 'parse_error_invalid_choices',
224+
error: rawError.message,
225+
parsed,
226+
jsonPart,
227+
});
228+
229+
// fallthrough
230+
}
231+
}
232+
233+
// fallthrough
234+
}
235+
199236
default: {
200237
cliDebugPrint('RunCLIError', { type: 'unhandled', error: rawError.message, parsed });
201238

@@ -204,10 +241,10 @@ export async function runCLI(entrypoint: string) {
204241
'The CLI encountered an unhandled argument parsing error!',
205242
`Please report this issue at https://github.com/apify/apify-cli/issues, and provide the following information:`,
206243
'',
207-
`- Stack:\n\t${rawError.stack}`,
244+
`- Stack:\n${rawError.stack}`,
208245
'',
209246
'- Arguments (!!!only provide these as is if there is no sensitive information!!!):',
210-
`\t${JSON.stringify(process.argv.slice(2))}`,
247+
` ${JSON.stringify(process.argv.slice(2))}`,
211248
'',
212249
`- CLI version: \`${cliVersion}\``,
213250
`- CLI debug logs (process.env.APIFY_CLI_DEBUG): ${process.env.APIFY_CLI_DEBUG ? 'Enabled' : 'Disabled'}`,

0 commit comments

Comments
 (0)