command

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2025 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package command constructs cobra.Command instances fluently starting from New.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ApplyToCommand

type ApplyToCommand func(command Command)

ApplyToCommand is a ExecuteOption modifying the command before execution. This is primarily useful for tests but also offers fluent access to any property of the embedded cobra.Command.

type Command

type Command struct {
	*cobra.Command
	// contains filtered or unexported fields
}

A Command wraps cobra.Command and provides a fluent API to build CLI commands. Use New to construct one and starting fluently building. You can either use Run and register parameters using Flag or FlagBool. Or use AddCommands to build a command hierarchy. In any case, use Short and Long and LongParagraph to build the help message.

func New

func New() Command

New constructs a command.

func (Command) AddCommands

func (c Command) AddCommands(commands ...Command) Command

AddCommands registers children commands. This is used to build a hierarchy of commands.

func (Command) AddPersistentPreRun

func (c Command) AddPersistentPreRun(run func() error) Command

AddPersistentPreRun adds the given code to run persistently, that means it will be executed also for sub commands added with AddCommands. Pre-runs before PreRun of command itself and the Run of the command.

func (Command) All

func (c Command) All() iter.Seq[Command]

All returns this command and all sub-commands added via AddCommands recursively as an iterator.

func (Command) CaptureCobraOutput

func (c Command) CaptureCobraOutput(t *testing.T) (getStdout, getStderr func() string)

CaptureCobraOutput install buffers for Stdout and Stderr and provides suppliers once the command has been executed to obtain the output.

This function is only useful for testing.

func (Command) Execute

func (c Command) Execute(options ...ExecuteOption) (err error)

Execute executes the command using cobra and takes care of error handling. By default, exits the application with proper exit code and never returns. By default, logs an error originating from Run callback execution using log.Printf. See WithExiter and WithErrorLogger to change this default behavior (which can be useful for testing).

func (Command) Flag

func (c Command) Flag(flagValue flag.Value, options flag.RegisterOptions) Command

Flag registers a new flag. Use flag.String, flag.Bool or flag.Slice to construct one and set appropriate flag.RegisterOptions. The given param might implement flag.Binding.

func (Command) Long

func (c Command) Long(sentence string) Command

Long adds the given sentence to the long command description (separated by newline).

func (Command) LongParagraph

func (c Command) LongParagraph(paragraph string) Command

LongParagraph adds the given sentence as a new paragraph to the long command description (preceded by two newlines).

func (Command) MarkFlagsMutuallyExclusive

func (c Command) MarkFlagsMutuallyExclusive(targets ...any) Command

MarkFlagsMutuallyExclusive exposes github.com/spf13/cobra.Command.MarkFlagsMutuallyExclusive fluently, accepting pointers to already registered flag values via Flag.

func (Command) MarkFlagsOneRequired

func (c Command) MarkFlagsOneRequired(targets ...any) Command

MarkFlagsOneRequired exposes github.com/spf13/cobra.Command.MarkFlagsOneRequired fluently, accepting pointers to already registered flag values via Flag.

func (Command) MarkFlagsRequiredTogether

func (c Command) MarkFlagsRequiredTogether(targets ...any) Command

MarkFlagsRequiredTogether exposes github.com/spf13/cobra.Command.MarkFlagsRequiredTogether fluently, accepting pointers to already registered flag values via Flag.

func (Command) Parents

func (c Command) Parents() iter.Seq[Command]

Parents returns all parent commands up and including the root Command as an iterator.

func (Command) Run

func (c Command) Run(run func() error) Command

Run sets the given code to run during Execute and returned errors are logged. The error may implement WithExitCodeError and is wrapped in fromRunCallbackError.

func (Command) Short

func (c Command) Short(short string) Command

Short sets the short command description.

func (Command) Use

func (c Command) Use(use string) Command

Use specify the command usage, see cobra.Command#Use.

type ExecuteOption

type ExecuteOption interface {
	// contains filtered or unexported methods
}

ExecuteOption is given to Command.Execute to modify the execution. See implementations, in particular ApplyToCommand.

func AssertExitCode

func AssertExitCode(t *testing.T, expectedExitCode int) ExecuteOption

AssertExitCode is a ExecuteOption which uses WithExiter to assert the given exit code upon execution.

This function is only useful for testing.

func AssertWithRun

func AssertWithRun(t *testing.T, onRun func()) ExecuteOption

AssertWithRun installs a Run callback into the command to assert that it was called. Additional runs provided onRun if not nil. A possibly existing Run function is overwritten. Restores the previous possibly nil RunE function using testing.T.Cleanup.

This function is only useful for testing.

func WithArgs

func WithArgs(args ...string) ExecuteOption

WithArgs sets the used arguments before the command is executed. Providing this ExecuteOption without any arguments has the effect that os.Args is not considered by Cobra, which is useful for tests.

func WithErrorLogger

func WithErrorLogger(logger func(err error)) ExecuteOption

WithErrorLogger uses the given logger for errors originating from executing Command.Run callbacks. By default, logs using log.Printf.

func WithExiter

func WithExiter(exiter func(exitCode int)) ExecuteOption

WithExiter sets a different exit function. The default is os.Exit which makes Command.Execute never return.

type WithExitCodeError

type WithExitCodeError struct {
	ExitCode int
	Wrapped  error
}

WithExitCodeError allows to set an exit code for that error. See Command.Execute.

func (WithExitCodeError) Error

func (e WithExitCodeError) Error() string

func (WithExitCodeError) Unwrap

func (e WithExitCodeError) Unwrap() error

Jump to

Keyboard shortcuts

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