Help us shape the future of Go! Take the 2026 Go Developer Survey

cliflag

package
v0.9.1 Latest Latest
Warning

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

Go to latest
Published: Aug 24, 2026 License: AGPL-3.0, AGPL-3.0-only Imports: 3 Imported by: 0

Documentation

Overview

Package cliflag provides self-describing CLI flag types. An EnumValue carries its own set of allowed values, so (1) the CLI validates input at parse time — an out-of-set value is rejected with a clear message instead of silently accepted — and (2) the docs generator reads the allowed set directly, rendering the real options instead of hoping the usage string mentions them. One source of truth for --help and the reference docs.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EnumVar

func EnumVar(fs *pflag.FlagSet, target *string, name string, allowed []string, def, usage string)

EnumVar registers an enum-constrained string flag on fs. The usage should be plain prose (no value list) — the allowed values are appended for --help and surfaced separately in the docs.

func OptionsSuffix

func OptionsSuffix(allowed []string) string

OptionsSuffix is the " (one of: …)" hint appended to an enum flag's usage so `--help` lists the allowed values inline. It is shared with the docs generator, which strips it back off (the reference renders the values in a dedicated Options column instead, so keeping it in the description too would be redundant). Empty for a non-enum.

Types

type EnumValue

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

EnumValue is a pflag.Value backed by a fixed set of allowed strings.

func NewEnum

func NewEnum(target *string, allowed []string, def string) *EnumValue

NewEnum stores def into target and returns an EnumValue constrained to allowed. An empty def (a common "fall back to config" sentinel) is permitted and left as-is.

func (*EnumValue) Allowed

func (e *EnumValue) Allowed() []string

Allowed returns the permitted values, for documentation.

func (*EnumValue) Set

func (e *EnumValue) Set(v string) error

Set validates v against the allowed set (empty is always accepted as "unset").

func (*EnumValue) String

func (e *EnumValue) String() string

func (*EnumValue) Type

func (e *EnumValue) Type() string

Type reports "string" so the value reads as an ordinary string flag everywhere.

Jump to

Keyboard shortcuts

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