command

package
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Jul 6, 2025 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Provides several types for managing multiple commands within a single application.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Command

type Command interface {
	// Returns the command's name; essentially it's identifier
	GetName() string

	// Prints usage information
	PrintUsage()

	// Runs the command using the provided args and returns any error
	Run(args []string) error
}

The interface definition of a command. Any type that implements the interface can be used by the runner.

type CommandBase

type CommandBase struct {
	// The name for command. Used by the GetName function
	Name string

	// The description of the command's usage. Used by the PrintUsage function
	Description string

	// The flag set for the command. Please use NewCommandBase to initialize correctly
	Flags *flag.FlagSet
}

A base struct definition for a command. Provides default implementations for some command interface methods.

func NewCommandBase

func NewCommandBase(name string, desc string) CommandBase

Creates a new command base; mostly used when creating child commands. Additionally sets a custom usage function for flag set.

func (CommandBase) GetName

func (cmd CommandBase) GetName() string

Command interface implementation. Returns the Name field.

func (CommandBase) PrintUsage

func (cmd CommandBase) PrintUsage()

Command interface implementation. Prints usage using the command's Name and Description fields.

type ConfigCommand added in v1.2.0

type ConfigCommand[T config.Config] struct {
	ConfigCommandBase[T]
}

Command for performing operations with the config file.

func NewConfigCommand added in v1.2.0

func NewConfigCommand[T config.Config]() ConfigCommand[T]

Creates a new config command.

func (ConfigCommand[T]) Run added in v1.2.0

func (cmd ConfigCommand[T]) Run(args []string) error

Runs the config commands. Run with -h for details.

type ConfigCommandBase added in v1.2.0

type ConfigCommandBase[T config.Config] struct {
	CommandBase
	// contains filtered or unexported fields
}

func NewConfigCommandBase added in v1.2.0

func NewConfigCommandBase[T config.Config](name, desc string) ConfigCommandBase[T]

func (ConfigCommandBase[T]) LoadConfig added in v1.2.0

func (cmd ConfigCommandBase[T]) LoadConfig() (*T, error)

type Runner

type Runner struct {
	// The map of commands. Please use NewRunner to initialize correctly
	Commands map[string]Command
}

Runner stores multiple commands and provides methods for running by name.

func NewRunner

func NewRunner(commands ...Command) Runner

Creates a new runner using the provided commands.

func (Runner) ListCommands

func (r Runner) ListCommands()

Print the usage for all commands to the console.

func (Runner) RunCommand

func (r Runner) RunCommand(name string, args []string) error

Run the command that matches the provided name (case sensitive) with the provided arguments. Returns any run errors, or an error if the command was not found.

Directories

Path Synopsis
Provides sample commands used to demonstrate the command package.
Provides sample commands used to demonstrate the command package.

Jump to

Keyboard shortcuts

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