Documentation
¶
Overview ¶
Package flags provides a powerful, reflection-based way to generate modern command-line interfaces (CLIs) from Go structs. It uses spf13/cobra for command execution and rsteube/carapace for advanced shell completion.
The primary workflow is to define your CLI structure (commands, flags, positional arguments) using Go structs and field tags, and then call flags.ParseCommands() to create a fully configured *cobra.Command tree, complete with shell completions, ready for execution.
For useful, pre-built flag types like Counter or HexBytes, see the subpackage at "github.com/reeflective/flags/types".
Index ¶
- Variables
- func Bind(cmd *cobra.Command, data any, opts ...Option) error
- func Parse(data any, opts ...Option) (*cobra.Command, error)
- type Commander
- type Completer
- type Marshaler
- type Option
- func WithCompleter(name string, completer carapace.CompletionCallback) Option
- func WithEnvDivider(divider string) Option
- func WithEnvPrefix(prefix string) Option
- func WithFlagDivider(divider string) Option
- func WithPrefix(prefix string) Option
- func WithValidation() Option
- func WithValidator(v *validator.Validate) Option
- func WithVars(vars map[string]string) Option
- type PostRunner
- type PostRunnerE
- type PreRunner
- type PreRunnerE
- type Runner
- type Unmarshaler
- type ValidateFunc
- type Value
Constants ¶
This section is empty.
Variables ¶
var ( // ErrParse is a general error used to wrap more specific parsing errors. ErrParse = errors.ErrParse // ErrNotPointerToStruct indicates that a provided data container is not // a pointer to a struct. ErrNotPointerToStruct = errors.ErrNotPointerToStruct // ErrNotCommander is returned when a struct is tagged as a command but // does not implement a command interface (e.g., Commander). ErrNotCommander = errors.ErrNotCommander // ErrInvalidTag indicates an invalid tag or invalid use of an existing tag. ErrInvalidTag = errors.ErrInvalidTag // ErrNotValue indicates that a struct field type for a flag does not // implement the flags.Value interface. ErrNotValue = errors.ErrNotValue )
Functions ¶
func Bind ¶ added in v0.11.0
Bind parses a struct and binds its commands, flags, and positional arguments to an existing *cobra.Command. This is useful for integrating flags with a command tree that is partially managed manually.
Shell completions for the bound components are generated and attached automatically.
func Parse ¶ added in v0.11.0
Generate parses a struct and creates a new, fully configured *cobra.Command. The provided `data` argument must be a pointer to a struct. Struct fields tagged with `command:"..."` become subcommands, and other tagged fields become flags. A struct implementing one of the Runner interfaces becomes an executable command.
Shell completions are generated and attached automatically.
This is the primary entry point for creating a new CLI application.
Types ¶
type Commander ¶
type Commander = interfaces.Commander
Commander is the primary interface for a struct to be recognized as an executable command. Its Execute method is bound to cobra.Command.RunE.
type Completer ¶ added in v0.11.0
type Completer = interfaces.Completer
Completer is the interface for types that can provide their own shell completion suggestions.
type Marshaler ¶ added in v0.11.0
type Marshaler = interfaces.Marshaler
Marshaler is the interface implemented by types that can marshal themselves to a string representation of the flag. Retroported from jessevdk/go-flags.
type Option ¶ added in v0.11.0
Option is a functional option for configuring command and flag generation.
func WithCompleter ¶ added in v0.11.0
func WithCompleter(name string, completer carapace.CompletionCallback) Option
WithCompleter adds a custom completer function to the parser options. You can use this completer by tagging flag or positional arg struct fields with `complete:"custom-completer-name"`, and bind this completer under the same name in this function.
func WithEnvDivider ¶ added in v0.11.0
WithEnvDivider sets the character used to separate words in environment variable names.
func WithEnvPrefix ¶ added in v0.11.0
WithEnvPrefix sets a prefix for all environment variables.
func WithFlagDivider ¶ added in v0.11.0
WithFlagDivider sets the character used to separate words in long flag names.
func WithPrefix ¶ added in v0.11.0
WithPrefix sets a prefix that will be applied to all long flag names.
func WithValidation ¶ added in v0.11.0
func WithValidation() Option
WithValidation adds field validation for fields with the "validate" tag. This makes use of go-playground/validator internally, refer to their docs for an exhaustive list of valid tag validations.
func WithValidator ¶ added in v0.11.0
WithValidator registers a custom validation function for flags and arguments. It is required to pass a go-playground/validator object for customization. The latter library has been chosen because it supports most of the validation one would want in CLI, and because there are vast possibilities for registering and using custom validations through the *Validate type.
type PostRunner ¶ added in v0.9.1
type PostRunner = interfaces.PostRunner
PostRunner is the equivalent of cobra.Command.PostRun.
type PostRunnerE ¶ added in v0.9.1
type PostRunnerE = interfaces.PostRunnerE
PostRunnerE is the equivalent of cobra.Command.PostRunE.
type PreRunner ¶ added in v0.9.1
type PreRunner = interfaces.PreRunner
PreRunner is the equivalent of cobra.Command.PreRun.
type PreRunnerE ¶ added in v0.9.1
type PreRunnerE = interfaces.PreRunnerE
PreRunnerE is the equivalent of cobra.Command.PreRunE.
type Runner ¶ added in v0.9.1
type Runner = interfaces.Runner
Runner is a simpler command interface bound to cobra.Command.Run. It is ignored if the struct also implements Commander.
type Unmarshaler ¶ added in v0.11.0
type Unmarshaler = interfaces.Unmarshaler
Unmarshaler is the interface implemented by types that can unmarshal a flag argument to themselves. The provided value is directly passed from the command line. Retroported from jessevdk/go-flags.
type ValidateFunc ¶
type ValidateFunc = validation.ValidateFunc
ValidateFunc is the core validation function type. It takes the actual Go value to validate, the validation tag string, and the field name for error reporting. This is the simplified interface the user wants to implement.
Directories
¶
| Path | Synopsis |
|---|---|
|
internal
|
|
|
gen
Package flags (github.com/reeflective/flags/gen/flags) provides the entrypoints to command trees and flags generation for arbitrary structures.
|
Package flags (github.com/reeflective/flags/gen/flags) provides the entrypoints to command trees and flags generation for arbitrary structures. |
|
values/genvalues
command
This generator is for internal usage only.
|
This generator is for internal usage only. |
|
Package types provides useful, pre-built implementations of the flags.Value interface for common use cases.
|
Package types provides useful, pre-built implementations of the flags.Value interface for common use cases. |