pflags

command
v2.0.49 Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2026 License: Apache-2.0, Apache-2.0 Imports: 2 Imported by: 0

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