샘플
github.com/spf13/pflag v1.0.6: Parse command line flags with shorthand and default values using FlagSet
검증된 샘플 — golang github.com/spf13/pflag v1.0.6: Parse command line flags with shorthand and default values using FlagSet. go 1.26 · linux alpine/x64 …
sha256:11618d8c713d6b6b8f7615d92d1cbe7ecfa82458b07151cb3b9ba6ce01fd4d22
이 네트워크가 제공하는 것은 하나입니다. 빌드되는 샘플. 샌드박스에서 돌리고 서명된 영수증을 보관합니다. 등급을 매기지 않고 무엇도 보증하지 않습니다 — 같은 코드가 당신 환경에서 빌드되는지는 측정한 적이 없습니다.
통과한 계약 영수증을 낸 서로 다른 서명 키의 수입니다. 하나면 작성자 혼자이고, 둘 이상이면 다른 사람도 빌드했다는 뜻입니다. 키는 스스로 만드는 것이고 뒤에 등록된 신원이 없으므로, 세는 것은 사람이 아니라 키입니다.
MIT-0
실행 증거
선언된 환경과 서명된 실행을 분리해 두었습니다. 이 샘플이 무엇을 어디서 실행했는지 그대로 볼 수 있습니다.
- 증거 기준
- 서명된 컨트랙트 통과
- 검증 영수증
- 1
- 빌드한 서명 키
- 1
선언된 환경
windows 11 x64 go
검증 실행 환경
| 환경 | 컨트랙트 | 단계 | 실행일 |
|---|---|---|---|
| go 1.26 · linux alpine/x64 · docker ed25519:2175b912ea1c23b1 | PASS | compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS CONTAINER_RUN · golang@1 |
2026-08-19 |
케이스
HOW- 목표
- Parse command line flags with shorthand and default values using FlagSet
- 심벌
-
- FlagSet.BoolVarP
- FlagSet.IntVarP
- FlagSet.Parse
- FlagSet.StringVarP
- 생성일
- 2026-08-18T17:46:57Z
컨트랙트
- FlagSet.StringVarP binds string flag with long and shorthand names and falls back to default value
- FlagSet.IntVarP binds integer flag with long and shorthand names and parses integer values correctly
- FlagSet.BoolVarP binds boolean flag with long and shorthand names and parses boolean values correctly
- FlagSet.Parse parses argument slice and sets bound variables without error on valid input
파일
- PROMPT.md
- csx.json
- go.mod
- go.sum
- main.go
- spec.json
- test/main.go
소스
Clean-room public code sample — generation instructions
Write a brand-new, minimal, self-contained code sample in this clean-room directory.
Do not copy, paraphrase, or reference any existing project source. Work only from this spec.
A csx.json manifest scaffold already exists. Do not recreate it from memory. Preserve its case.goal, packages and symbols; fill its empty case.contract with exact assertions and correct its environment, commands and verifierAdapter for the files you generate.
Goal: Parse command line flags with shorthand and default values using FlagSet
Kind: HOW
Use EXACTLY these public packages and versions:
- pkg:golang/github.com/spf13/pflag@v1.0.6
Demonstrate these symbols/APIs:
- FlagSet.BoolVarP
- FlagSet.IntVarP
- FlagSet.Parse
- FlagSet.StringVarP
Rules:
- One focused purpose; the smallest project that proves the goal.
- Include a contract test (test/contract.*) that runs OFFLINE and exits 0 exactly when the goal behavior works.
- Pin every dependency with a lockfile so resolution is reproducible.
- No secrets, credentials, or tokens. No real URLs (only example.com or localhost). No absolute paths.
- No personal names, emails, company names, or project identifiers of any kind.
- No binaries and no generated output (node_modules, dist, target, venv, .git, .env).
- Keep it under 200 files and 256KB packed.
{"case":{"caseId":"case:sha256:b269470dbe4cc35f16a9130d14f9a94d8efc01db5a09689cd48c09b59ac0cc75","contract":["FlagSet.StringVarP binds string flag with long and shorthand names and falls back to default value","FlagSet.IntVarP binds integer flag with long and shorthand names and parses integer values correctly","FlagSet.BoolVarP binds boolean flag with long and shorthand names and parses boolean values correctly","FlagSet.Parse parses argument slice and sets bound variables without error on valid input"],"goal":"Parse command line flags with shorthand and default values using FlagSet","kind":"HOW","packages":["pkg:golang/github.com/spf13/pflag@v1.0.6"],"schemaVersion":1,"symbols":["FlagSet.BoolVarP","FlagSet.IntVarP","FlagSet.Parse","FlagSet.StringVarP"]},"contractCommand":["go","run","./test"],"environment":{"arch":"x64","ecosystem":"golang","os":"windows","osVersionBucket":"11","packageManager":"go","schemaVersion":1},"license":"MIT-0","packages":["pkg:golang/github.com/spf13/pflag@v1.0.6"],"schemaVersion":1,"symbols":["FlagSet.BoolVarP","FlagSet.IntVarP","FlagSet.Parse","FlagSet.StringVarP"],"verifierAdapter":"golang@1"}
module sample
go 1.26.5
require github.com/spf13/pflag v1.0.6
github.com/spf13/pflag v1.0.6 h1:jFzHGLGAlb3ruxLB8MhbI6A8+AQX/2eW4qeyNZXNp2o=
github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
package main
import (
"fmt"
"os"
"github.com/spf13/pflag"
)
func main() {
var host string
var port int
var verbose bool
fs := pflag.NewFlagSet("server", pflag.ContinueOnError)
fs.StringVarP(&host, "host", "h", "localhost", "Server listen address")
fs.IntVarP(&port, "port", "p", 8080, "Server listen port")
fs.BoolVarP(&verbose, "verbose", "v", false, "Enable verbose logging")
args := os.Args[1:]
if err := fs.Parse(args); err != nil {
fmt.Fprintf(os.Stderr, "Error parsing flags: %v\n", err)
os.Exit(1)
}
fmt.Printf("Host: %s\n", host)
fmt.Printf("Port: %d\n", port)
fmt.Printf("Verbose: %t\n", verbose)
}
{
"schemaVersion": 1,
"goal": "Parse command line flags with shorthand and default values using FlagSet",
"kind": "HOW",
"packages": [
"pkg:golang/github.com/spf13/pflag@v1.0.6"
],
"symbols": [
"FlagSet.BoolVarP",
"FlagSet.IntVarP",
"FlagSet.Parse",
"FlagSet.StringVarP"
]
}
package main
import (
"fmt"
"os"
"github.com/spf13/pflag"
)
func main() {
// Case 1: Default values when no flags are supplied
{
var host string
var port int
var verbose bool
fs := pflag.NewFlagSet("test_default", pflag.ContinueOnError)
fs.StringVarP(&host, "host", "h", "localhost", "listen host")
fs.IntVarP(&port, "port", "p", 8080, "listen port")
fs.BoolVarP(&verbose, "verbose", "v", false, "verbose output")
if err := fs.Parse([]string{}); err != nil {
fmt.Fprintf(os.Stderr, "Contract failure: empty parse failed: %v\n", err)
os.Exit(1)
}
if host != "localhost" {
fmt.Fprintf(os.Stderr, "Contract failure: expected default host 'localhost', got '%s'\n", host)
os.Exit(1)
}
if port != 8080 {
fmt.Fprintf(os.Stderr, "Contract failure: expected default port 8080, got %d\n", port)
os.Exit(1)
}
if verbose != false {
fmt.Fprintf(os.Stderr, "Contract failure: expected default verbose false, got %t\n", verbose)
os.Exit(1)
}
}
// Case 2: Long flags parsing
{
var host string
var port int
var verbose bool
fs := pflag.NewFlagSet("test_long", pflag.ContinueOnError)
fs.StringVarP(&host, "host", "h", "localhost", "listen host")
fs.IntVarP(&port, "port", "p", 8080, "listen port")
fs.BoolVarP(&verbose, "verbose", "v", false, "verbose output")
args := []string{"--host", "127.0.0.1", "--port", "9000", "--verbose"}
if err := fs.Parse(args); err != nil {
fmt.Fprintf(os.Stderr, "Contract failure: long flags parse failed: %v\n", err)
os.Exit(1)
}
if host != "127.0.0.1" {
fmt.Fprintf(os.Stderr, "Contract failure: expected host '127.0.0.1', got '%s'\n", host)
os.Exit(1)
}
if port != 9000 {
fmt.Fprintf(os.Stderr, "Contract failure: expected port 9000, got %d\n", port)
os.Exit(1)
}
if verbose != true {
fmt.Fprintf(os.Stderr, "Contract failure: expected verbose true, got %t\n", verbose)
os.Exit(1)
}
}
// Case 3: Shorthand flags parsing
{
var host string
var port int
var verbose bool
fs := pflag.NewFlagSet("test_short", pflag.ContinueOnError)
fs.StringVarP(&host, "host", "h", "0.0.0.0", "listen host")
fs.IntVarP(&port, "port", "p", 3000, "listen port")
fs.BoolVarP(&verbose, "verbose", "v", true, "verbose output")
args := []string{"-h", "example.com", "-p", "443", "-v=false"}
if err := fs.Parse(args); err != nil {
fmt.Fprintf(os.Stderr, "Contract failure: shorthand flags parse failed: %v\n", err)
os.Exit(1)
}
if host != "example.com" {
fmt.Fprintf(os.Stderr, "Contract failure: expected host 'example.com', got '%s'\n", host)
os.Exit(1)
}
if port != 443 {
fmt.Fprintf(os.Stderr, "Contract failure: expected port 443, got %d\n", port)
os.Exit(1)
}
if verbose != false {
fmt.Fprintf(os.Stderr, "Contract failure: expected verbose false, got %t\n", verbose)
os.Exit(1)
}
}
// Case 4: Non-flag arguments handling
{
var host string
fs := pflag.NewFlagSet("test_args", pflag.ContinueOnError)
fs.StringVarP(&host, "host", "h", "localhost", "listen host")
args := []string{"--host", "node1", "extra1", "extra2"}
if err := fs.Parse(args); err != nil {
fmt.Fprintf(os.Stderr, "Contract failure: parse with extra args failed: %v\n", err)
os.Exit(1)
}
if host != "node1" {
fmt.Fprintf(os.Stderr, "Contract failure: expected host 'node1', got '%s'\n", host)
os.Exit(1)
}
remaining := fs.Args()
if len(remaining) != 2 || remaining[0] != "extra1" || remaining[1] != "extra2" {
fmt.Fprintf(os.Stderr, "Contract failure: unexpected remaining args %v\n", remaining)
os.Exit(1)
}
}
// Case 5: Error handling on unknown flag and invalid int syntax
{
var port int
fs := pflag.NewFlagSet("test_error", pflag.ContinueOnError)
fs.IntVarP(&port, "port", "p", 8080, "listen port")
if err := fs.Parse([]string{"--invalid-flag"}); err == nil {
fmt.Fprintf(os.Stderr, "Contract failure: expected error on unknown flag, got nil\n")
os.Exit(1)
}
if err := fs.Parse([]string{"--port", "not-a-number"}); err == nil {
fmt.Fprintf(os.Stderr, "Contract failure: expected error on non-integer port, got nil\n")
os.Exit(1)
}
}
fmt.Println("PASS: all pflag FlagSet contract assertions passed")
}
오리진 시더
익명