fix(cli): default --format to text instead of crashing on the documented usage#12
Open
dmchaledev wants to merge 1 commit into
Open
fix(cli): default --format to text instead of crashing on the documented usage#12dmchaledev wants to merge 1 commit into
dmchaledev wants to merge 1 commit into
Conversation
The most basic documented invocation, `sbom-diff old.json new.json`, crashed
with `Error: Unsupported format: <path>`. When `--format` was absent,
`args.indexOf('--format')` returned -1, so `args[-1 + 1]` resolved to the first
positional argument (the old SBOM path). That non-undefined value defeated the
`?? 'text'` default and was passed to renderReport, which threw.
Extract argument parsing into a testable `parseArgs` (src/args.ts) that:
- defaults the format to `text` when --format is omitted
- supports both `--format json` and `--format=json`
- validates the format and prints a clear one-line error on bad input
Add unit tests covering the default, both flag forms, and error cases.
https://claude.ai/code/session_01YMe4qfgnC6BuCvLreBNnkQ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The package's primary, documented invocation crashes. Running the README's quick-start command:
exits with:
Root cause
In
src/cli.tsthe--formatvalue was resolved with:When
--formatis absent,args.indexOf('--format')returns-1, soargs[-1 + 1]resolves toargs[0]— the first positional argument (the old SBOM path). That value is notundefined, so the?? 'text'default never applies, and the path string gets passed torenderReport, which throws on the unknown format.Net effect: the only way to not crash was to always pass
--formatexplicitly, contradicting the README.Fix
parseArgs(src/args.ts) that:textwhen--formatis omitted,--format json(spaced) and--format=json(inline),Invalid format: "xml". Valid formats: text, json, markdown.) instead of a stack trace.cli.tsnow consumesparseArgsand prints usage/validation errors to stderr with exit code 1.src/__tests__/args.test.tscovering the default, both flag forms, the missing-args case, and the invalid-format case.Verification
npm run typecheck,npm run lint, andnpm test(25 tests) all pass.Noted follow-up (out of scope for this PR)
While testing I found a separate correctness issue:
diff()keys components bypurl ?? name, but purls embed the version (pkg:npm/lodash@4.17.20). So a version bump produces two distinct keys and is reported as removed + added rather than upgraded — silently disabling the headline "upgraded dependencies" feature for any SBOM that includes versioned purls. Happy to file a separate issue / PR to normalize purls (strip the version qualifier) for matching.https://claude.ai/code/session_01YMe4qfgnC6BuCvLreBNnkQ
Generated by Claude Code