From 8a36dd8568bcbea07a5e269326fcec2927924107 Mon Sep 17 00:00:00 2001 From: Misha Kaletsky Date: Wed, 29 May 2024 17:12:09 -0400 Subject: [PATCH] More helpful error message Add levenshtein helpfulness some day? --- src/index.ts | 4 +++- test/e2e.test.ts | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index 827e978..bcf1d4b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -142,7 +142,9 @@ export const trpcCli = ({router, ...params}: TrpcCliParams< const procedureInfo = command && procedureMap[command] if (!procedureInfo) { - die('No command provided.') + const name = JSON.stringify(command || parsedArgv._[0]) + const message = name ? `Command not found: ${name}.` : 'No command specified.' + return die(message) } if (Object.entries(unknownFlags).length > 0) { diff --git a/test/e2e.test.ts b/test/e2e.test.ts index 7b12386..b6c778e 100644 --- a/test/e2e.test.ts +++ b/test/e2e.test.ts @@ -114,6 +114,40 @@ test('cli divide failure', async () => { `) }) +test('cli non-existent command', async () => { + const output = await tsx('calculator', ['multiploo', '2', '3']) + expect(output).toMatchInlineSnapshot(` + "Command not found: "multiploo". + Commands: + add Add two numbers. Use this if you and your friend both have apples, and you want to know how many apples there are in total. + subtract Subtract two numbers. Useful if you have a number and you want to make it smaller. + multiply Multiply two numbers together. Useful if you want to count the number of tiles on your bathroom wall and are short on time. + divide Divide two numbers. Useful if you have a number and you want to make it smaller and \`subtract\` isn't quite powerful enough for you. + + Flags: + -h, --help Show help + --verbose-errors Throw raw errors (by default errors are summarised) + " + `) +}) + +test('cli no command', async () => { + const output = await tsx('calculator', []) + expect(output).toMatchInlineSnapshot(` + "No command specified. + Commands: + add Add two numbers. Use this if you and your friend both have apples, and you want to know how many apples there are in total. + subtract Subtract two numbers. Useful if you have a number and you want to make it smaller. + multiply Multiply two numbers together. Useful if you want to count the number of tiles on your bathroom wall and are short on time. + divide Divide two numbers. Useful if you have a number and you want to make it smaller and \`subtract\` isn't quite powerful enough for you. + + Flags: + -h, --help Show help + --verbose-errors Throw raw errors (by default errors are summarised) + " + `) +}) + test('migrations help', async () => { const output = await tsx('migrations', ['--help']) expect(output).toMatchInlineSnapshot(`