Skip to content

fix(cli): suggest --help when unknown or unexpected CLI argument is passed (Closes #400)#405

Open
dagangtj wants to merge 2 commits into
OWASP:mainfrom
dagangtj:fix-400-help-hint
Open

fix(cli): suggest --help when unknown or unexpected CLI argument is passed (Closes #400)#405
dagangtj wants to merge 2 commits into
OWASP:mainfrom
dagangtj:fix-400-help-hint

Conversation

@dagangtj
Copy link
Copy Markdown

Summary

Fixes #400 — when a user passes an unknown flag or unexpected argument, the error now includes a pointer to --help.

Changes

File Change
src/cli/args.ts Appends --help hint to all Unknown option and Unexpected argument errors (6 sites across 3 CLI entry points)
src/index.ts Adds a guard so the top-level catch does not print the hint a second time when args.ts already included it
tests/args.test.ts New file with 6 unit tests covering all error paths

Acceptance Criteria

  • cve-lite --verbosse prints Error: Unknown option: --verbosse followed by Run cve-lite --help to see supported options.
  • cve-lite . extra-arg prints the same trailing hint
  • Existing tests pass (cli-integration.test.ts assertion already expects the hint and remains compatible)

Verification

Unit tests added for all three CLI entry points:

  • default scan command
  • advisories-sync subcommand
  • install-skill subcommand

Closes #400

…assed

When parseArgs encounters an unknown option or unexpected argument, the
error message previously only stated what went wrong without telling
the user they can run --help to see valid options.

This change:
- Appends 'Run cve-lite --help to see supported options.' to every
  Unknown option and Unexpected argument error thrown by args.ts
- Updates index.ts to avoid printing the hint a second time when
  the message already contains it
- Adds dedicated unit tests covering all three CLI entry points

Closes OWASP#400
…B unavailability

Issue OWASP#401: Flag incompatibility errors now explain what each flag does
and guide the user on which to remove:
- --fix vs --json: explains interactive fixes vs JSON output
- --offline/--offline-db vs --osv-url: explains local DB vs custom endpoint
- --no-cache vs --offline: explains cache behavior in offline mode
- --report vs --json: explains HTML report vs JSON output

Issue OWASP#402: Offline advisory database unavailable error now includes
the sync command hint:
- Without --offline-db: 'To build it, run: cve-lite advisories sync'
- With --offline-db: includes --output <path> in the hint

Closes OWASP#401
Closes OWASP#402
Copy link
Copy Markdown
Collaborator

@sonukapoor sonukapoor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please ignore this comment — feedback has been moved to inline comments above.

Comment thread src/cli/args.ts
if (arg.startsWith("--output=")) { options.output = arg.slice("--output=".length); continue; }
if (arg.startsWith("-")) throw new Error(`Unknown option: ${arg}`);
throw new Error(`Unexpected argument: ${arg}`);
if (arg.startsWith("-")) throw new Error(`Unknown option: ${arg}\nRun `cve-lite --help` to see supported options.`);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The backticks around cve-lite --help aren't escaped here, so the template literal terminates early — tsc --noEmit produces 12 errors across all six lines (19, 29, 30, 79, 81 too).

Also worth pulling the hint into a constant since it's repeated six times:

const HELP_HINT = `\\nRun \`cve-lite --help\` to see supported options.`;
// then: throw new Error(`Unknown option: ${arg}${HELP_HINT}`);

Comment thread src/index.ts
const message = error instanceof Error ? error.message : String(error);
console.error(chalk.red(`Error: ${message}`));
console.error(chalk.gray("Run `cve-lite --help` to see supported options."));
if (!message.includes("--help")) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works, but it ties the catch block to the text content of errors thrown in args.ts. If the wording ever changes the deduplication silently breaks. Cleaner to keep args.ts throwing plain messages and always append the hint here unconditionally.

Comment thread tests/args.test.ts
@@ -0,0 +1,38 @@
import { jest } from "@jest/globals";
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jest is imported but never used — safe to remove. Also, there's already a describe("parseArgs") block in helpers.test.ts — could you fold these in there to keep everything in one place?

Comment thread src/index.ts
@@ -121,11 +123,11 @@ if (parsedArgs) {
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The improved flag-conflict messages here don't have test coverage yet. The existing cli-integration.test.ts already mocks these option combinations, so it'd be a natural home for assertions on the new message text.

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.

fix: suggest --help when an unknown or unexpected CLI argument is passed

2 participants