Skip to content

fix(cli): default to text format when --format is omitted#6

Open
dmchaledev wants to merge 1 commit into
mainfrom
claude/magical-ptolemy-5rdy0
Open

fix(cli): default to text format when --format is omitted#6
dmchaledev wants to merge 1 commit into
mainfrom
claude/magical-ptolemy-5rdy0

Conversation

@dmchaledev
Copy link
Copy Markdown
Contributor

Problem

The primary documented CLI invocation crashes:

$ npx @hailbytes/sbom-diff old.json new.json
Error: Unsupported format: old.json
    at renderReport (dist/reporter.js:11:24)
    at main (dist/cli.js:30:17)

This is the very first example in the README's Quick Start, so every new user following the docs hits it.

Root cause

In src/cli.ts, format parsing fell through to the positional form even when --format was absent:

const formatArg = args.find(a => a.startsWith('--format='))?.split('=')[1]
  ?? args[args.indexOf('--format') + 1];

When --format is not passed, args.indexOf('--format') returns -1, so args[-1 + 1]args[0] → the old file path gets used as the format. renderReport() then throws on the unknown format.

Fix

  • Extract a unit-tested resolveFormat() helper that supports both --format json and --format=json, and defaults to text when the flag is absent or empty.
  • Validate the value and emit a clean one-line error (no stack trace) for unsupported formats: Unsupported format: xml. Valid formats: text, json, markdown.
  • Guard main() so it only runs when the module is invoked directly, enabling unit testing of the parser.
  • Add src/__tests__/cli.test.ts regression coverage.
  • Fix the README Programmatic example, which was also wrong — it showed await diff('old.cdx.json', 'new.cdx.json') (file paths, async), but diff() takes two parsed SBOM objects and is synchronous. Updated to parse() + diff() matching the real API, and corrected the return-type comments (VersionChange[], CVEEntry[]).

Verification

$ node dist/cli.js old.json new.json            # default → works, prints text report
$ node dist/cli.js old.json new.json --format markdown   # works
$ node dist/cli.js old.json new.json --format=json       # works
$ node dist/cli.js old.json new.json --format xml        # clean error, exit 1

npm run lint, npm run typecheck, and npm test all pass (25 tests, including 5 new CLI tests).

https://claude.ai/code/session_014i1tcsdNL9uaFYgd34bTV6


Generated by Claude Code

The default documented invocation `sbom-diff old.json new.json` crashed
with "Unsupported format". When --format was absent, args.indexOf('--format')
returned -1, so args[0] (the old file path) was used as the format value and
renderReport() threw.

- Extract a tested resolveFormat() helper supporting both `--format x` and
  `--format=x`, defaulting to 'text' when absent/empty
- Validate the value and emit a clean one-line error (no stack trace) for
  unsupported formats
- Guard main() so it only runs when invoked directly, enabling unit tests
- Add cli.test.ts regression coverage
- Fix the README programmatic example to match the real API (parse + diff
  on SBOM objects, not file paths)

https://claude.ai/code/session_014i1tcsdNL9uaFYgd34bTV6
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.

2 participants