Skip to content

fix: ensure non-zero exit code for unknown CLI flags#213

Open
codxbrexx wants to merge 1 commit intometacall:masterfrom
codxbrexx:fix/unknown-cli-flag
Open

fix: ensure non-zero exit code for unknown CLI flags#213
codxbrexx wants to merge 1 commit intometacall:masterfrom
codxbrexx:fix/unknown-cli-flag

Conversation

@codxbrexx
Copy link
Contributor

Problem

Previously, when an unrecognized flag was passed to the CLI (e.g., metacall-deploy --invalid), the program would print a warning and the help message but exit with code 0. This made it impossible for CI/CD pipelines or scripts to detect usage errors.

What changed?

  1. src/deploy.ts: Introduced ErrorCode.InvalidArguments = 7 to the ErrorCode enum.
  2. src/help.ts: Refactored printHelp() to accept an optional exitCode parameter (defaulting to ErrorCode.Ok for backward compatibility and explicit --help calls).
  3. src/cli/unknown.ts: Updated handleUnknownArgs() to call printHelp(ErrorCode.InvalidArguments) when unknown flags are detected. It still exits with 0 for -h or --help.
  4. src/test/unknown.spec.ts: Created a comprehensive unit test suite (9 tests) that stubs process.exit and console.log to verify that the correct exit codes are returned for various flag combinations (valid help, single unknown, multiple unknown, etc.).

Resolves

Fixes #212

Testing

  • npm run unit has been executed.
  • Manual verification: Running ./dist/index.js --invalid-flag now correctly returns an exit code of 7.
image

- Add ErrorCode.InvalidArguments = 7
- printHelp() now accepts optional exitCode param (default: ErrorCode.Ok)
- handleUnknownArgs() exits with InvalidArguments for unknown flags,
  ErrorCode.Ok only for --help / -h
- Add test/unknown.spec.ts with 9 unit tests
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.

Bug: unknown CLI flags exit with code 0 instead of a failure code

1 participant