Skip to content

fix: exit non-zero on unknown commands and options - #18

Merged
Asdivyansh merged 1 commit into
SilosLabs:mainfrom
TheDreamSaver:fix/cli-exit-codes
Nov 15, 2026
Merged

Asdivyansh merged 1 commit into
SilosLabs:mainfrom
TheDreamSaver:fix/cli-exit-codes

Conversation

@TheDreamSaver

Copy link
Copy Markdown
Member

Fixes #17

What

  • src/cli.ts: the top-level catch now exits with err.exitCode for a CommanderError (Commander has already printed the message, help or version text), and exits 1 for any other throw, including non-Error values. Previously every CommanderError, and anything that wasn't an Error, was swallowed with exit 0.
  • New tests/cli.test.ts: runs the real entry point (node --import tsx src/cli.ts …, with a throwaway HOME) and checks the exit codes. cli.ts parses argv on import, so it can't be tested in-process.
Command before after
silo bogus-cmd 0 1
silo --bogus 0 1
silo build --bogus 1 1
silo --help, silo --version, silo build --help 0 0
silo (no command) 0 1

One behaviour change to check: bare silo prints help and now exits 1. That's Commander's own default for "no command given" (it prints the help to stderr), and it's what git does. If you'd rather keep 0 there, it's a one-line special case for err.code === "commander.help".

Testing

  • npm run typecheck: passes.
  • npm test: 25 files, 345 tests pass (4 new).
  • npm run build: succeeds.
  • The new tests fail on the unmodified cli.ts (both unknown-command and unknown-option cases exit 0) and pass with the fix.
  • A trial merge with open fix: make build --no-cache and start --rebuild work #13 (which also touches src/cli.ts) is clean.

program.exitOverride() makes Commander throw a CommanderError instead of
exiting, but the top-level catch dropped every CommanderError (and every
non-Error throw), so `silo bogus-cmd` and `silo --bogus` exited 0 after
printing the error. Keep Commander's exit code: 0 for --help/--version,
1 for errors.

Add tests that run the real entry point and check the exit codes.

Fixes SilosLabs#17
@Asdivyansh

Copy link
Copy Markdown
Member

Thanks for the fix!

@Asdivyansh
Asdivyansh merged commit 310022f into SilosLabs:main Nov 15, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unknown commands and options exit 0 (silo bogus-cmd, silo --bogus)

2 participants