or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

advanced-features.mdflag-access.mdflag-definition.mdflag-types.mdflagset-methods.mdgo-integration.mdindex.mdusage-help.md
tile.json

flag-types.mddocs/

Flag Types

This document provides a comprehensive reference for all type-specific flag definers in pflag. Each type follows the same four-variant pattern: Type, TypeP, TypeVar, and TypeVarP.

Import

import "github.com/spf13/pflag"

Pattern Overview

Each type has four function variants:

  • Type(name, value, usage) - Returns pointer to value
  • TypeP(name, shorthand, value, usage) - Returns pointer with shorthand
  • TypeVar(p, name, value, usage) - Binds to existing variable
  • TypeVarP(p, name, shorthand, value, usage) - Binds to existing variable with shorthand

Boolean Flags

Boolean flags can be set with --flag, --flag=true, or --flag=false. They accept values: 1, 0, t, f, true, false, TRUE, FALSE, True, False.

func Bool(name string, value bool, usage string) *bool
func BoolP(name, shorthand string, value bool, usage string) *bool
func BoolVar(p *bool, name string, value bool, usage string)
func BoolVarP(p *bool, name, shorthand string, value bool, usage string)

Usage:

verbose := pflag.BoolP("verbose", "v", false, "enable verbose output")
debug := pflag.Bool("debug", false, "enable debug mode")

var quiet bool
pflag.BoolVar(&quiet, "quiet", false, "suppress output")

Boolean Function Flags

Boolean function flags call a callback function each time the flag is encountered. Useful for implementing count-like behavior or custom logic.

func BoolFunc(name string, usage string, fn func(string) error)
func BoolFuncP(name, shorthand string, usage string, fn func(string) error)

Usage:

var verbosity int
pflag.BoolFuncP("verbose", "v", "increase verbosity", func(value string) error {
	verbosity++
	return nil
})

Function Flags

Function flags call a callback with the flag value each time --flag=value is parsed.

func Func(name string, usage string, fn func(string) error)
func FuncP(name, shorthand string, usage string, fn func(string) error)

Usage:

var values []string
pflag.Func("add", "add a value", func(value string) error {
	values = append(values, value)
	return nil
})

Integer Flags

Integer flags accept decimal (1234), octal (0664), hexadecimal (0x1234), and may be negative.

Int

func Int(name string, value int, usage string) *int
func IntP(name, shorthand string, value int, usage string) *int
func IntVar(p *int, name string, value int, usage string)
func IntVarP(p *int, name, shorthand string, value int, usage string)

Usage:

port := pflag.IntP("port", "p", 8080, "server port")
workers := pflag.Int("workers", 4, "number of workers")

Int8

func Int8(name string, value int8, usage string) *int8
func Int8P(name, shorthand string, value int8, usage string) *int8
func Int8Var(p *int8, name string, value int8, usage string)
func Int8VarP(p *int8, name, shorthand string, value int8, usage string)

Int16

func Int16(name string, value int16, usage string) *int16
func Int16P(name, shorthand string, value int16, usage string) *int16
func Int16Var(p *int16, name string, value int16, usage string)
func Int16VarP(p *int16, name, shorthand string, value int16, usage string)

Int32

func Int32(name string, value int32, usage string) *int32
func Int32P(name, shorthand string, value int32, usage string) *int32
func Int32Var(p *int32, name string, value int32, usage string)
func Int32VarP(p *int32, name, shorthand string, value int32, usage string)

Int64

func Int64(name string, value int64, usage string) *int64
func Int64P(name, shorthand string, value int64, usage string) *int64
func Int64Var(p *int64, name string, value int64, usage string)
func Int64VarP(p *int64, name, shorthand string, value int64, usage string)

Usage:

timestamp := pflag.Int64("timestamp", 0, "unix timestamp")

Unsigned Integer Flags

Unsigned integer flags work like integer flags but only accept non-negative values.

Uint

func Uint(name string, value uint, usage string) *uint
func UintP(name, shorthand string, value uint, usage string) *uint
func UintVar(p *uint, name string, value uint, usage string)
func UintVarP(p *uint, name, shorthand string, value uint, usage string)

Uint8

func Uint8(name string, value uint8, usage string) *uint8
func Uint8P(name, shorthand string, value uint8, usage string) *uint8
func Uint8Var(p *uint8, name string, value uint8, usage string)
func Uint8VarP(p *uint8, name, shorthand string, value uint8, usage string)

Uint16

func Uint16(name string, value uint16, usage string) *uint16
func Uint16P(name, shorthand string, value uint16, usage string) *uint16
func Uint16Var(p *uint16, name string, value uint16, usage string)
func Uint16VarP(p *uint16, name, shorthand string, value uint16, usage string)

Uint32

func Uint32(name string, value uint32, usage string) *uint32
func Uint32P(name, shorthand string, value uint32, usage string) *uint32
func Uint32Var(p *uint32, name string, value uint32, usage string)
func Uint32VarP(p *uint32, name, shorthand string, value uint32, usage string)

Uint64

func Uint64(name string, value uint64, usage string) *uint64
func Uint64P(name, shorthand string, value uint64, usage string) *uint64
func Uint64Var(p *uint64, name string, value uint64, usage string)
func Uint64VarP(p *uint64, name, shorthand string, value uint64, usage string)

Float Flags

Float flags parse floating-point numbers.

Float32

func Float32(name string, value float32, usage string) *float32
func Float32P(name, shorthand string, value float32, usage string) *float32
func Float32Var(p *float32, name string, value float32, usage string)
func Float32VarP(p *float32, name, shorthand string, value float32, usage string)

Usage:

threshold := pflag.Float32("threshold", 0.5, "confidence threshold")

Float64

func Float64(name string, value float64, usage string) *float64
func Float64P(name, shorthand string, value float64, usage string) *float64
func Float64Var(p *float64, name string, value float64, usage string)
func Float64VarP(p *float64, name, shorthand string, value float64, usage string)

Usage:

ratio := pflag.Float64("ratio", 1.618, "golden ratio")

String Flags

String flags parse string values.

func String(name string, value string, usage string) *string
func StringP(name, shorthand string, value string, usage string) *string
func StringVar(p *string, name string, value string, usage string)
func StringVarP(p *string, name, shorthand string, value string, usage string)

Usage:

name := pflag.StringP("name", "n", "default", "user name")
output := pflag.String("output", "out.txt", "output file")

Duration Flags

Duration flags parse time durations using Go's time.ParseDuration format (e.g., "1h30m", "100ms", "5s").

func Duration(name string, value time.Duration, usage string) *time.Duration
func DurationP(name, shorthand string, value time.Duration, usage string) *time.Duration
func DurationVar(p *time.Duration, name string, value time.Duration, usage string)
func DurationVarP(p *time.Duration, name, shorthand string, value time.Duration, usage string)

Usage:

import "time"

timeout := pflag.Duration("timeout", 30*time.Second, "request timeout")
interval := pflag.DurationP("interval", "i", 5*time.Minute, "polling interval")

Time Flags

Time flags parse time values using specified format strings.

func Time(name string, value time.Time, formats []string, usage string) *time.Time
func TimeP(name, shorthand string, value time.Time, formats []string, usage string) *time.Time
func TimeVar(p *time.Time, name string, value time.Time, formats []string, usage string)
func TimeVarP(p *time.Time, name, shorthand string, value time.Time, formats []string, usage string)

Usage:

import "time"

formats := []string{"2006-01-02", "2006/01/02", time.RFC3339}
startTime := pflag.Time("start", time.Now(), formats, "start time")

Network Type Flags

IP

IP flags parse IPv4 and IPv6 addresses.

func IP(name string, value net.IP, usage string) *net.IP
func IPP(name, shorthand string, value net.IP, usage string) *net.IP
func IPVar(p *net.IP, name string, value net.IP, usage string)
func IPVarP(p *net.IP, name, shorthand string, value net.IP, usage string)

Usage:

import "net"

addr := pflag.IP("address", net.IPv4(127, 0, 0, 1), "server address")
bind := pflag.IPP("bind", "b", nil, "bind address")

IPMask

IPMask flags parse IP network masks.

func IPMask(name string, value net.IPMask, usage string) *net.IPMask
func IPMaskP(name, shorthand string, value net.IPMask, usage string) *net.IPMask
func IPMaskVar(p *net.IPMask, name string, value net.IPMask, usage string)
func IPMaskVarP(p *net.IPMask, name, shorthand string, value net.IPMask, usage string)

Usage:

import "net"

mask := pflag.IPMask("mask", net.IPv4Mask(255, 255, 255, 0), "network mask")

IPNet

IPNet flags parse IP networks in CIDR notation.

func IPNet(name string, value net.IPNet, usage string) *net.IPNet
func IPNetP(name, shorthand string, value net.IPNet, usage string) *net.IPNet
func IPNetVar(p *net.IPNet, name string, value net.IPNet, usage string)
func IPNetVarP(p *net.IPNet, name, shorthand string, value net.IPNet, usage string)

Usage:

import "net"

_, defaultNet, _ := net.ParseCIDR("192.168.1.0/24")
network := pflag.IPNet("network", *defaultNet, "target network")

Binary Data Flags

BytesHex

BytesHex flags parse hexadecimal-encoded byte arrays.

func BytesHex(name string, value []byte, usage string) *[]byte
func BytesHexP(name, shorthand string, value []byte, usage string) *[]byte
func BytesHexVar(p *[]byte, name string, value []byte, usage string)
func BytesHexVarP(p *[]byte, name, shorthand string, value []byte, usage string)

Usage:

key := pflag.BytesHex("key", []byte{}, "encryption key in hex")
// Command line: --key=48656c6c6f

BytesBase64

BytesBase64 flags parse base64-encoded byte arrays.

func BytesBase64(name string, value []byte, usage string) *[]byte
func BytesBase64P(name, shorthand string, value []byte, usage string) *[]byte
func BytesBase64Var(p *[]byte, name string, value []byte, usage string)
func BytesBase64VarP(p *[]byte, name, shorthand string, value []byte, usage string)

Usage:

data := pflag.BytesBase64("data", []byte{}, "base64 encoded data")
// Command line: --data=SGVsbG8gV29ybGQ=

Count Flags

Count flags increment their value each time the flag appears on the command line. Useful for verbosity levels.

func Count(name string, usage string) *int
func CountP(name, shorthand string, usage string) *int
func CountVar(p *int, name string, usage string)
func CountVarP(p *int, name, shorthand string, usage string)

Usage:

verbosity := pflag.CountP("verbose", "v", "verbosity level")
pflag.Parse()

// Command line: -vvv  => *verbosity = 3
// Command line: -v -v => *verbosity = 2

Slice Type Flags

Slice flags allow specifying multiple values. Values can be specified multiple times or as comma-separated lists (except StringArray).

StringSlice

StringSlice parses comma-separated string lists and splits values.

func StringSlice(name string, value []string, usage string) *[]string
func StringSliceP(name, shorthand string, value []string, usage string) *[]string
func StringSliceVar(p *[]string, name string, value []string, usage string)
func StringSliceVarP(p *[]string, name, shorthand string, value []string, usage string)

Usage:

tags := pflag.StringSlice("tags", []string{}, "tags to apply")
// Command line: --tags=foo,bar,baz
// Command line: --tags=foo --tags=bar

StringArray

StringArray does NOT split on commas. Each flag occurrence adds one element.

func StringArray(name string, value []string, usage string) *[]string
func StringArrayP(name, shorthand string, value []string, usage string) *[]string
func StringArrayVar(p *[]string, name string, value []string, usage string)
func StringArrayVarP(p *[]string, name, shorthand string, value []string, usage string)

Usage:

args := pflag.StringArray("arg", []string{}, "arguments")
// Command line: --arg="foo,bar" --arg="baz"
// Result: []string{"foo,bar", "baz"}  (comma NOT split)

BoolSlice

func BoolSlice(name string, value []bool, usage string) *[]bool
func BoolSliceP(name, shorthand string, value []bool, usage string) *[]bool
func BoolSliceVar(p *[]bool, name string, value []bool, usage string)
func BoolSliceVarP(p *[]bool, name, shorthand string, value []bool, usage string)

IntSlice

func IntSlice(name string, value []int, usage string) *[]int
func IntSliceP(name, shorthand string, value []int, usage string) *[]int
func IntSliceVar(p *[]int, name string, value []int, usage string)
func IntSliceVarP(p *[]int, name, shorthand string, value []int, usage string)

Usage:

ports := pflag.IntSlice("ports", []int{}, "ports to listen on")
// Command line: --ports=8080,8081,8082

Int32Slice

func Int32Slice(name string, value []int32, usage string) *[]int32
func Int32SliceP(name, shorthand string, value []int32, usage string) *[]int32
func Int32SliceVar(p *[]int32, name string, value []int32, usage string)
func Int32SliceVarP(p *[]int32, name, shorthand string, value []int32, usage string)

Int64Slice

func Int64Slice(name string, value []int64, usage string) *[]int64
func Int64SliceP(name, shorthand string, value []int64, usage string) *[]int64
func Int64SliceVar(p *[]int64, name string, value []int64, usage string)
func Int64SliceVarP(p *[]int64, name, shorthand string, value []int64, usage string)

UintSlice

func UintSlice(name string, value []uint, usage string) *[]uint
func UintSliceP(name, shorthand string, value []uint, usage string) *[]uint
func UintSliceVar(p *[]uint, name string, value []uint, usage string)
func UintSliceVarP(p *[]uint, name, shorthand string, value []uint, usage string)

Float32Slice

func Float32Slice(name string, value []float32, usage string) *[]float32
func Float32SliceP(name, shorthand string, value []float32, usage string) *[]float32
func Float32SliceVar(p *[]float32, name string, value []float32, usage string)
func Float32SliceVarP(p *[]float32, name, shorthand string, value []float32, usage string)

Float64Slice

func Float64Slice(name string, value []float64, usage string) *[]float64
func Float64SliceP(name, shorthand string, value []float64, usage string) *[]float64
func Float64SliceVar(p *[]float64, name string, value []float64, usage string)
func Float64SliceVarP(p *[]float64, name, shorthand string, value []float64, usage string)

DurationSlice

func DurationSlice(name string, value []time.Duration, usage string) *[]time.Duration
func DurationSliceP(name, shorthand string, value []time.Duration, usage string) *[]time.Duration
func DurationSliceVar(p *[]time.Duration, name string, value []time.Duration, usage string)
func DurationSliceVarP(p *[]time.Duration, name, shorthand string, value []time.Duration, usage string)

Usage:

import "time"

timeouts := pflag.DurationSlice("timeout", []time.Duration{}, "timeouts")
// Command line: --timeout=1s,5s,10s

IPSlice

func IPSlice(name string, value []net.IP, usage string) *[]net.IP
func IPSliceP(name, shorthand string, value []net.IP, usage string) *[]net.IP
func IPSliceVar(p *[]net.IP, name string, value []net.IP, usage string)
func IPSliceVarP(p *[]net.IP, name, shorthand string, value []net.IP, usage string)

Usage:

import "net"

servers := pflag.IPSlice("server", []net.IP{}, "server IP addresses")
// Command line: --server=192.168.1.1,192.168.1.2

IPNetSlice

func IPNetSlice(name string, value []net.IPNet, usage string) *[]net.IPNet
func IPNetSliceP(name, shorthand string, value []net.IPNet, usage string) *[]net.IPNet
func IPNetSliceVar(p *[]net.IPNet, name string, value []net.IPNet, usage string)
func IPNetSliceVarP(p *[]net.IPNet, name, shorthand string, value []net.IPNet, usage string)

Usage:

import "net"

networks := pflag.IPNetSlice("network", []net.IPNet{}, "target networks")
// Command line: --network=192.168.1.0/24,10.0.0.0/8

Map Type Flags

Map flags parse key=value pairs.

StringToString

func StringToString(name string, value map[string]string, usage string) *map[string]string
func StringToStringP(name, shorthand string, value map[string]string, usage string) *map[string]string
func StringToStringVar(p *map[string]string, name string, value map[string]string, usage string)
func StringToStringVarP(p *map[string]string, name, shorthand string, value map[string]string, usage string)

Usage:

labels := pflag.StringToString("label", map[string]string{}, "labels")
// Command line: --label=env=prod,region=us-west
// Command line: --label=env=prod --label=region=us-west

StringToInt

func StringToInt(name string, value map[string]int, usage string) *map[string]int
func StringToIntP(name, shorthand string, value map[string]int, usage string) *map[string]int
func StringToIntVar(p *map[string]int, name string, value map[string]int, usage string)
func StringToIntVarP(p *map[string]int, name, shorthand string, value map[string]int, usage string)

Usage:

limits := pflag.StringToInt("limit", map[string]int{}, "resource limits")
// Command line: --limit=cpu=4,memory=8192

StringToInt64

func StringToInt64(name string, value map[string]int64, usage string) *map[string]int64
func StringToInt64P(name, shorthand string, value map[string]int64, usage string) *map[string]int64
func StringToInt64Var(p *map[string]int64, name string, value map[string]int64, usage string)
func StringToInt64VarP(p *map[string]int64, name, shorthand string, value map[string]int64, usage string)

Text Marshaler Flags

Text flags work with types implementing encoding.TextUnmarshaler and encoding.TextMarshaler.

func TextVar(p encoding.TextUnmarshaler, name string, value encoding.TextMarshaler, usage string)
func TextVarP(p encoding.TextUnmarshaler, name, shorthand string, value encoding.TextMarshaler, usage string)

Usage:

import (
	"encoding/json"
	"net/url"
)

var u url.URL
defaultURL, _ := url.Parse("http://localhost")
pflag.TextVar(&u, "url", defaultURL, "target URL")

var config json.RawMessage
pflag.TextVar(&config, "config", json.RawMessage("{}"), "JSON config")