샘플
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
검증된 샘플 — 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 tell the…
sha256:40b43e1c51d66679bac8ef03d3e56f00b6f9bb2c8bc4f36791b2196260a8cce1
이 네트워크가 제공하는 것은 하나입니다. 빌드되는 샘플. 샌드박스에서 돌리고 서명된 영수증을 보관합니다. 등급을 매기지 않고 무엇도 보증하지 않습니다 — 같은 코드가 당신 환경에서 빌드되는지는 측정한 적이 없습니다.
통과한 계약 영수증을 낸 서로 다른 서명 키의 수입니다. 하나면 작성자 혼자이고, 둘 이상이면 다른 사람도 빌드했다는 뜻입니다. 키는 스스로 만드는 것이고 뒤에 등록된 신원이 없으므로, 세는 것은 사람이 아니라 키입니다.
MIT-0
실행 증거
선언된 환경과 서명된 실행을 분리해 두었습니다. 이 샘플이 무엇을 어디서 실행했는지 그대로 볼 수 있습니다.
- 증거 기준
- 서명된 컨트랙트 통과
- 검증 영수증
- 2
- 빌드한 서명 키
- 2
선언된 환경
go 1.26 linux x64 go 1.26 go go
검증 실행 환경
| 환경 | 컨트랙트 | 단계 | 실행일 |
|---|---|---|---|
| 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 |
케이스
HOW- 목표
- Drive a spf13/cobra command tree from a test with SetArgs and SetOut, without a subprocess, and tell the failure modes of Execute apart
- 심벌
-
- 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
- 환경
- go 1.26
- 생성일
- 2026-08-14T12:07:26Z
컨트랙트
- run a subcommand in memory and assert its output lands in the buffer installed with SetOut instead of on stdout
- assert RunE's error comes back out of Execute wrapped as returned, so errors.Is still reaches the sentinel
- 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
- assert SetArgs takes the arguments without the program name, and that including it makes the program name an unknown command
- assert an unset SetArgs falls back to os.Args[1:], which is what makes a command tree fail on -test flags inside go test
- 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
- assert cobra skips that fallback only when the program name is cobra.test
- assert a missing required flag returns exactly required flag(s) "name" not set
- assert a child's Flags() does not contain the parent's persistent flag until parsing merges it in
- assert a persistent parent flag is readable from the child's Flags() and shows up in InheritedFlags but not LocalFlags
- 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
- assert the child's help lists the inherited flag under Global Flags and never lists the parent's local flag
- 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
- 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
- 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
- assert an unknown subcommand is an error carrying a Did you mean this suggestion, printed as a one-line pointer with no usage block
- assert an unknown flag is a different error, printed with the failing subcommand's full usage and no pointer line
- assert cobra.ArbitraryArgs on the root turns the unknown-command check off, so the typo becomes a positional argument while real subcommands still route
- 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
- 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
- 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
- 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
- assert --help returns nil from Execute and prints help to the out buffer, where clap reports help as an error
- 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
파일
- csx.json
- go.mod
- go.sum
- src/cli.go
- test/main.go