From 7f3c1da241fa406c74b9d1508d3f788588b0397c Mon Sep 17 00:00:00 2001 From: DanMaly Date: Thu, 12 Feb 2026 14:22:31 +0100 Subject: [PATCH] Fix CLI stack trace on help errors --- src/cli.ts | 12 +++++++----- test/cli-smoke.test.ts | 12 ++++++++++++ 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/cli.ts b/src/cli.ts index 3e64327..550e622 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -57,11 +57,13 @@ export async function main(argv: string[]): Promise { try { await program.parseAsync(argv) } catch (err) { - if ( - err instanceof CommanderError && - (err.code === 'commander.helpDisplayed' || err.code === 'commander.version') - ) { - return + if (err instanceof CommanderError) { + if (err.code === 'commander.helpDisplayed' || err.code === 'commander.version') { + return + } + + console.error(err.message) + process.exit(err.exitCode || 1) } if (err instanceof ShpeckError || err instanceof GitError) { diff --git a/test/cli-smoke.test.ts b/test/cli-smoke.test.ts index 269b9d0..b3e9ff7 100644 --- a/test/cli-smoke.test.ts +++ b/test/cli-smoke.test.ts @@ -15,6 +15,18 @@ describe('cli smoke', () => { expect(stdout).toContain('Usage: shpeck') }) + it('does not print stack trace when no args', async () => { + const { exitCode, stdout, stderr } = await runCommand(['bun', cliPath], { + cwd: repoRoot, + allowNonZeroExit: true, + }) + + expect(exitCode).toBeGreaterThan(0) + expect(stderr).toContain('Usage: shpeck') + expect(stderr).not.toContain('Error:') + expect(stderr).not.toContain('at ') + }) + it('fails on unknown command', async () => { const { exitCode, stderr } = await runCommand(['bun', cliPath, 'nope'], { cwd: repoRoot,