Skills Agentes

Golang Spf13 Cobra

Librería de árbol de comandos CLI en Golang con spf13/cobra: cobra.Command, RunE vs Run, PersistentPreRunE, validadores Args, flags persistentes/locales, completions y testing con SetArgs/SetOut/SetErr.

Solicitaread edit write glob grep bash(go:*) bash(golangci-lint:*) bash(git:*) agent webfetch mcp__context7__resolve-library-id mcp__context7__query-docs bash(godig:*) bash(gopls:*) lsp mcp__gopls__*
Estrellas
3.3k

en todo el repo

Actividad
58

0–100, la ruta de este skill

Actualizado
hace 2 meses

último commit aquí

Commits
5

últimos 90 días

Contexto
2.6k tok

181 tok en reposo

Paquete
7 archivos

48 KB

Instalar

Funciona con cualquier agente que lea SKILL.md

npx -y skills add samber/cc-skills-golang --skill golang-spf13-cobra --agent claude-code

Se instala solo en este repositorio.

Qué hace

  • Guía el diseño de árboles de comandos con spf13/cobra: comandos, flags, validadores de args y completions
  • Establece reglas sobre RunE vs Run, la cadena PersistentPreRunE/PreRunE/RunE/PostRunE/PersistentPostRunE
  • Define buenas prácticas de testing con SetArgs/SetOut/SetErr y OutOrStdout()/ErrOrStderr()

Úsalo cuando

  • Se está usando o adoptando spf13/cobra
  • El código importa github.com/spf13/cobra
  • Se construye, extiende o audita una CLI basada en cobra

No lo uses cuando

    Qué lo activa

    Di cualquiera de estas frases y el agente debería cargar este skill.

    • “Ayúdame a estructurar los comandos de mi CLI con cobra”
    • “Revisa si estoy usando bien PersistentPreRunE en mi CLI de Go”
    • “Cómo agrego autocompletado dinámico a un comando cobra”
    • “Escribe tests para mis comandos cobra usando SetArgs”

    SKILL.md

    En inglés

    Persona: You are a Go CLI engineer building command trees that feel native to the Unix shell. You design the user-facing surface first, then wire behavior into the right hook.

    Modes:

    • Build — creating a new CLI from scratch: follow command tree setup, hook wiring, and flag sections sequentially.
    • Extend — adding subcommands, flags, or completions to an existing CLI: read the current command tree first, then apply changes consistent with the existing structure.
    • Review — auditing an existing CLI: check the Common Mistakes table, verify RunE usage, OutOrStdout(), hook chain ordering, and args validation.

    Using spf13/cobra for CLI command trees in Go

    Cobra is the de facto standard for Go CLI applications. It provides the command/subcommand tree, flag parsing (via pflag), args validation, shell completion generation, and documentation generation. It does not handle configuration layering — that's viper's job.

    Official Resources:

    This skill is not exhaustive. Please refer to library documentation and code examples for more information. For Go package docs, symbols, versions, importers, and known vulnerabilities, → See samber/cc-skills-golang@golang-pkg-go-dev skill (godig) — prefer it over Context7 for Go package facts. To navigate this library's usage in your own code (definitions, call sites, diagnostics), → See samber/cc-skills-golang@golang-gopls skill (gopls). Context7 remains a fallback for docs not indexed on pkg.go.dev.

    go get github.com/spf13/cobra@latest
    

    Cobra vs. viper

    These libraries do fundamentally different things and can be used independently.

    Concern cobra viper
    Owns Command tree, flags, arg validation, completions Configuration value resolution
    User-facing? Yes — subcommands, flags, help text No — purely a key-value resolver
    Without the other? Yes — a CLI with flags only needs cobra Yes — a daemon reading YAML + env needs only viper
    Integration seam Hands pflag.Flag to viper via BindPFlag Treats the cobra flag as the highest-precedence layer

    Use cobra alone when your binary takes flags and args but needs no config file or env resolution. Use viper alone when you have a long-running service reading config from YAML + env with no CLI subcommands. Use both when you need both — bind at PersistentPreRunE on the root command.

    → See samber/cc-skills-golang@golang-spf13-viper for the viper side of this integration.

    Command tree

    Every cobra CLI has a root command plus zero or more subcommands registered with AddCommand. The root command name is the binary name.

    var rootCmd = &cobra.Command{
        Use:          "myapp",
        Short:        "One-line summary",
        SilenceUsage: true,  // ✓ prevents usage wall on every error
        SilenceErrors: true, // ✓ lets you control error output format
    }
    

    Use AddGroup to label subcommands in help output — register groups before the AddCommand calls that reference them; cobra does not retroactively assign groups.

    The Run* family

    Cobra commands have five run hooks executed in order:

    PersistentPreRunE → PreRunE → RunE → PostRunE → PersistentPostRunE
    

    Always use *E variants — the non-E forms cannot return errors. Key rules:

    • PersistentPreRunE on the root runs before every subcommand — use it for config init and auth checks.
    • A child PersistentPreRunE replaces the parent's entirely — call the parent explicitly if you need both.
    • PostRunE runs only if RunE succeeded.

    For the full lifecycle and inheritance rules, see commands-and-args.md.

    Args validators

    Cobra validates positional arguments before RunE runs. Never write len(args) checks inside RunE — that bypasses cobra's standard error messages and arg count tracking.

    Built-ins: NoArgs, ExactArgs(n), MinimumNArgs(n), MaximumNArgs(n), RangeArgs(min,max), OnlyValidArgs, ExactValidArgs(n). Compose with MatchAll(v1, v2). Custom validator: func(cmd *cobra.Command, args []string) error.

    For the full validator set with examples and MatchAll patterns, see commands-and-args.md.

    Flags primer

    Cobra delegates flag parsing to pflag. Persistent flags (PersistentFlags()) are inherited by all subcommands; local flags (Flags()) apply only to the declaring command.

    rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file path") // inherited by all subcommands
    serveCmd.Flags().IntVar(&port, "port", 8080, "listen port")                     // local to serveCmd only
    serveCmd.MarkFlagRequired("port")
    serveCmd.MarkFlagsMutuallyExclusive("json", "yaml")
    

    For pflag types, custom flag values, flag groups, and viper binding, see flags.md.

    Completions primer

    Cobra generates shell completions automatically. Extend them with:

    • ValidArgs []string — static positional arg completion.
    • ValidArgsFunction — dynamic: func(cmd, args, toComplete string) ([]string, ShellCompDirective). Return ShellCompDirectiveNoFileComp to suppress file fallback.
    • RegisterFlagCompletionFunc(name, fn) — flag value completion.

    For ShellCompDirective values, annotations, and testing, see completions.md.

    Testing commands

    Test commands by executing them programmatically. Never use os.Stdout / os.Stderr directly in command handlers — use cmd.OutOrStdout() / cmd.ErrOrStderr() so tests can redirect output.

    func TestServeCmd(t *testing.T) {
        buf := new(bytes.Buffer)
        rootCmd.SetOut(buf)
        rootCmd.SetArgs([]string{"serve", "--port", "9090"})
        require.NoError(t, rootCmd.Execute())
        assert.Contains(t, buf.String(), "listening on :9090")
    }
    

    Cobra accumulates flag state across Execute() calls — build a fresh command tree per test. For isolation patterns, golden files, and testing completions, see testing.md.

    Best Practices

    1. Always use RunE, never Run — Run cannot return an error; the only escape is os.Exit or panic, bypassing defers.
    2. Put config initialization in PersistentPreRunE — it runs before every subcommand; the right place for viper binding and auth checks.
    3. Validate positional args with Args, not inside RunE — Args gives cobra's standard error messages; MatchAll composes validators.
    4. Use cmd.OutOrStdout() / cmd.ErrOrStderr() for all output — direct os.Stdout writes cannot be captured by tests.
    5. Re-create the command tree per test — cobra accumulates flag state across Execute() calls on the same instance.

    Common Mistakes

    Mistake Why it fails Fix
    Using Run instead of RunE Cannot return an error — only escape is os.Exit or panic, bypassing defers Use RunE — return the error, let cobra handle the exit
    Writing len(args) checks in RunE Bypasses cobra's standard error messages ("accepts 1 arg, received 2") Declare Args: cobra.ExactArgs(1) on the command
    Writing to os.Stdout directly Tests cannot capture output — os-level file handles can't be redirected Use cmd.OutOrStdout() / cmd.ErrOrStderr()
    Child PersistentPreRunE silently drops parent's Cobra does not chain — the child replaces the parent's hook entirely Call parent.PersistentPreRunE(cmd, args) from the child's hook
    Reusing a root command across tests Cobra accumulates flag state; second Execute() sees flags from the first Build a fresh command tree per test

    Further Reading

    • commands-and-args.md — full PreRun*/PostRun* chain, every Args validator, PersistentPreRunE inheritance rules
    • flags.md — pflag types, required/exclusive/oneRequired groups, custom value types, viper binding
    • completions.md — ShellCompDirective set, annotation-based completions, testing completions
    • generators.md — man page, markdown, YAML, RST doc generation; cobra-cli scaffolder
    • testing.md — isolation patterns, golden files, testing completions, table-driven command tests

    Cross-References

    • → See samber/cc-skills-golang@golang-cli skill for general CLI architecture — project layout, exit codes, signal handling, I/O patterns
    • → See samber/cc-skills-golang@golang-spf13-viper skill for configuration layering alongside cobra (flag → env → file → default precedence)
    • → See samber/cc-skills-golang@golang-testing skill for general Go testing patterns

    If you encounter a bug or unexpected behavior in spf13/cobra, open an issue at https://github.com/spf13/cobra/issues.

    Reproducido de samber/cc-skills-golang bajo licencia MIT. Leer esta página en markdown.

    Archivos

    7 archivos en el paquete. Solo se lee SKILL.md al activarse — las referencias se cargan si el skill decide que las necesita.

    Antes de instalar

    Requiere el binario `go` y el paquete github.com/spf13/cobra en el proyecto.

    Detalles

    Creador
    samber
    Licencia
    MIT
    Recursos incluidos
    referencias
    Código fuente
    Ver SKILL.md

    Etiquetas

    Más de samber/cc-skills-golang

    Este repo incluye 46 skills. Si instalas uno, normalmente ya tienes los demás. Ver el pack cc-skills-golang entero y su comando de instalación

    Buenas prácticas de linting y configuración de golangci-lint para proyectos Golang: ejecutar linters, configurar .golangci.yml, suprimir avisos con nolint, interpretar salidas y elegir linters.

    Costo de contexto al activarse
    1.8k tok
    Tamaño del paquete
    5 archivos
    Última actualización
    el mes pasado
    herramientas desarrollo

    Benchmarking, profiling y medición de rendimiento en Golang: escribir y comparar benchmarks, perfilar con pprof, analizar con benchstat y detectar regresiones en CI.

    Costo de contexto al activarse
    3.3k tok
    Tamaño del paquete
    10 archivos
    Última actualización
    hace 2 meses
    testing qa

    Orquestador de skills de Golang, siempre activo en cualquier tarea de código, revisión, debug o setup: carga las skills más relevantes de samber/cc-skills-golang, a menudo varias a la vez.

    Costo de contexto al activarse
    3.8k tok
    Tamaño del paquete
    4 archivos
    Última actualización
    hace 2 meses
    herramientas desarrollo

    Patrones y metodología de optimización de rendimiento en Golang: si hay cuello de botella X, aplica el patrón Y, una vez que profiling o benchmarks ya lo identificaron.

    Costo de contexto al activarse
    2.3k tok
    Tamaño del paquete
    9 archivos
    Última actualización
    hace 2 meses
    herramientas desarrollo

    Tests de Golang listos para producción: table-driven, suites y mocks con testify, tests paralelos, fuzzing, fixtures, detección de fugas de goroutines con goleak, snapshot testing, cobertura, tests de integración.

    Costo de contexto al activarse
    4.4k tok
    Tamaño del paquete
    6 archivos
    Última actualización
    hace 2 meses
    testing qa

    Inyección de dependencias en Golang con samber/do: contenedores de servicios, gestión de ciclo de vida, scopes, health checks, apagado ordenado y organización en módulos.

    Costo de contexto al activarse
    2.3k tok
    Tamaño del paquete
    4 archivos
    Última actualización
    hace 2 meses
    herramientas desarrollo

    Skills relacionados

    Desarrollo de aplicaciones CLI en Go: estructura de comandos, flags, configuración por capas, versión embebida, exit codes, señales, completions y testing con cobra, viper o urfave/cli.

    Costo de contexto al activarse
    2.6k tok
    Tamaño del paquete
    14 archivos
    Última actualización
    hace 4 meses
    herramientas desarrollo

    Convenciones de estilo en Golang: longitud y corte de líneas, declaración de variables, claridad del control de flujo y cuándo los comentarios ayudan u estorban.

    Costo de contexto al activarse
    2.5k tok
    Tamaño del paquete
    3 archivos
    Última actualización
    hace 2 meses
    herramientas desarrollo

    Patrones de concurrencia en Go: úsalo al escribir o revisar código concurrente con goroutines, channels, select, locks, sync primitives, errgroup, singleflight, worker pools o pipelines fan-out/fan-in.

    Costo de contexto al activarse
    2.3k tok
    Tamaño del paquete
    5 archivos
    Última actualización
    hace 2 meses
    herramientas desarrollo