Reject unknown flags instead of dropping them silently - #18
Open
EvilFreelancer wants to merge 1 commit into
Open
Reject unknown flags instead of dropping them silently#18EvilFreelancer wants to merge 1 commit into
EvilFreelancer wants to merge 1 commit into
Conversation
Dynamic API commands are parsed by hand, not by yargs, and every flag that the spec did not declare was either swallowed or attached as a JSON body field to a request that could not carry one. A typo like --expand instead of --$expand produced a 200 OK with missing data and exit code 0. Add a pure Layer 2 module command-args.ts that validates parsed flags against CliCommand.options and suggests the closest declared name, and call it from runApiCommand before the request is built. Body-capable operations whose spec declares no request body keep forwarding undeclared flags as body fields - that passthrough is the only way to reach undocumented payloads. Enable yargs .strict() so built-in commands reject unknown arguments too, and pin the yargs locale to English so those messages do not change with the environment. Fixes #17 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
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.
Fixes #17.
Problem
Dynamic API commands never go through yargs -
runApiCommandparsesargvby hand. Any flag the spec did not declare was silently discarded, or attached as a JSON body field to a request that cannot carry one.ocli widgets_id --id widget-1 --expand parts(instead of--$expand) sentGET /widgets/widget-1with no querystring, printed a normal-looking response and exited 0. As the reporter noted in a follow-up comment, this was not$-specific:--bogus valuebehaved the same way. Built-in commands had the same hole because yargs was not in strict mode.Fix
src/command-args.ts:findUnknownFlagsvalidates parsed flags againstCliCommand.options,formatUnknownFlagsErrorrenders the message, and the suggestion picks the closest declared name (normalized match first, then edit distance <= 2), so--expandpoints at--$expand;runApiCommandcalls it after parsing and before building the request, so nothing is sent when a flag is wrong;.strict()on the yargs tree coverscommands,profiles,use,onboard;.locale("en")so yargs' own messages do not switch language with the environment -Unknown argument: qeurywas coming out localized.One case stays permissive on purpose: when an operation accepts a body (
POST,PUT,PATCH,DELETE) and the spec declares no request body, undeclared flags are still forwarded as JSON body fields. That passthrough is the only way to call endpoints whose payload is undocumented, and it is covered by existing tests. Once the spec declares body properties orformDataparameters, those names become the full list of accepted flags.Behavior
Verified end-to-end against a local mock server reproducing the issue: before the change the wrong flag returned
"parts": nullwith exit 0, after it the request is never sent.Tests
tests/command-args.test.ts- 14 new unit tests (unknown flag detection,$-prefix and typo suggestions, free-form body exception, message formatting);tests/cli.test.ts- 6 new tests covering the reported scenario, the declared-body case, the correctly spelled--$expand, the retained body passthrough, and strict mode oncommands.Full suite: 139 passed, 11 suites.
npm run buildclean. README,skills/ocli-api/SKILL.md, andexamples/skill-ocli-api.mddocument the new behavior; the architecture rule is updated in both.claudeand.cursortrees.🤖 Generated with Claude Code