pflags

command
v2.0.54 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2026 License: Apache-2.0, Apache-2.0 Imports: 2 Imported by: ? modules, 0 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

Pflags generator

Generate strongly typed CLI flags for the fields of a Go struct, including nested structs. With the config package, these flags can override configuration values.

Build and install

From the repository root, build the binary:

make -C flytestdlib compile

The binary is written to flytestdlib/bin/pflags. Add that directory to PATH before running go generate.

Alternatively, install it into GOBIN (or GOPATH/bin):

go install ./flytestdlib/cli/pflags

Usage

Run the generator in the destination package directory:

pflags MyStruct --package myproject/mypackage

The package defaults to the current directory. You can also add a directive to the file declaring the struct:

//go:generate pflags MyStruct

The target must be a struct. Supported field types include basic types, JSON-unmarshalable types, nested structs, and supported slices and maps.

The generator writes mystruct_flags.go and mystruct_flags_test.go. By default, it looks for a variable named defaultConfig to provide defaults. Use --default-var to select another variable, or --bind-default-var to bind flags to that variable's fields.

Field tags

Use the pflag tag to specify a default value and help text as a comma-separated pair. A matching default variable takes precedence over the tag's default value.

type MyStruct struct {
    StringValue string `json:"str" pflag:"\"hello world\",\"life is short\""`
    BoolValue   bool   `json:"bl" pflag:",This is a bool value that will default to false."`
}

Use pflag:"-" to exclude a field.

Documentation

Overview

Generates a Register method to automatically add pflags to a pflagSet for all fields in a given type.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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