Skip to content

fix(cmd): report expected arguments when positional count is wrong - #892

Merged
lizhengfeng101 merged 1 commit into
alibaba:mainfrom
chaojixinren:fix/friendly-arg-count-errors
Aug 13, 2026
Merged

lizhengfeng101 merged 1 commit into
alibaba:mainfrom
chaojixinren:fix/friendly-arg-count-errors

Conversation

@chaojixinren

@chaojixinren chaojixinren commented Aug 13, 2026 •

Copy link
Copy Markdown
Contributor

What

Cobra's default message for a wrong positional-argument count names neither the command nor what it expects, and the root command sets SilenceUsage, so no usage block follows it:

$ ocr config set provider
Error: accepts 2 arg(s), received 1

After:

$ ocr config set provider
Error: "ocr config set" requires exactly 2 argument(s) (<key> <value>)

Usage:
  ocr config set <key> <value> [flags]

Example:
  ocr config set llm.model claude-opus-4-6
  ocr config set provider anthropic

Run 'ocr config set --help' for more information.

How

exactArgs and minimumArgs are drop-in replacements for cobra.ExactArgs and cobra.MinimumNArgs that build the message from metadata the command already declares: the positional signature in Use, plus Example and ValidArgs where present.

The guidance therefore cannot drift from the command's own --help output, and no command carries a hand-written error string. This mirrors how the existing flagErrorWithSuggestion handles the analogous flag-error case.

The supplied count is deliberately not echoed back: it adds nothing the user cannot already see in the command they just typed.

Seven commands are wired: config set, config unset, rules check, session show, session comments, delegate rule, and completion.

Aliases report the canonical path (ocr d rule → "ocr delegate rule"), matching what --help prints.

Scope

Message text only. Exit codes are unchanged, and every previously accepted argument count is still accepted.

I diffed the built binary against main across 29 invocations: 14 differ in text, 0 differ in exit code, and 15 are byte-identical. The byte-identical cases cover all NoArgs commands, unknown subcommands, OnlyValidArgs rejections, unknown flags, and all success paths.

Two adjacent gaps are deliberately left out to keep this reviewable and are worth follow-up issues:

  1. The OnlyValidArgs error (ocr completion xyz) does not list valid values despite the command declaring them.
  2. The 15 NoArgs commands hit the same dead end under SilenceUsage.

Both require a different mechanism than a count validator.

Tests

TestEveryPositionalCommandUsesFriendlyErrors walks the command tree and fails if a command declaring positional placeholders still reports Cobra's raw message, so wiring a future command to cobra.ExactArgs directly is caught.

I validated this guard destructively: reverting rules check to cobra.ExactArgs(1) makes it fail as intended.

Verified locally against every CI gate: license headers, action pins, gofmt -s, line endings, go mod tidy, go vet, tests (23 packages, 0 failures), coverage at 90.3% against the 90% threshold, build, and the smoke test's 10 assertions.

arg_errors.go is at 100% per-function coverage.

I did not run govulncheck because there are no dependency changes and go.mod is untouched, or the cross-compile matrix because this is pure Go with no build tags or cgo.

Fixes #890

Cobra's default message for a wrong positional-argument count ("accepts
2 arg(s), received 1") names neither the command nor what it expects, and
the root command sets SilenceUsage, so no usage block follows it. Running
"ocr config set provider" gave the user nothing to act on.

Add exactArgs and minimumArgs, drop-in replacements for cobra.ExactArgs
and cobra.MinimumNArgs that build the message from metadata the command
already declares: the positional signature in Use, plus Example and
ValidArgs where present. The guidance therefore cannot drift from the
command's own help output, and no command carries a hand-written error
string. This mirrors how flagErrorWithSuggestion handles the analogous
flag-error case.

Wire the seven commands that take positional arguments: config set,
config unset, rules check, session show, session comments, delegate rule
and completion. Exit codes are unchanged; only the message text differs.
The supplied count is deliberately not echoed back, since it adds nothing
the user cannot see in the line they just typed.

A tree walk over the command tree fails if a command declaring positional
placeholders still reports the raw count message, so wiring a new command
to cobra.ExactArgs directly is caught by tests.

Fixes alibaba#890
@github-actions

Copy link
Copy Markdown
Contributor

✅ OpenCodeReview: Review complete: 0 finding(s) across 6 selected item(s).

@lizhengfeng101 lizhengfeng101 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

@lizhengfeng101
lizhengfeng101 merged commit 5fafe82 into alibaba:main Aug 13, 2026
12 checks passed
@chaojixinren
chaojixinren deleted the fix/friendly-arg-count-errors branch August 13, 2026 13:35
kall pushed a commit to kall/open-code-review that referenced this pull request Aug 14, 2026
Three breakage classes from the merge, in one unit because the build does
not compile until all three are fixed:

- Rewrite the old module path to github.com/alibaba/open-code-review in
  core_cmd.go, core_diff.go and core_diff_test.go (alibaba#526).
- Replace the last stale helper call with the model.Diff method the branch
  extracted it into (internal/agent/agent.go).
- Re-register the core command group as cobra.Command trees after main
  deleted cmd/opencodereview/flags.go in the Cobra migration (alibaba#625). The
  newOcrFlagSet-based parsing in five run helpers is gone; flags now bind
  through Flags() and the helpers take explicit parameters.

Conventions carried over from the surrounding commands: the parent rejects
unknown subcommands and prints help when bare (alibaba#660, alibaba#694); rule and prompt
validate positional count through exactArgs so the error carries the
command's own usage (alibaba#749, alibaba#892); repo and diff flags reuse the shared
helpers; prompt phases are declared in ValidArgs for completion (alibaba#625).

The manual dispatcher and its hand-written usage block are dropped -- Cobra
owns both now -- and core_cmd_test.go trades its argv-slice calls for tests
that exercise the wiring through rootCmd.
Githab-capibara added a commit to Githab-capibara/open-code-review that referenced this pull request Sep 17, 2026
…libaba#892)

Cobra's default message for a wrong positional-argument count ("accepts
2 arg(s), received 1") names neither the command nor what it expects, and
the root command sets SilenceUsage, so no usage block follows it. Running
"ocr config set provider" gave the user nothing to act on.

Add exactArgs and minimumArgs, drop-in replacements for cobra.ExactArgs
and cobra.MinimumNArgs that build the message from metadata the command
already declares: the positional signature in Use, plus Example and
ValidArgs where present. The guidance therefore cannot drift from the
command's own help output, and no command carries a hand-written error
string. This mirrors how flagErrorWithSuggestion handles the analogous
flag-error case.

Wire the seven commands that take positional arguments: config set,
config unset, rules check, session show, session comments, delegate rule
and completion. Exit codes are unchanged; only the message text differs.
The supplied count is deliberately not echoed back, since it adds nothing
the user cannot see in the line they just typed.

A tree walk over the command tree fails if a command declaring positional
placeholders still reports the raw count message, so wiring a new command
to cobra.ExactArgs directly is caught by tests.

Fixes alibaba#890
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(cli): argument-count errors don't name the missing argument or show usage

2 participants