CodeSampleX

Ejemplo

github.com/spf13/cobra 1.10.2: Drive a spf13/cobra command tree from a test with SetArgs and SetOut, without a subprocess, and tell the failure modes of Execute apart

Muestra verificada para golang github.com/spf13/cobra 1.10.2: Drive a spf13/cobra command tree from a test with SetArgs and SetOut, without a subprocess, and…

sha256:40b43e1c51d66679bac8ef03d3e56f00b6f9bb2c8bc4f36791b2196260a8cce1

Esta red ofrece una sola cosa: una muestra que compila. La ejecutó en un sandbox y guardó el recibo firmado. No califica ni garantiza nada: si el mismo código compila donde estás no es algo que haya medido. Cuántas claves de firma distintas presentaron un recibo de contrato aprobado. Una es solo el autor; más de una significa que alguien más también lo compiló. Una clave se genera sola y no tiene identidad registrada detrás, así que cuenta claves, no personas. MIT-0

Evidencia de ejecución

El entorno declarado y las ejecuciones firmadas se muestran por separado, para que veas exactamente qué ejecutó esta muestra y dónde.

Base de evidencia
Contrato firmado aprobado
Recibos de verificación
2
Claves de firma que lo compilaron
2
Entorno declarado go 1.26 linux x64 go 1.26 go go

Entornos de las ejecuciones de verificación

Entorno Contrato Etapas Ejecución
go 1.26 · linux alpine/x64 · docker ed25519:a2ec939a4c60e243 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · golang@1
2026-08-14
go 1.26 · linux alpine/x64 · docker ed25519:d91480838ac982c9 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · golang@1
2026-08-18

Caso

HOW
Objetivo
Drive a spf13/cobra command tree from a test with SetArgs and SetOut, without a subprocess, and tell the failure modes of Execute apart
Paquetes
Símbolos
  • Command.SetArgs
  • Command.Execute
  • Command.SetOut
  • Command.RunE
  • Command.PersistentFlags
  • Command.MarkFlagRequired
  • Command.MarkPersistentFlagRequired
  • Command.InheritedFlags
  • Command.LocalFlags
  • Command.SilenceUsage
  • Command.SilenceErrors
  • cobra.ArbitraryArgs
  • cobra.NoArgs
Entorno
go 1.26
Creado
2026-08-14T12:07:26Z

Contrato

  1. run a subcommand in memory and assert its output lands in the buffer installed with SetOut instead of on stdout
  2. assert RunE's error comes back out of Execute wrapped as returned, so errors.Is still reaches the sentinel
  3. assert Execute prints the error line to SetErr and the usage block to SetOut, so reading one buffer shows half of what the user sees
  4. assert SetArgs takes the arguments without the program name, and that including it makes the program name an unknown command
  5. assert an unset SetArgs falls back to os.Args[1:], which is what makes a command tree fail on -test flags inside go test
  6. assert SetArgs(nil) is that same fallback rather than an empty command line, while an empty non-nil slice runs the root with no arguments
  7. assert cobra skips that fallback only when the program name is cobra.test
  8. assert a missing required flag returns exactly required flag(s) "name" not set
  9. assert a child's Flags() does not contain the parent's persistent flag until parsing merges it in
  10. assert a persistent parent flag is readable from the child's Flags() and shows up in InheritedFlags but not LocalFlags
  11. assert a parent flag declared with Flags() is unknown to the child from either position on the command line, while still working on the parent
  12. assert the child's help lists the inherited flag under Global Flags and never lists the parent's local flag
  13. assert MarkFlagRequired on the child rejects a parent's persistent flag with no such flag -config, while MarkPersistentFlagRequired on the owner makes it required for the child
  14. assert a required flag with a default value is still an error unless it is passed explicitly, because the check is on pflag's Changed
  15. assert two missing required flags come back as one message with both names joined inside a single pair of quotes, ordered by pflag's sorted walk and not by declaration
  16. assert an unknown subcommand is an error carrying a Did you mean this suggestion, printed as a one-line pointer with no usage block
  17. assert an unknown flag is a different error, printed with the failing subcommand's full usage and no pointer line
  18. assert cobra.ArbitraryArgs on the root turns the unknown-command check off, so the typo becomes a positional argument while real subcommands still route
  19. assert cobra.NoArgs on the root rejects the same typo with the same wording but no suggestion and the root's whole usage block, because that error comes from ValidateArgs inside the command rather than from Find
  20. assert SilenceUsage on either the root or the failing command drops the usage block without changing the returned error, including for a genuine usage error
  21. assert SilenceUsage set inside RunE also drops the usage block, since cobra reads the field after the command returns, but leaves it in place for a flag error that never reaches RunE
  22. assert SilenceErrors alone drops the error line and leaves a usage block with no explanation, and that both together make Execute print nothing while still returning the error
  23. assert --help returns nil from Execute and prints help to the out buffer, where clap reports help as an error
  24. assert two Execute calls on one tree share flag state, so the second run passes the required-flag check with the first run's value

Archivos

  • csx.json
  • go.mod
  • go.sum
  • src/cli.go
  • test/main.go

Descargar el artefacto de código fuente (tar.gz)

Seeder de origen

anonymous