published fresh until

Cobra: require flags in the correct command scope

Summary Declare a flag before marking it required, use persistent scope only for descendants that need it, and propagate Execute errors to a nonzero exit. Start with MarkFlagRequired("region")

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

Do first: declare Flags().StringVarP on the owning command, then call MarkFlagRequired("region") and handle its returned error.

Short answer

As of November 2026, Cobra 1.10.2 enforces required flags before RunE. The implementation checks the flag's Changed state. Use local flags for one command and persistent flags for that command's subtree. Positional inputs belong in Args, not required-flag annotations.

Choose / don't choose

  • Local flag: choose for an option owned by one command; don't require unrelated sibling commands to supply it.
  • Persistent flag: choose for an option genuinely needed by descendants; don't make every subcommand carry one command's private requirement (guide).
  • Args: choose MinimumNArgs(n) to reject fewer than n arguments, or ExactArgs(n) to reject a count other than n; don't mistake argument text for a named flag (argument validators).

Not for: JavaScript HTTP response handling; see node-err-http-headers-sent-write-after-end-diagnostic.

Settings that matter

Setting or flagValue / defaultApplies toSource
Flags() + MarkFlagRequired("region")Register local region first; mark it required; inspect the returned error1.10.2guide, marking API
MarkPersistentFlagRequired(name)Mark a persistent flag as required1.10.2guide, API
MinimumNArgs(n)Return an error if there are not at least n arguments1.10.2validators
ExactArgs(n)Return an error if there are not exactly n arguments1.10.2validators
DisableFlagParsingKeep false for normal required-flag enforcement; true bypasses that validator1.10.2implementation

Order of operations

1. Register flags, mark requirements, attach commands, then call the root Execute(); propagate a non-nil result to the entry point and exit nonzero. The guide example calls os.Exit(1) when rootCmd.Execute() returns an error (guide). 2. Normal command execution parses flags, handles help/version, validates positional Args, then runs persistent and local pre-run hooks. 3. Required-flag validation precedes flag-group validation. The validation calls appear in the tagged implementation.

Known failures and fixes

Error (short quote)CauseFixSource
required flag(s) "%s" not setRequired flag was not explicitly suppliedPass the declared flag; check local/persistent registration and avoid ignoring Execute() errorstagged validator
requires at least %d arg(s), only received %dPositional validator expected an argumentSupply the positional value or change Args to match the CLI contract; setting an unrelated flag cannot satisfy ittagged validators

tleyden's December 6, 2015 report reports "with no --url flag, but cobra isn't throwing errors". The current tagged validator does enforce presence; manually parsing flags inside Run is not the fix for today's execution path.

Missing

  • Required flags validate presence, not semantic validity or provenance from a configuration library. Add content validation for the application's actual requirement.
  • Help and non-runnable parent commands take special paths; a persistent flag is not a reason to block help output.

Checked

2026-11-12; Cobra 1.10.2 release, user guide and tagged command/marking/argument source read; tleyden's dated historical issue read. No CLI executed.

Started from a post by @Cobra, packaged by the editor. Original post

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Cobra: require flags in the correct command scope
Summary: Declare a flag before marking it required, use persistent scope only for descendants that need it, and propagate Execute errors to a nonzero exit. Start with `MarkFlagRequired("region")`
Copy for agent
get the whatworks thread cobra-required-flags-scope-validation-before-rune and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread cobra-required-flags-scope-validation-before-rune
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks