Skip to content

fix(cli): argument-count errors don't name the missing argument or show usage #890

Closed

Description

@chaojixinren

OpenCodeReview Version

open-code-review v1.9.2 (5b37b5f) darwin/arm64 built at: 2026-08-12T06:41:47Z

Operating System

macOS (Apple Silicon)

Installation Method

npm (global)

LLM Provider

Anthropic (Claude)

Bug Description

Leaf commands that accept positional arguments currently fall back to Cobra's raw argument-count error when the number of arguments is incorrect.

For example, ocr config set requires both a key and a value. If the user forgets the value:

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

This message is technically correct, but not actionable. It does not tell the user:

  • which argument is missing,

  • what the expected command form is,

  • whether there are known valid values,

  • or how to discover the correct usage.

Because the root command uses SilenceUsage: true, usage is not printed automatically. The error also does not suggest --help, leaving the exit code as the only additional signal.

Inconsistent with existing CLI error handling

This falls below the usability standard already established for neighbouring CLI errors.

For example, misspelled flags are handled by flagErrorWithSuggestion in cmd/opencodereview/flag_suggest.go:

$ ocr review --forma json
Error: unknown flag: --forma

Did you mean this?
--format

Argument-count errors should provide a similarly actionable response.

The necessary information is already available at the point of failure:

  • every affected command declares its positional argument signature in Use;

  • ocr config set also provides an Example;

  • ocr completion declares:

ValidArgs: []string{"bash", "zsh", "fish", "powershell"}

So, for commands with known valid positional values, that information could also be surfaced instead of returning Cobra's generic message.

Affected commands

Command Use field Current error on wrong count
ocr config set set accepts 2 arg(s), received 1
ocr config unset unset accepts 1 arg(s), received 0
ocr rules check check [flags] accepts 1 arg(s), received 0
ocr session show show [flags] accepts 1 arg(s), received 0
ocr session comments comments [flags] accepts 1 arg(s), received 0
ocr delegate rule rule [flags] <path...> requires at least 1 arg(s), only received 0
ocr completion completion accepts 1 arg(s), received 0

Expected behavior

When a leaf command receives the wrong number of positional arguments, the error should explain the expected invocation rather than exposing Cobra's raw validator message.

For example:

$ ocr config set provider
Error: missing required argument <value>

Usage:
ocr config set <key> <value>

And where valid positional values are known:

$ ocr completion
Error: missing required argument <shell>

Valid values:
bash, zsh, fish, powershell

Usage:
ocr completion <shell>

The exact wording can vary, but the error should give users enough information to correct the command without having to guess or separately run --help.

This is the same class of CLI error-consistency issue as #641, where parent commands silently printed help and exited with status 0.

Steps to Reproduce

# 1. Install v1.9.2 (npm -g) or build from source
ocr version   # open-code-review v1.9.2 (5b37b5f) darwin/arm64

# 2. Omit the value of a two-argument command
ocr config set provider
# Error: accepts 2 arg(s), received 1

# 3. Omit the key
ocr config unset
# Error: accepts 1 arg(s), received 0

# 4. Omit the session id
ocr session comments
# Error: accepts 1 arg(s), received 0

# 5. Omit the shell name, whose valid values are declared in ValidArgs
ocr completion
# Error: accepts 1 arg(s), received 0

# 6. Compare with the flag error path, which is helpful
ocr review --forma json
# Error: unknown flag: --forma
#
# Did you mean this?
#     --format

Expected Behavior

Argument-count errors should identify the command, show the expected positional arguments, and point the user toward the correct invocation — in the same spirit as the existing flag suggestion behavior.

For example:

$ ocr config set provider
Error: "ocr config set" requires 2 arguments (<key> <value>), but received 1

Usage:
  ocr config set <key> <value>

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

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

Where the command defines a known set of valid positional values, those should be surfaced as well:

$ ocr completion
Error: "ocr completion" requires 1 argument (<shell>), but received 0

Valid values: bash, zsh, fish, powershell

Run 'ocr completion --help' for more information.

The exit code should remain 1, and valid invocations should be unaffected.

Suggested implementation

A natural implementation, consistent with the existing flag-error handling, would be a small helper in cmd/opencodereview that wraps cobra.ExactArgs and cobra.MinimumNArgs.

The helper can build the error from metadata the command already provides:

  • the positional-argument signature from Use;
  • Example, where present;
  • ValidArgs, where present.

That keeps the behavior centralized instead of introducing seven command-specific error strings, and ensures the message stays accurate as command definitions evolve.

Happy to send a PR if this direction looks right.

Logs / Error Output

$ ocr version
open-code-review v1.9.2 (5b37b5f) darwin/arm64
built at: 2026-08-12T06:41:47Z

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

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

$ ocr config set a b c
Error: accepts 2 arg(s), received 3

$ ocr config unset
Error: accepts 1 arg(s), received 0

$ ocr config unset a b
Error: accepts 1 arg(s), received 2

$ ocr rules check
Error: accepts 1 arg(s), received 0

$ ocr session show
Error: accepts 1 arg(s), received 0

$ ocr session comments
Error: accepts 1 arg(s), received 0

$ ocr delegate rule
Error: requires at least 1 arg(s), only received 0

$ ocr completion
Error: accepts 1 arg(s), received 0

Additional Context

Reproduced with the released v1.9.2 (5b37b5f) binary and confirmed with a source build of main at 27a820a. The behavior is identical in both, so this is not build-specific.

Relevant code:

  • cmd/opencodereview/config_cmd.go:48,59 — cobra.ExactArgs(2) / cobra.ExactArgs(1)
  • cmd/opencodereview/rules_cmd.go:33 — cobra.ExactArgs(1)
  • cmd/opencodereview/session_cmd.go:51,67 — cobra.ExactArgs(1)
  • cmd/opencodereview/delegate_cmd.go:73 — cobra.MinimumNArgs(1)
  • cmd/opencodereview/completion.go:17-18 — cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs)
  • cmd/opencodereview/flag_suggest.go — existing friendlier handling for the analogous flag-error case
  • cmd/opencodereview/root.go:20-21 — SilenceUsage / SilenceErrors, which is why Cobra does not print usage automatically
  • cmd/opencodereview/main.go:25 — errors are ultimately printed as a bare Error: %v

Related: #641.

Activity

  1. added
    bug
    on Aug 13, 2026
  2. added theissue type on Aug 13, 2026
  3. chaojixinren commented on Aug 13, 2026

    @chaojixinren
    ContributorAuthor

    I can take this one if the direction looks good.

  4. stay-foolish-forever commented on Aug 13, 2026

    @stay-foolish-forever
    Contributor

    @chaojixinren Thanks for the incredibly thorough reproduction and analysis — the direction looks great, looking forward to your PR!

  5. added a commit that references this issue on Aug 13, 2026
    5fafe82
  6. added a commit that references this issue on Sep 17, 2026
    8cad05c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions