Silence cobra usage/error dump on failure (#31)#52
Open
vinistoisr wants to merge 1 commit into
Open
Conversation
Cobra prints the full usage and flags block to stderr on any RunE or flag-parse error. That clutters logs and buries the real message, breaking the "data on stdout, errors on stderr" contract: a stray flag like `--version` produced a screenful of usage text. Set SilenceUsage and SilenceErrors on the root command and print a single-line "Error: <msg>" to stderr from Execute instead. stdout stays data-only; stderr carries one concise line. Adds a regression test asserting no usage/command block is emitted on a flag-parse error. Co-Authored-By: Claude Opus 4.8 <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 #31.
Problem
Cobra prints the full usage/flags block to stderr on any
RunEor flag-parse error. This clutters logs and buries the real message, breaking the "data on stdout, errors on stderr" contract. For example, a stray--versionproduced a full screen of usage text and exit code 1.Fix
SilenceUsage: trueandSilenceErrors: trueonrootCmd.Error: <msg>to stderr fromExecute()ourselves.stdout stays data-only; stderr carries one concise line.
Behavior after fix
Tests
Adds
TestRootCmd_NoUsageDumpOnError, asserting neitherUsage:norAvailable Commands:is emitted on a flag-parse error.go test ./...andgo vet ./...pass.🤖 Generated with Claude Code