CodeVetter · Repo Unpack · Data flow
spf13/cobra
Execution starts with an argument slice and a caller-built command graph. Resolution selects a stateful Command, pflag separates flags from positional arguments, and validators gate its action. Completion exchanges a textual protocol with a separate shell process. Documentation and completion-file APIs form the explicit filesystem output boundary.
- ExecuteC selects c.args when supplied; otherwise it reads os.Args[1:], except for the cobra.test executable-name guard.
- Find strips flags to identify successive subcommands while preserving remaining input for execution; with nil Args it applies legacyArgs, whose root/subcommand rules differ.
- ParseFlags merges local persistent and ancestor flag sets, transfers FParseErrWhitelist into pflag's allowlist, parses input, and emits buffered warnings only when parsing succeeds.
- The action receives pflag's remaining arguments, or the untouched argument slice under DisableFlagParsing; positional validation runs before pre-hooks, while required/group validation runs after them.
- Output, error, and input stream lookup walks parent commands before falling back to stdout, stderr, or stdin, allowing root-level stream injection to serve descendants.
- getCompletions extracts the incomplete final token, copies preceding tokens, resolves the target, propagates context, and parses flags before choosing completion results.
- Completion responses filter disabled Active Help, optionally remove tab-delimited descriptions, truncate embedded newlines, trim choices, and append :<directive>; diagnostics use the error stream.
- Markdown tree generation maps CommandPath spaces to underscores, joins the basename to a supplied directory, creates the file, writes a custom prefix, and streams rendered command metadata into it.