flags

package module
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Jun 2, 2026 License: MIT Imports: 7 Imported by: 3 modules, 12 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

go-flags

GoVersion GoDoc License Report

master Test codecov

dev Test codecov

go-flags is a library that provides pflag.FlagSet objects for cobra commands.

Installation

This package is available through go get:

go get github.com/gildas/go-flags

Usage

EnumFlag

You can use the EnumFlag to define a flag that can only take a set of predefined values:

cmd := &cobra.Command{
    Use: "myapp",
    . . .
}

state := flags.NewEnumFlag("+one", "two", "three")
cmd.Flags().Var(state, "state", "State of the flag")
_ = cmd.RegisterFlagCompletionFunc(state.CompletionFunc("state"))

As you can see, the EnumFlag is created with a list of strings that are the only values the flag can take. The RegisterFlagCompletionFunc is used to provide completion for the flag.

The default value is prepended with a +.

Instead of values, you can provided instead a function that will be called to get the list of allowed values:

cmd := &cobra.Command{
    Use: "myapp",
    . . .
}

state := flags.NewEnumFlagWithFunc(cmd, "one", func(context.Context, *cobra.Command, []string) []string {
    return []string{"one", "two", "three"}
})
cmd.Flags().Var(state, "state", "State of the flag")
_ = cmd.RegisterFlagCompletionFunc(state.CompletionFunc("state"))

Note that the default value is not prepended with a + in this case.

Note: Since 0.5.0, when creating the EnumFlag with a function, you must provide the command as the first argument. This is a breaking change from previous versions.

EnumSliceFlag

You can use the EnumSliceFlag to define a flag that can only take a set of predefined values:

cmd := &cobra.Command{
    Use: "myapp",
    . . .
}

state := flags.NewEnumSliceFlag("+one", "+two", "three")
cmd.Flags().Var(state, "state", "State of the flag")
_ = cmd.RegisterFlagCompletionFunc(state.CompletionFunc("state"))

The default values are prepended with a +.

If all values can be provided with an all value, you can use:

cmd := &cobra.Command{
    Use: "myapp",
    . . .
}

state := flags.NewEnumSliceFlagWithAllAllowed("one", "two", "three")
cmd.Flags().Var(state, "state", "State of the flag")
_ = cmd.RegisterFlagCompletionFunc(state.CompletionFunc("state"))

Note that there is no need to add the all value to the list of allowed values.

Similarly to EnumFlag, you can provide a function that will be called to get the list of allowed values:

cmd := &cobra.Command{
    Use: "myapp",
    . . .
}

state := flags.NewEnumSliceFlagWithFunc(cmd, func(context.Context, *cobra.Command, []string) []string {
    return []string{"one", "two", "three"}
})
cmd.Flags().Var(state, "state", "State of the flag")
_ = cmd.RegisterFlagCompletionFunc(state.CompletionFunc("state"))

Note: Since 0.5.0, when creating the EnumFlag with a function, you must provide the command as the first argument. This is a breaking change from previous versions.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	InvalidEnumValue = errors.NewSentinel(http.StatusBadRequest, "error.value.invalid", "Flag value \"%s\" is invalid. Expected values are %s")
)
View Source
var VERSION = "0.5.0"

VERSION is the version of this package

Functions

This section is empty.

Types

type AllowedFunc added in v0.3.0

type AllowedFunc func(ctx context.Context, cmd *cobra.Command, args []string, toComplete string) ([]string, error)

AllowedFunc is a function that returns the allowed values for a flag

See https://pkg.go.dev/github.com/spf13/cobra@v1.8.1#Command.RegisterFlagCompletionFunc

type EnumFlag

type EnumFlag struct {
	Allowed     []string
	AllowedFunc AllowedFunc
	Value       string
	// contains filtered or unexported fields
}

EnumFlag represents a flag that can only have a value from a list of allowed values

If the AllowedFunc is set, the Allowed values are ignored and the function is called to get the allowed values.

func NewEnumFlag

func NewEnumFlag(allowed ...string) *EnumFlag

NewEnumFlag creates a new EnumFlag

The default value is prepended with a +.

If no default value is provided, the flag will not have a default value.

If more than one default value is provided, the first one is used.

Example:

flag := flags.NewEnumFlag("one", "+two", "three")

func NewEnumFlagWithFunc

func NewEnumFlagWithFunc(cmd *cobra.Command, defaultValue string, allowedFunc AllowedFunc) *EnumFlag

NewEnumFlagWithFunc creates a new EnumFlag with a function to get the allowed values

func (*EnumFlag) CompletionFunc

func (flag *EnumFlag) CompletionFunc(flagName string) (string, func(*cobra.Command, []string, string) ([]string, cobra.ShellCompDirective))

CompletionFunc returns the completion function of the flag

func (*EnumFlag) Set

func (flag *EnumFlag) Set(value string) (err error)

Set sets the flag value

If the AllowedFunc is set, the Allowed values are ignored and the function is called to get the allowed values.

implements pflag.Value

func (EnumFlag) String

func (flag EnumFlag) String() string

String returns the string representation of the flag

implements fmt.Stringer and pflag.Value

func (EnumFlag) Type

func (flag EnumFlag) Type() string

Type returns the type of the flag

implements pflag.Value

type EnumSliceFlag

type EnumSliceFlag struct {
	Allowed     []string
	Values      []string
	Default     []string
	AllowedFunc AllowedFunc
	AllAllowed  bool
	// contains filtered or unexported fields
}

EnumSliceFlag represents a flag that can only have values from a list of allowed values

The flag can be repeated to have multiple values.

func NewEnumSliceFlag

func NewEnumSliceFlag(allowed ...string) *EnumSliceFlag

NewEnumSliceFlag creates a new EnumSliceFlag

The default values are prepended with a +.

If no default value is provided, the flag will not have a default value.

Example:

flag := flags.NewEnumSliceFlag("+one", "+two", "three")

func NewEnumSliceFlagWithAllAllowed

func NewEnumSliceFlagWithAllAllowed(allowed ...string) *EnumSliceFlag

NewEnumSliceFlagWithAllAllowed creates a new EnumSliceFlag

The default values are prepended with a +.

If the flag is set to "all", all the allowed values are set.

If no default value is provided, the flag will not have a default value.

Example:

flag := flags.NewEnumSliceFlagWithAllAllowed("+one", "+two", "three")

func NewEnumSliceFlagWithAllAllowedAndFunc added in v0.2.0

func NewEnumSliceFlagWithAllAllowedAndFunc(cmd *cobra.Command, allowedFunc AllowedFunc, defaultvalues ...string) *EnumSliceFlag

NewEnumSliceFlagWithAllAllowedAndFunc creates a new EnumSliceFlag

func NewEnumSliceFlagWithFunc added in v0.2.0

func NewEnumSliceFlagWithFunc(cmd *cobra.Command, allowedFunc AllowedFunc, defaultvalues ...string) *EnumSliceFlag

NewEnumSliceFlagWithFunc creates a new EnumSliceFlag

func (*EnumSliceFlag) Append added in v0.3.2

func (flag *EnumSliceFlag) Append(value string) error

Append appends a value to the flag

implements pflag.SliceValue

func (EnumSliceFlag) CompletionFunc

func (flag EnumSliceFlag) CompletionFunc(flagName string) (string, func(*cobra.Command, []string, string) ([]string, cobra.ShellCompDirective))

CompletionFunc returns the completion function of the flag

This function is used by the cobra.Command when it needs to complete the flag value.

See: https://pkg.go.dev/github.com/spf13/cobra#Command.RegisterFlagCompletionFunc

func (EnumSliceFlag) GetSlice added in v0.3.2

func (flag EnumSliceFlag) GetSlice() []string

GetSlice returns the flag value list as a slice of strings

implements pflag.SliceValue

func (*EnumSliceFlag) Replace added in v0.3.2

func (flag *EnumSliceFlag) Replace(values []string) error

Replace replaces the flag values with the given values

implements pflag.SliceValue

func (*EnumSliceFlag) Set

func (flag *EnumSliceFlag) Set(value string) (err error)

Set sets the flag value

implements pflag.Value

func (EnumSliceFlag) String

func (flag EnumSliceFlag) String() string

String returns the string representation of the flag

implements fmt.Stringer and pflag.Value

func (EnumSliceFlag) Type

func (flag EnumSliceFlag) Type() string

Type returns the type of the flag

implements pflag.Value

Jump to

Keyboard shortcuts

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