flagtype

package
v0.0.0-...-f7b2ef8 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2026 License: MIT Imports: 7 Imported by: 0 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

flagtype

flagtype provides common flag.Value implementations for repeatable and validated flags. Each one works with State.GetFlag[T].

Flags: cli.FlagsFunc(func(f *flag.FlagSet) {
	f.Var(flagtype.StringSlice(), "tag", "add a tag (repeatable)")
	f.Var(flagtype.Enum("json", "yaml", "table"), "format", "output format")
	f.Var(flagtype.StringMap(), "label", "key=value pair (repeatable)")
}),
Exec: func(ctx context.Context, s *cli.State) error {
	tags := s.GetFlag[[]string]("tag")
	format := s.GetFlag[string]("format")
	labels := s.GetFlag[map[string]string]("label")
	_, _, _ = tags, format, labels
	return nil
},

The package also includes values for URLs and regular expressions. See the package documentation for the full API.

Documentation

Overview

Package flagtype provides common flag.Value implementations. Each also implements flag.Getter for use with cli.State.GetFlag.

Flags: cli.FlagsFunc(func(f *flag.FlagSet) {
    f.Var(flagtype.StringSlice(), "tag", "add a tag (repeatable)")
    f.Var(flagtype.Enum("json", "yaml", "table"), "format", "output format")
    f.Var(flagtype.StringMap(), "label", "key=value pair (repeatable)")
})

Inside Exec:

tags   := s.GetFlag[[]string]("tag")
format := s.GetFlag[string]("format")

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Enum

func Enum(allowed ...string) flag.Value

Enum returns a flag.Value restricted to allowed. Its value is retrieved as a string.

func EnumDefault

func EnumDefault(defaultVal string, allowed []string) flag.Value

EnumDefault is like Enum with a default value, also retrieved as a string. It panics if defaultVal is not allowed.

func Regexp

func Regexp() flag.Value

Regexp returns a flag.Value that compiles its input as a regular expression. Its value is retrieved as *regexp.Regexp.

func StringMap

func StringMap() flag.Value

StringMap returns a repeatable flag.Value that parses key=value pairs. Values may contain "="; the result is retrieved as map[string]string.

func StringSlice

func StringSlice() flag.Value

StringSlice returns a repeatable flag.Value retrieved as []string.

func URL

func URL() flag.Value

URL returns a flag.Value that requires a URL with a scheme and host. Its value is retrieved as *url.URL.

Types

This section is empty.

Jump to

Keyboard shortcuts

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