CodeVetter · Repo Unpack · Extension points
spf13/cobra
Extension uses ordinary Go values, callbacks, and metadata rather than requiring a special constructor. Command composition defines new actions and hierarchies. Registration APIs customize completion, validation, templates, lifecycle behavior, and generated documentation. Compatibility APIs include both supported extension surfaces and explicitly disabled legacy methods.
- Applications instantiate Command directly and register children through AddCommand, which assigns parents, propagates normalization, and invalidates command sorting.
- Run/RunE and persistent/local pre/post callback fields provide action and lifecycle extension points; error-returning variants take precedence when both forms are defined.
- PositionalArgs is a function type; MatchAll builds a validator that runs supplied validators in order and returns the first error.
- RegisterFlagCompletionFunc keys callbacks by the resolved pflag.Flag pointer, rejects missing or already-registered flags, and complements command-level ValidArgsFunction.
- Flag annotations plug into required-flag, filename, directory, and legacy Bash custom completion behavior; portable Go callbacks are recommended over MarkFlagCustom.
- SetHelpFunc, SetUsageFunc, template setters, and AddTemplateFunc customize inherited help/usage behavior and template function lookup.
- CommandDisplayNameAnnotation adapts displayed identity for external plugins such as kubectl-myplugin presented as kubectl myplugin; DisplayName reads this metadata.
- Documentation custom APIs accept link handlers and file prependers; GenManTreeOptions adds output path and command separator, while GenManHeader supplies title, section, date, source, and manual.