Skip to content

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

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

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

Conversation

@dmchaledev
Copy link
Copy Markdown
Contributor

Problem

The tool's headline command — the very first example in the README and the npx one-liner — crashes for every user:

$ 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)

Root cause

In src/cli.ts the --format value was resolved with:

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

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

So the documented default usage (text output, no flag) was completely broken; only the explicit --format=json / --format markdown forms worked.

Fix

  • Extract argument parsing into a pure, unit-tested parseArgs helper (src/args.ts).
  • Default to text when --format is omitted.
  • Handle both --format json and --format=json, and consume the space-separated value so it isn't mistaken for a positional file argument (this also makes flag-before-files ordering work).
  • Validate the format and report invalid values / missing args with a clean message instead of a raw stack trace (ArgError).

Tests

Added src/__tests__/args.test.ts covering the regression and every invocation form (default, --format=value, --format value, flag-first, missing args, invalid format).

Verification

no flag (was broken)  → OK
--format=json         → OK
--format markdown     → OK
flag first            → OK
invalid format        → "Invalid format: \"xml\". Supported formats: text, json, markdown." (exit 1, no stack)
missing args          → "Usage: sbom-diff <old.json> <new.json> ..." (exit 1)

npm run lint, npm run typecheck, npm test (26 passed), and npm run build all pass.

https://claude.ai/code/session_01LuhAMY6e7HPmHMGWGeCiVh


Generated by Claude Code

The headline command `sbom-diff <old> <new>` (and the npx one-liner in the
README) crashed for every user. With no --format flag, `args.indexOf('--format')`
returned -1, so `args[-1 + 1]` read the first file path as the format value,
producing 'Unsupported format: <path>'.

Extract argument parsing into a pure, tested `parseArgs` helper that:
- defaults to 'text' when --format is absent
- handles both '--format json' and '--format=json' in any position
- reports invalid formats and missing args with a clean message (no stack trace)

Add args.test.ts covering the regression and each invocation form.
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