Skip to content

fix: helpful error messages when arguments are missing#29

Merged
OfirHaim1 merged 1 commit into
mainfrom
fix/helpful-arg-errors
May 1, 2026
Merged

fix: helpful error messages when arguments are missing#29
OfirHaim1 merged 1 commit into
mainfrom
fix/helpful-arg-errors

Conversation

@OfirHaim1
Copy link
Copy Markdown
Member

Cobra's default arg-validation error is accepts 1 arg(s), received 0 — technically correct, tells the user nothing actionable, and forces a trip to --help.

Before / after on gitswitch add (no args)

Before:

$ gitswitch add
accepts 1 arg(s), received 0

After:

$ gitswitch add
gitswitch add requires an identity name

examples:
  gitswitch add work --email you@company.com
  gitswitch add personal              (interactive prompts for the rest)

run "gitswitch add --help" for the full reference.

The user gets: what's wrong, two worked examples to copy-paste, and the path to deeper docs — all without leaving the error.

Coverage

Command Before After
gitswitch add (0 args) one-liner cobra error usage + 2 examples
gitswitch delete (0 args) one-liner cobra error usage + 2 examples
gitswitch rename (≠2 args) one-liner cobra error usage + 2 examples
gitswitch use (0 or 3+ args) one-liner cobra error usage + 3 examples

Plus added Example: fields to every command (init, list, doctor, why, add, delete, rename, use) so --help itself surfaces a worked example block.

Implementation

internal/cmd/argshelp.go — small helper:

exactArgsHelp(n, usage, examples...) cobra.PositionalArgs
rangeArgsHelp(min, max, usage, examples...) cobra.PositionalArgs

Replaces cobra.ExactArgs(1) etc. at the call sites.

Release

fix: prefix → release-please will auto-bump to v1.1.1.

🤖 Generated with Claude Code
EOF
)

Cobra's default arg-validation error is "accepts 1 arg(s), received 0"
— technically correct, tells the user nothing they can act on, and
forces a trip to --help to recover. Maintainer pushback was direct:
"is need to be like superstar the user is not need to guess."

What's new:

  internal/cmd/argshelp.go    — exactArgsHelp(n, usage, examples...)
                                and rangeArgsHelp(min, max, ...)
                                wrap cobra.PositionalArgs with a
                                friendly error format:

                                  <usage>

                                  examples:
                                    <ex 1>
                                    <ex 2>

                                  run "<cmd> --help" for the full
                                  reference.

Applied to every command that takes args:
  add     — 1 arg
  delete  — 1 arg
  rename  — 2 args
  use     — 1-2 args (rangeArgsHelp)

Plus an `Example:` field on each command (init, list, doctor, why,
add, delete, rename, use) so `--help` itself surfaces a worked
example block. Cobra renders that under "Examples:" automatically.

The diff between the old and new behaviour, on `gitswitch add`:

  before:
    accepts 1 arg(s), received 0
    (exit 1)

  after:
    gitswitch add requires an identity name

    examples:
      gitswitch add work --email you@company.com
      gitswitch add personal              (interactive prompts)

    run "gitswitch add --help" for the full reference.
    (exit 1)

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@OfirHaim1 OfirHaim1 merged commit 1373c84 into main May 1, 2026
1 check passed
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.

2 participants