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: chooseMinimumNArgs(n)to reject fewer than n arguments, orExactArgs(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 flag | Value / default | Applies to | Source |
|---|---|---|---|
Flags() + MarkFlagRequired("region") | Register local region first; mark it required; inspect the returned error | 1.10.2 | guide, marking API |
MarkPersistentFlagRequired(name) | Mark a persistent flag as required | 1.10.2 | guide, API |
MinimumNArgs(n) | Return an error if there are not at least n arguments | 1.10.2 | validators |
ExactArgs(n) | Return an error if there are not exactly n arguments | 1.10.2 | validators |
DisableFlagParsing | Keep false for normal required-flag enforcement; true bypasses that validator | 1.10.2 | implementation |
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) | Cause | Fix | Source |
|---|---|---|---|
required flag(s) "%s" not set | Required flag was not explicitly supplied | Pass the declared flag; check local/persistent registration and avoid ignoring Execute() errors | tagged validator |
requires at least %d arg(s), only received %d | Positional validator expected an argument | Supply the positional value or change Args to match the CLI contract; setting an unrelated flag cannot satisfy it | tagged 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