cli

package
v0.0.0-...-c8ecd8b Latest Latest
Warning

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

Go to latest
Published: Aug 9, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package cli provides standard error handling infrastructure for CLI entry points.

This package implements a consistent error handling pattern across all command-line tools, including:

  • Panic recovery with defer/recover
  • Structured error wrapping with context
  • Consistent exit codes
  • Error logging for debugging

Usage pattern:

func main() {
    cli.Main(run)
}

func run() error {
    // Actual main logic here
    return nil
}

Index

Constants

View Source
const (
	ExitSuccess       = 0
	ExitCodeError     = 1
	ExitCodeUsage     = 2
	ExitCodePanic     = 70 // Internal software error (BSD EX_SOFTWARE)
	ExitCodeInterrupt = 4
)

Exit codes

Variables

This section is empty.

Functions

func IsUsageError

func IsUsageError(err error) bool

IsUsageError checks if an error is a UsageError.

func Main

func Main(runFn RunFunc)

Main wraps a RunFunc with standard error handling and panic recovery. It catches panics, logs errors appropriately, and exits with the correct code.

Usage:

func main() {
    cli.Main(run)
}

func run() error {
    // Your main logic here
    return nil
}

func SanitizeError

func SanitizeError(err error) error

SanitizeError removes sensitive information from error messages. This is a basic implementation that looks for common patterns. Extend this as needed for your specific sensitive data patterns.

func WrapContext

func WrapContext(err error, context string) error

WrapContext wraps an error with additional context information. This is useful for adding stack-like context to errors as they propagate.

Types

type ExitError

type ExitError struct {
	// Message is the error message to display
	Message string

	// Code is the exit code to use
	Code int

	// Underlying is the original error (if any)
	Underlying error
}

ExitError is a standard error that carries an exit code.

func NewExitError

func NewExitError(message string, code int) *ExitError

NewExitError creates an ExitError with a message and exit code.

func WrapExitError

func WrapExitError(err error, code int) *ExitError

WrapExitError wraps an existing error with an exit code.

func (*ExitError) Error

func (e *ExitError) Error() string

Error implements the error interface.

func (*ExitError) Unwrap

func (e *ExitError) Unwrap() error

Unwrap returns the underlying error for errors.Is/As compatibility.

type RunFunc

type RunFunc func() error

RunFunc is the signature for the main logic function. All CLI entry points should implement their main logic in a function of this type, then pass it to Main().

type UsageError

type UsageError struct {
	Message string
}

UsageError is an error indicating incorrect command-line usage. It should exit with code 2 (Unix convention for usage errors).

func NewUsageError

func NewUsageError(message string) *UsageError

NewUsageError creates a UsageError.

func (*UsageError) Error

func (e *UsageError) Error() string

Error implements the error interface.

Jump to

Keyboard shortcuts

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