flagtype
Common flag.Value implementations for pressly/cli. Each constructor
returns a value you register with f.Var(); storage is internal, and values are
retrieved through cli.GetFlag[T].
import "github.com/pressly/cli/flagtype"
At a glance
Each symbol links to its godoc entry, the canonical reference.
FUNCTIONS
func StringSlice() flag.Value
func Enum(allowed ...string) flag.Value
func EnumDefault(defaultVal string, allowed []string) flag.Value
func StringMap() flag.Value
func URL() flag.Value
func Regexp() flag.Value
Patterns
Register a value with f.Var(), retrieve it with the matching cli.GetFlag[T]. The full set, in one place:
cmd := &cli.Command{
Name: "fetch",
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.EnumDefault("sql", []string{"sql", "go"}),
"type", "migration type")
f.Var(flagtype.StringMap(),
"label", "key=value (repeatable)")
f.Var(flagtype.URL(),
"endpoint", "API endpoint")
f.Var(flagtype.Regexp(),
"match", "filter pattern")
}),
Exec: func(ctx context.Context, state *cli.State) error {
tags := cli.GetFlag[[]string](state, "tag")
format := cli.GetFlag[string](state, "format")
migType := cli.GetFlag[string](state, "type")
labels := cli.GetFlag[map[string]string](state, "label")
endpoint := cli.GetFlag[*url.URL](state, "endpoint")
re := cli.GetFlag[*regexp.Regexp](state, "match")
_, _, _, _, _, _ = tags, format, migType, labels, endpoint, re
return nil
},
}
$ fetch --help
Usage:
fetch [flags]
Flags:
--endpoint url API endpoint
--format enum output format
--label stringMap key=value (repeatable)
--match regexp filter pattern
--tag stringSlice add a tag (repeatable)
--type enum migration type (default: sql)
Each flagtype carries a type hint that shows up in the auto-generated help. Repeatable flags (StringSlice, StringMap) collect values across multiple appearances on the command line:
fetch --tag a --tag b --label env=prod --label tier=api