flagtype

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Feb 18, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package flagtype provides common flag.Value implementations for use with flag.FlagSet.Var.

All types implement flag.Getter so they work with [cli.GetFlag].

The following types are available:

  • StringSlice - repeatable flag that collects values into []string
  • Enum - restricts values to a predefined set, retrieved as string
  • StringMap - repeatable flag that parses key=value pairs into map[string]string
  • URL - parses and validates a URL (must have scheme and host), retrieved as *url.URL
  • Regexp - compiles a regular expression, retrieved as *regexp.Regexp

Example registration:

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)")
})

Example retrieval in Exec:

tags   := cli.GetFlag[[]string](s, "tag")
format := cli.GetFlag[string](s, "format")
labels := cli.GetFlag[map[string]string](s, "label")

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 that restricts the flag to one of the allowed values. If a value not in the allowed list is provided, an error is returned listing valid options.

Use [cli.GetFlag] with type string to retrieve the value.

func Regexp

func Regexp() flag.Value

Regexp returns a flag.Value that compiles the flag value as a regular expression. If the pattern is invalid, an error is returned.

Use [cli.GetFlag] with type *regexp.Regexp to retrieve the value.

func StringMap

func StringMap() flag.Value

StringMap returns a flag.Value that parses key=value pairs into a map. The flag can be repeated to add multiple entries, like --label=env=prod --label=tier=web. The value is split on the first "=" character, so values may contain additional "=" characters.

Use [cli.GetFlag] with type map[string]string to retrieve the value.

func StringSlice

func StringSlice() flag.Value

StringSlice returns a flag.Value that collects values into a string slice. Each time the flag is set, the value is appended. This allows repeatable flags like --tag=foo --tag=bar.

Use [cli.GetFlag] with type []string to retrieve the value.

func URL

func URL() flag.Value

URL returns a flag.Value that parses the flag value as a URL. The URL must have both a scheme and a host, otherwise an error is returned.

Use [cli.GetFlag] with type *url.URL to retrieve the value.

Types

This section is empty.

Jump to

Keyboard shortcuts

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