cli

package
v0.0.12 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2026 License: GPL-3.0 Imports: 4 Imported by: 1

Documentation

Overview

Package cli provides a minimal framework for building command-line applications with named subcommands.

The CLI type is generic over an App parameter, which allows passing application-level dependencies (e.g. database connections, configuration) through to command handlers without global state.

CLIs can be nested by using CLI.Command to convert a child CLI into a Command that can be registered on a parent.

Index

Constants

View Source
const (
	ExitCodeSuccess = 0
	ExitCodeError   = 1
	ExitCodeUsage   = 2
)

Exit codes returned by CLI.Run.

Variables

This section is empty.

Functions

This section is empty.

Types

type CLI

type CLI[App any] struct {
	Name     string
	Commands map[string]Command[App]
}

CLI is a command-line application with a set of named subcommands. The App type parameter represents application-level dependencies shared across commands.

func (*CLI[App]) Command

func (cli *CLI[App]) Command(desc string) Command[App]

Command wraps this CLI as a Command with the given description, allowing it to be registered as a subcommand of a parent CLI. This enables nested command hierarchies.

func (*CLI[App]) Run

func (cli *CLI[App]) Run(ctx context.Context, app App, args []string) int

Run parses the first element of args as a command name and dispatches to the matching Command. It returns an exit code suitable for use with os.Exit. If no command is given or the command is "help", it prints usage information and returns ExitCodeUsage.

type Command

type Command[App any] struct {
	Description string
	Run         func(ctx context.Context, app App, args []string) error
}

Command is a named subcommand with a description and a run function. The description is displayed in help output. The run function receives the context, the application value, and any remaining arguments after the command name.

Jump to

Keyboard shortcuts

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