Documentation
¶
Overview ¶
Package command constructs cobra.Command instances fluently starting from New.
Index ¶
- type ApplyToCommand
- type Command
- func (c Command) AddCommands(commands ...Command) Command
- func (c Command) AddPersistentPreRun(run func() error) Command
- func (c Command) All() iter.Seq[Command]
- func (c Command) CaptureCobraOutput(t *testing.T) (getStdout, getStderr func() string)
- func (c Command) Execute(options ...ExecuteOption) (err error)
- func (c Command) Flag(flagValue flag.Value, options flag.RegisterOptions) Command
- func (c Command) Long(sentence string) Command
- func (c Command) LongParagraph(paragraph string) Command
- func (c Command) MarkFlagsMutuallyExclusive(targets ...any) Command
- func (c Command) MarkFlagsOneRequired(targets ...any) Command
- func (c Command) MarkFlagsRequiredTogether(targets ...any) Command
- func (c Command) Parents() iter.Seq[Command]
- func (c Command) Run(run func() error) Command
- func (c Command) Short(short string) Command
- func (c Command) Use(use string) Command
- type ExecuteOption
- type WithExitCodeError
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 ¶
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 (Command) AddCommands ¶
AddCommands registers children commands. This is used to build a hierarchy of commands.
func (Command) AddPersistentPreRun ¶
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 ¶
All returns this command and all sub-commands added via AddCommands recursively as an iterator.
func (Command) CaptureCobraOutput ¶
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 ¶
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 ¶
Long adds the given sentence to the long command description (separated by newline).
func (Command) LongParagraph ¶
LongParagraph adds the given sentence as a new paragraph to the long command description (preceded by two newlines).
func (Command) MarkFlagsMutuallyExclusive ¶
MarkFlagsMutuallyExclusive exposes github.com/spf13/cobra.Command.MarkFlagsMutuallyExclusive fluently, accepting pointers to already registered flag values via Flag.
func (Command) MarkFlagsOneRequired ¶
MarkFlagsOneRequired exposes github.com/spf13/cobra.Command.MarkFlagsOneRequired fluently, accepting pointers to already registered flag values via Flag.
func (Command) MarkFlagsRequiredTogether ¶
MarkFlagsRequiredTogether exposes github.com/spf13/cobra.Command.MarkFlagsRequiredTogether fluently, accepting pointers to already registered flag values via Flag.
func (Command) Parents ¶
Parents returns all parent commands up and including the root Command as an iterator.
func (Command) Run ¶
Run sets the given code to run during Execute and returned errors are logged. The error may implement WithExitCodeError and is wrapped in fromRunCallbackError.
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 ¶
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