# Behavior traces — spf13/cobra

> Canonical page: https://codevetter.com/unpack/cobra/behavior-traces

# Behavior traces — spf13/cobra

Scanned commit adbc8813901bba65827259daa8e22ff94ec1f30e (2026-07-10). Generated by codevetter codevetter 1.15.0 on 2026-10-02.

Source inspection; upstream code and tests were not executed.

Three concrete flows were followed through source: normal command execution, dynamic shell completion, and documentation persistence. Help and script generation provide additional branches through the same command tree. The traces describe source execution order rather than observed runtime results. Early returns and option-dependent branches materially change which callbacks run.
- Startup: Execute → ExecuteC supplies context, redirects to Root when necessary, invokes preExecHookFn, initializes help and completion commands, checks groups, then selects Find or Traverse. — [command.go#L1070-1084](https://github.com/spf13/cobra/blob/adbc8813901bba65827259daa8e22ff94ec1f30e/command.go#L1070-L1084) [command.go#L1085-1127](https://github.com/spf13/cobra/blob/adbc8813901bba65827259daa8e22ff94ec1f30e/command.go#L1085-L1127)
- Primary action: cmd.execute initializes help/version flags → ParseFlags → preRun → deferred postRun → ValidateArgs → persistent/local pre-hooks → required/group validation → RunE or Run → local/persistent post-hooks. — [command.go#L905-926](https://github.com/spf13/cobra/blob/adbc8813901bba65827259daa8e22ff94ec1f30e/command.go#L905-L926) [command.go#L955-970](https://github.com/spf13/cobra/blob/adbc8813901bba65827259daa8e22ff94ec1f30e/command.go#L955-L970) [command.go#L984-1042](https://github.com/spf13/cobra/blob/adbc8813901bba65827259daa8e22ff94ec1f30e/command.go#L984-L1042)
- Execution exits early for parsing errors, help, version, or a non-runnable command; after execute returns ErrHelp, ExecuteC calls HelpFunc and returns nil error. — [command.go#L920-956](https://github.com/spf13/cobra/blob/adbc8813901bba65827259daa8e22ff94ec1f30e/command.go#L920-L956) [command.go#L1142-1154](https://github.com/spf13/cobra/blob/adbc8813901bba65827259daa8e22ff94ec1f30e/command.go#L1142-L1154)
- Named help: the generated help command calls Root.Find → initializes the target's help/version flags → Help; an unknown topic prints a message and routes through Root.Usage and CheckErr. — [command.go#L1293-1310](https://github.com/spf13/cobra/blob/adbc8813901bba65827259daa8e22ff94ec1f30e/command.go#L1293-L1310) [cobra.go#L234-239](https://github.com/spf13/cobra/blob/adbc8813901bba65827259daa8e22ff94ec1f30e/cobra.go#L234-L239)
- Dynamic completion starts when a generated Bash V2 function evaluates a request to __complete or __completeNoDesc; initCompleteCmd creates the hidden, flag-parsing-disabled command only for an actual completion invocation. — [bash_completionsV2.go#L32-36](https://github.com/spf13/cobra/blob/adbc8813901bba65827259daa8e22ff94ec1f30e/bash_completionsV2.go#L32-L36) [bash_completionsV2.go#L54-84](https://github.com/spf13/cobra/blob/adbc8813901bba65827259daa8e22ff94ec1f30e/bash_completionsV2.go#L54-L84) [completions.go#L231-248](https://github.com/spf13/cobra/blob/adbc8813901bba65827259daa8e22ff94ec1f30e/completions.go#L231-L248) [completions.go#L299-307](https://github.com/spf13/cobra/blob/adbc8813901bba65827259daa8e22ff94ec1f30e/completions.go#L299-L307)
- Completion resolution runs getCompletions → Find/Traverse → checkIfFlagCompletion → ParseFlags → help/version suppression → annotation/static/callback selection; it does not call the target command's execute method. — [completions.go#L330-365](https://github.com/spf13/cobra/blob/adbc8813901bba65827259daa8e22ff94ec1f30e/completions.go#L330-L365) [completions.go#L371-402](https://github.com/spf13/cobra/blob/adbc8813901bba65827259daa8e22ff94ec1f30e/completions.go#L371-L402) [completions.go#L530-581](https://github.com/spf13/cobra/blob/adbc8813901bba65827259daa8e22ff94ec1f30e/completions.go#L530-L581) [completions.go#L405-427](https://github.com/spf13/cobra/blob/adbc8813901bba65827259daa8e22ff94ec1f30e/completions.go#L405-L427)
- Script generation: completion bash dispatches its RunE to Root.GenBashCompletionV2 → genBashCompletion → genBashComp, buffers the script, and writes it to the configured output. — [completions.go#L830-837](https://github.com/spf13/cobra/blob/adbc8813901bba65827259daa8e22ff94ec1f30e/completions.go#L830-L837) [bash_completionsV2.go#L24-36](https://github.com/spf13/cobra/blob/adbc8813901bba65827259daa8e22ff94ec1f30e/bash_completionsV2.go#L24-L36) [bash_completionsV2.go#L480-484](https://github.com/spf13/cobra/blob/adbc8813901bba65827259daa8e22ff94ec1f30e/bash_completionsV2.go#L480-L484)
- Persistence: GenMarkdownTree → GenMarkdownTreeCustom recursively processes available descendants → os.Create → filePrepender → GenMarkdownCustom → printOptions → buffer.WriteTo, returning on the first encountered error. — [doc/md_docs.go#L125-157](https://github.com/spf13/cobra/blob/adbc8813901bba65827259daa8e22ff94ec1f30e/doc/md_docs.go#L125-L157) [doc/md_docs.go#L57-80](https://github.com/spf13/cobra/blob/adbc8813901bba65827259daa8e22ff94ec1f30e/doc/md_docs.go#L57-L80) [doc/md_docs.go#L112-116](https://github.com/spf13/cobra/blob/adbc8813901bba65827259daa8e22ff94ec1f30e/doc/md_docs.go#L112-L116)

Every claim links to its cited files at the pinned commit. Citations identify source evidence; they do not establish runtime correctness. Independent CodeVetter scan — not reviewed by upstream maintainers.

## Public product links

- [CodeVetter](https://codevetter.com/)
- [Download](https://codevetter.com/download)
- [Documentation](https://codevetter.com/docs/)
- [Source](https://github.com/Codevetter/codevetter)
