fix(cli): correct --format parsing so the default invocation works#13
Open
dmchaledev wants to merge 1 commit into
Open
fix(cli): correct --format parsing so the default invocation works#13dmchaledev wants to merge 1 commit into
dmchaledev wants to merge 1 commit into
Conversation
The CLI crashed on the documented default invocation
(sbom-diff old.json new.json) with 'Unsupported format: old.json'.
When no --format flag was present, args.indexOf('--format') returned
-1, so args[indexOf+1] resolved to args[0] (the first file path),
which was then passed as the report format.
A space-separated '--format json' placed before the positional
arguments also broke, because positional filtering only checked
startsWith('--') and so treated the value 'json' as a file path.
Rewrite argument parsing into a single, order-independent pass that:
- defaults to 'text' when no flag is given
- supports both '--format=value' and '--format value'
- consumes the flag value so it is never mistaken for a path
- validates the format and reports a clean error (no stack trace)
- rejects unknown options
Extract the parser as exported parseArgs() and guard main() so it
only runs when the module is invoked directly, enabling unit tests.
Add cli.test.ts covering the regressions and edge cases.
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.
Problem
The CLI crashes on its own headline documented usage. Running the first example from the README:
fails with:
Root cause
src/cli.tsderived the format like this:When no
--formatflag is present,args.indexOf('--format')returns-1, soargs[-1 + 1]→args[0]— the first file path — gets used as the report format. The default text output (the most common usage) therefore never worked.A second bug: a space-separated
--format jsonplaced before the positional arguments was also broken, because positional filtering only checkedstartsWith('--')and so treated the valuejsonas a file path:Fix
Rewrite argument parsing into a single, order-independent pass (
parseArgs) that:textwhen no flag is given--format=valueand--format valueparseArgsis exported andmain()is guarded to run only when the module is invoked directly, which makes the parser unit-testable.Tests
Added
src/__tests__/cli.test.tscovering both regressions plus edge cases (missing value, invalid value, unknown option, flag-before-paths). Full suite: 27 passing.npm run buildandnpm run lintare clean.Before / after
https://claude.ai/code/session_018XXxUt46ifP93Dt4WnTkZo
Generated by Claude Code