cleo

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 20, 2025 License: MIT Imports: 13 Imported by: 2

Documentation

Overview

Package cleo provides a modern, plugin-based command-line interface framework.

Cleo allows building CLI applications with:

  • Hierarchical command structures with sub-commands
  • Plugin-based extensibility for custom functionality
  • Flexible I/O and filesystem abstraction
  • Context-aware execution for cancellation and deadlines
  • Structured logging with slog integration
  • Memory-efficient operations using object pooling
  • Modern Go idioms including functional options pattern

Basic Usage

Create a new command using the functional options pattern:

cmd, err := cleo.NewCmd("myapp",
	cleo.WithStdio(iox.Default()),
	cleo.WithFS(os.DirFS(".")),
	cleo.WithDescription("My awesome CLI app"),
	cleo.WithAliases("ma", "myapp"),
)
if err != nil {
	log.Fatal(err)
}

Initialize the command with context:

ctx := context.Background()
if err := cmd.InitWithContext(ctx); err != nil {
	log.Fatal(err)
}

Plugin System

Cleo supports a flexible plugin system. Plugins can implement various interfaces to provide functionality:

  • Exiter: Handle exit operations
  • plugins.FSSetable: Receive filesystem instances
  • plugins.IOSetable: Receive I/O configurations
  • plugins.Needer: Receive plugin collections

Context Support

All major operations support context for cancellation, deadlines, and tracing:

ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()

err := cmd.MainWithContext(ctx, "/current/dir", []string{"arg1", "arg2"})

Structured Logging

Cleo integrates with Go's structured logging (slog) package:

logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
cmd, err := cleo.NewCmd("myapp", cleo.WithLogger(logger))

Memory Efficiency

Cleo uses object pooling internally to reduce garbage collection pressure when dealing with plugin collections and other frequently allocated objects.

Error Handling

Cleo provides typed errors for common conditions:

if errors.Is(err, cleo.ErrNilCommand) {
	// Handle nil command error
}

Available error types:

  • ErrNilCommand: Command is nil
  • ErrNilFS: Filesystem is nil
  • ErrEmptyCommandName: Command name is empty
  • ErrNilSubCommand: Sub-command is nil
  • ErrNoCommand: No command specified
  • ErrNoCommands: No commands registered
  • ErrUnknownCommand: Unknown command specified

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNoCommand        = errors.New("no command specified")
	ErrNoCommands       = errors.New("no commands registered")
	ErrNilCommand       = errors.New("nil command")
	ErrNilFS            = errors.New("fs.FS is nil")
	ErrEmptyCommandName = errors.New("empty command name")
	ErrNilSubCommand    = errors.New("nil sub-command")
)

Common errors

Functions

func Exit

func Exit(cmd plugins.Stdioer, code int, err error)

Exit will print the usage information for the command to the given Stderr. If the cmd.ExitFn is set, it will be called with the given exit code. Otherwise, os.Exit will be called.

Types

type Aliaser

type Aliaser interface {
	CmdAliases() []string
}

Aliaser provides command aliasing functionality.

type Cmd

type Cmd struct {
	iox.IO // IO to be used by the command
	fs.FS  // FS to be used by the command

	Aliases  []string             // Aliases for the command
	Commands map[string]Commander // Sub commands for the command
	Feeder   plugins.FeederFn     // Plugins for the command
	Name     string               // Name of the command

	Desc string // Description of the command

	ExitFn func(int) error // ExitFn is used by the Exit method.
	// contains filtered or unexported fields
}

func NewCmd

func NewCmd(name string, opts ...Option) (*Cmd, error)

NewCmd creates a new command with the given name and options.

func (*Cmd) CmdAliases

func (cmd *Cmd) CmdAliases() []string

CmdAliases returns the aliases for the command.

func (*Cmd) CmdName

func (cmd *Cmd) CmdName() string

CmdName returns the name of the command.

func (*Cmd) Description

func (cmd *Cmd) Description() string

func (*Cmd) Exit

func (cmd *Cmd) Exit(code int) error

func (*Cmd) ExitWithContext

func (cmd *Cmd) ExitWithContext(ctx context.Context, code int) error

ExitWithContext exits the command with the given code, using the provided context.

func (*Cmd) FileSystem

func (cmd *Cmd) FileSystem() (fs.FS, error)

func (*Cmd) Init

func (cmd *Cmd) Init() error

Init will initialize the command. It should be called before the command is used.

func (*Cmd) InitWithContext

func (cmd *Cmd) InitWithContext(ctx context.Context) error

InitWithContext will initialize the command with the given context. It should be called before the command is used.

func (*Cmd) Logger

func (cmd *Cmd) Logger() *slog.Logger

Logger returns the logger for the command.

func (*Cmd) Main

func (cmd *Cmd) Main(ctx context.Context, pwd string, args []string) error

Main is the main entry point for the command. NEEDS TO BE IMPLEMENTED

func (*Cmd) MainWithContext

func (cmd *Cmd) MainWithContext(ctx context.Context, pwd string, args []string) error

MainWithContext is the main entry point for the command with context support. NEEDS TO BE IMPLEMENTED

func (*Cmd) MarshalJSON

func (cmd *Cmd) MarshalJSON() ([]byte, error)

MarshalJSON returns a JSON representation of the command.

func (*Cmd) PluginFeeder

func (cmd *Cmd) PluginFeeder() plugins.FeederFn

Plugins will provider a single FeederFn that will return all of the plugins that are available to the command.

func (*Cmd) PluginName

func (cmd *Cmd) PluginName() string

PluginName returns name of the plugin.

func (*Cmd) ScopedPlugins

func (cmd *Cmd) ScopedPlugins() plugins.Plugins

ScopedPlugins returns the plugins scoped to the command. If the plugins include the current command, it will be removed from the returned list.

func (*Cmd) SetFileSystem

func (cmd *Cmd) SetFileSystem(cab fs.FS) error

func (*Cmd) SetStdio

func (cmd *Cmd) SetStdio(oi iox.IO) error

func (*Cmd) Stdio

func (cmd *Cmd) Stdio() iox.IO

func (*Cmd) String

func (cmd *Cmd) String() string

String returns a string representation of the command.

func (*Cmd) SubCommands

func (cmd *Cmd) SubCommands() []Commander

SubCommands returns the sub-commands for the command.

type Commander

type Commander = plugcmd.Commander

type ContextualCommander

type ContextualCommander interface {
	plugcmd.Commander
	MainWithContext(ctx context.Context, pwd string, args []string) error
}

ContextualCommander extends the base Commander with context support.

type ContextualExiter

type ContextualExiter interface {
	ExitWithContext(ctx context.Context, code int) error
}

ContextualExiter provides context-aware exit functionality.

type ContextualInitializer

type ContextualInitializer interface {
	InitWithContext(ctx context.Context) error
}

ContextualInitializer provides context-aware initialization.

type Describer

type Describer interface {
	Description() string
}

Describer provides command description functionality.

type Env

type Env struct {
	// contains filtered or unexported fields
}

func (*Env) Get

func (e *Env) Get(key string) string

func (*Env) Set

func (e *Env) Set(key, value string)

type ErrUnknownCommand

type ErrUnknownCommand string

func (ErrUnknownCommand) Error

func (e ErrUnknownCommand) Error() string

type Exiter

type Exiter interface {
	// plugins.Stdioer
	plugins.Plugin
	Exit(code int) error
}

type Logger

type Logger interface {
	Logger() *slog.Logger
}

Logger provides structured logging functionality.

type ModernCommander

ModernCommander combines all the modern interfaces for a full-featured command.

type Namer

type Namer interface {
	CmdName() string
}

Namer provides command naming functionality.

type Option

type Option func(*Cmd) error

Option represents a functional option for configuring a Cmd.

func WithAliases

func WithAliases(aliases ...string) Option

WithAliases sets the aliases for the command.

func WithDescription

func WithDescription(desc string) Option

WithDescription sets the description for the command.

func WithExitFunction

func WithExitFunction(exitFn func(int) error) Option

WithExitFunction sets the exit function for the command.

func WithFS

func WithFS(filesystem fs.FS) Option

WithFS sets the filesystem for the command.

func WithLogger

func WithLogger(logger *slog.Logger) Option

WithLogger sets the logger for the command.

func WithPluginFeeder

func WithPluginFeeder(feeder plugins.FeederFn) Option

WithPluginFeeder sets the plugin feeder for the command.

func WithStdio

func WithStdio(io iox.IO) Option

WithStdio sets the I/O for the command.

func WithSubCommand

func WithSubCommand(name string, subcmd Commander) Option

WithSubCommand adds a sub-command to the command.

Jump to

Keyboard shortcuts

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