cli

package
v0.19.4 Latest Latest
Warning

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

Go to latest
Published: Sep 20, 2026 License: Apache-2.0, Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidLogger = errors.New("cli: invalid logger specified")

ErrInvalidLogger is returned by WithLogger when a nil *slog.Logger is supplied.

View Source
var ErrUnknownType = fmt.Errorf("unknown type")

ErrUnknownType is returned by SetArgs when an Arg's Value holds a type outside the fixed set SetArgs knows how to register as a pflag (string, []string, bool, the signed/unsigned integer and float widths, and time.Duration).

Functions

func As

func As[T any](in any) T

As type-asserts in to T, returning T's zero value when in is nil or holds a different concrete type (the failed assertion is discarded rather than panicking). Handy for narrowing the any values returned by Args.Get.

func Children

func Children(children ...*cobra.Command) opt.Option[*Command]

Children registers each of children as a subcommand of the Command being built, via cobra's AddCommand.

func Feat

func Feat() opt.Option[*Command]

Feat marks the Command hidden, the same as Hide, and is the marker used for feature-gated subcommands. QG-065: intentionally minimal -- any richer feature-gating semantics are tracked separately; until then it deliberately behaves identically to Hide rather than pretending to more.

func Hide

func Hide() opt.Option[*Command]

Hide marks the Command hidden, excluding it from cobra's generated help listings while leaving it fully invocable by name.

func Parent

func Parent(p *cobra.Command) opt.Option[*Command]

Parent registers the Command as a child of p via p.AddCommand, wiring it into an existing cobra command tree.

func SetArgs

func SetArgs(flags *pflag.FlagSet, prefix string, args Args) error

func WithArgs

func WithArgs(prefix string, a Args) opt.Option[*Command]

WithArgs registers a's flags on the Command's pflag.FlagSet under prefix (via SetArgs) and binds each to viper, so the resulting Command's flags are readable both through cobra and via Args.Get.

func WithLogger

func WithLogger(l *slog.Logger) opt.Option[*Command]

WithLogger overrides the Command's structured logger (used for the debug-level execute/execute-complete log lines) with l. Returns ErrInvalidLogger if l is nil.

Types

type Arg

type Arg struct {
	Short       string `json:"short" yaml:"short"` // one-letter abbreviated flag
	Value       any    `json:"value" yaml:"value"`
	Deprecated  bool   `json:"deprecated" yaml:"deprecated"`
	Replacement string `json:"replacement" yaml:"replacement"`
	Description string `json:"description" yaml:"description"`
	Required    bool   `json:"required" yaml:"required"`
	GetFN       func() any
}

Arg describes a single pflag/viper-bound command-line argument: its default Value, an optional one-letter Short flag, human-readable Description, Required/Deprecated markers, and (once registered via SetArgs) a GetFN closure that reads the live value back from viper.

func (*Arg) String

func (a *Arg) String() string

String renders a's Description for use as pflag usage text, appending "(required)" when a.Required is set, or "(default: <value>, required)" when a.Required is set and Value is non-zero.

type Args

type Args map[string]*Arg

Args is a named collection of command-line/config arguments, keyed by the flag name under which each Arg is registered (see SetArgs).

func (Args) Get

func (a Args) Get(key string) any

Get returns the current value of the argument named key: the live viper-bound value when the arg has been registered via SetArgs (through Arg.GetFN), or its static Value otherwise. Returns nil and logs a warning if key is not present in a, or if the argument's getter reports it was not found.

type Command

type Command struct {
	*cobra.Command

	Args Args
	Docs Docs
	// contains filtered or unexported fields
}

Command wraps a *cobra.Command with a structured logger, the bound Args used to register its flags, and the Docs describing it for help/usage output.

func NewCommand

func NewCommand(
	docs Docs,
	exec Execute,
	opts ...opt.Option[*Command],
) (*Command, error)

NewCommand builds a Command from docs, wrapping exec with debug-level entry/exit logging (via the default slog.Logger, unless overridden with WithLogger) before installing it as the underlying cobra.Command's RunE. Each opt is applied in order and NewCommand returns the first error any of them produce.

func (*Command) LogValue

func (c *Command) LogValue() slog.Value

LogValue implements slog.LogValuer, rendering c as its cobra Use and Version fields so logging a Command does not dump the full cobra.Command (flag sets, parent/child links, etc).

type Docs

type Docs struct {
	Name  string `json:"name" yaml:"name"`
	Usage string `json:"usage" yaml:"usage"`
	Short string `json:"short_desc" yaml:"short_desc"`
	Desc  string `json:"desc" yaml:"desc"`
}

Docs holds the help/usage text for a Command: its Name, cobra Usage string, one-line Short description, and longer Desc.

type Execute

type Execute func(*cobra.Command, []string) error

Execute is the signature of a Command's business logic, matching cobra.Command.RunE. NewCommand wraps the supplied Execute in debug logging before assigning it to the underlying cobra.Command's RunE.

Jump to

Keyboard shortcuts

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