flags

package module
v1.7.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 1, 2026 License: MIT Imports: 11 Imported by: 30 modules, 94 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

flags

Build

Golang flags parser with zero dependency.

Usage

See simple.go for a basic usage.

Concept

flags gives a simple way to get flag's value from argument or environment variable.

Argument takes priority over environment variable. Argument and environment variable names are generated from configuration you pass.

The FlagSet name is used to prefix all environment variable, replacing - by _.

The prefix name is used then to specialize your flag name (e.g. if you have want to use the same flags twice, you can change the prefix)

The argument's name is in camelCase format. The environement variable name is in SNAKE_UPPER_CASE format.

Flags can take a default value, that can be overriden programatically, always in the case you reuse the same flags twice (see advanced.go example.)

An environment variable that cannot be parsed (e.g. abc for an int flag) is reported like an invalid argument would be, following the FlagSet error handling: with flag.ExitOnError a message is printed and the program exits with status 2, with flag.PanicOnError it panics, and with flag.ContinueOnError the message is printed and the default value is kept. The offending value is never printed.

Security

Be careful when using the arguments values, if someone list the processes on the system, they will appear in plain-text. Pass secrets by environment variables: it's less easily visible. Values read from environment variables are applied after the flag is registered, so they never appear as the default in the usage output.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bool added in v1.0.0

func Bool(fs *flag.FlagSet, prefix, docPrefix, name, shorthand, label, env string, value bool, overrides []Override) *bool

func BoolVar added in v1.4.0

func BoolVar(fs *flag.FlagSet, output *bool, prefix, docPrefix, name, shorthand, label, env string, value bool, overrides []Override)

func Duration added in v1.1.0

func Duration(fs *flag.FlagSet, prefix, docPrefix, name, shorthand, label, env string, value time.Duration, overrides []Override) *time.Duration

func DurationVar added in v1.4.0

func DurationVar(fs *flag.FlagSet, output *time.Duration, prefix, docPrefix, name, shorthand, label, env string, value time.Duration, overrides []Override)

func Float64 added in v1.0.0

func Float64(fs *flag.FlagSet, prefix, docPrefix, name, shorthand, label, env string, value float64, overrides []Override) *float64

func Float64Slice added in v1.5.0

func Float64Slice(fs *flag.FlagSet, prefix, docPrefix, name, shorthand, label, env, envSeparator string, values []float64, overrides []Override) *[]float64

func Float64SliceVar added in v1.5.0

func Float64SliceVar(fs *flag.FlagSet, output *[]float64, prefix, docPrefix, name, shorthand, label, env, envSeparator string, values []float64, overrides []Override)

func Float64Var added in v1.4.0

func Float64Var(fs *flag.FlagSet, output *float64, prefix, docPrefix, name, shorthand, label, env string, value float64, overrides []Override)

func Int added in v1.0.0

func Int(fs *flag.FlagSet, prefix, docPrefix, name, shorthand, label, env string, value int, overrides []Override) *int

func Int64 added in v1.0.0

func Int64(fs *flag.FlagSet, prefix, docPrefix, name, shorthand, label, env string, value int64, overrides []Override) *int64

func Int64Var added in v1.4.0

func Int64Var(fs *flag.FlagSet, output *int64, prefix, docPrefix, name, shorthand, label, env string, value int64, overrides []Override)

func IntVar added in v1.4.0

func IntVar(fs *flag.FlagSet, output *int, prefix, docPrefix, name, shorthand, label, env string, value int, overrides []Override)

func String added in v1.0.0

func String(fs *flag.FlagSet, prefix, docPrefix, name, shorthand, label, env, value string, overrides []Override) *string

func StringSlice added in v1.3.0

func StringSlice(fs *flag.FlagSet, prefix, docPrefix, name, shorthand, label, env, envSeparator string, values []string, overrides []Override) *[]string

func StringSliceVar added in v1.4.0

func StringSliceVar(fs *flag.FlagSet, output *[]string, prefix, docPrefix, name, shorthand, label, env, envSeparator string, values []string, overrides []Override)

func StringVar added in v1.4.0

func StringVar(fs *flag.FlagSet, output *string, prefix, docPrefix, name, shorthand, label, env, value string, overrides []Override)

func Uint added in v1.0.0

func Uint(fs *flag.FlagSet, prefix, docPrefix, name, shorthand, label, env string, value uint, overrides []Override) *uint

func Uint64 added in v1.0.0

func Uint64(fs *flag.FlagSet, prefix, docPrefix, name, shorthand, label, env string, value uint64, overrides []Override) *uint64

func Uint64Var added in v1.4.0

func Uint64Var(fs *flag.FlagSet, output *uint64, prefix, docPrefix, name, shorthand, label, env string, value uint64, overrides []Override)

func UintVar added in v1.4.0

func UintVar(fs *flag.FlagSet, output *uint, prefix, docPrefix, name, shorthand, label, env string, value uint, overrides []Override)

func Usage added in v1.3.1

func Usage(fs *flag.FlagSet) func()

Types

type Builder added in v1.3.1

type Builder struct {
	// contains filtered or unexported fields
}

func New

func New(name, label string) Builder

func (Builder) DocPrefix added in v1.3.1

func (b Builder) DocPrefix(docPrefix string) Builder

func (Builder) Env added in v1.3.1

func (b Builder) Env(env string) Builder

func (Builder) EnvSeparator added in v1.3.1

func (b Builder) EnvSeparator(envSeparator string) Builder

func (Builder) Flag added in v1.7.0

func (b Builder) Flag[T flagType](fs *flag.FlagSet, value T, overrides []Override) *T

Flag declares a flag of the type of value and returns its pointer. An untyped constant gives int or float64: use an explicit conversion (uint(8080)) or an instantiation (Flag[uint]) for other types.

func (Builder) FlagVar added in v1.7.0

func (b Builder) FlagVar[T flagType](fs *flag.FlagSet, output *T, value T, overrides []Override)

FlagVar declares a flag of the type of value and binds it to output. See Flag for the typing of constants.

func (Builder) Prefix added in v1.3.1

func (b Builder) Prefix(prefix string) Builder

func (Builder) Shorthand added in v1.3.1

func (b Builder) Shorthand(shorthand string) Builder

type Override

type Override struct {
	// contains filtered or unexported fields
}

func NewOverride

func NewOverride(name string, value any) Override

NewOverride replaces the default of the flags named name, ignoring case and any prefix. The value must have the Go type of the flag.

Directories

Path Synopsis
cmd
advanced command
simple command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL