fix: helpful error messages when arguments are missing#29
Merged
Conversation
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>
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.
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:
After:
The user gets: what's wrong, two worked examples to copy-paste, and the path to deeper docs — all without leaving the error.
Coverage
gitswitch add(0 args)gitswitch delete(0 args)gitswitch rename(≠2 args)gitswitch use(0 or 3+ args)Plus added
Example:fields to every command (init,list,doctor,why,add,delete,rename,use) so--helpitself surfaces a worked example block.Implementation
internal/cmd/argshelp.go— small helper: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
)