cobra

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: May 19, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package cobra applies a restrained ANSI palette — aligned with github.com/firetiger-oss/stripes — to help, usage, and error output of commands built with github.com/spf13/cobra.

The integration mirrors github.com/charmbracelet/fang: help and usage are rendered from scratch (not by post-processing cobra's defaults), and ANSI is downgraded or stripped automatically when stdout/stderr is not a terminal.

Typical usage:

root := &cobra.Command{Use: "mytool", Short: "..."}
// ... add subcommands and flags ...
if err := stripescobra.Execute(ctx, root); err != nil {
	os.Exit(1)
}

Index

Constants

This section is empty.

Variables

View Source
var DefaultStyles = &Styles{
	Title:       stripes.DefaultStyles.Name,
	Program:     stripes.DefaultStyles.Name,
	Command:     stripes.DefaultStyles.Anchor,
	Flag:        stripes.DefaultStyles.String,
	Argument:    lipgloss.NewStyle().Foreground(lipgloss.Color("250")),
	Description: lipgloss.NewStyle(),
	Default:     stripes.DefaultStyles.Comment,
	Example:     stripes.DefaultStyles.Code,
	Error:       lipgloss.NewStyle().Foreground(lipgloss.Color("1")).Bold(true),
	Hint:        stripes.DefaultStyles.Comment,
	Indent:      stripes.DefaultStyles.Indent,
	Width:       stripes.DefaultStyles.Width,
}

DefaultStyles reuses fields of stripes.DefaultStyles so the two palettes cannot drift. lipgloss.Style is a value type, so the assignments below are independent copies — later mutation of stripes.DefaultStyles does not affect this instance.

Functions

func Apply

func Apply(root *cobra.Command, opts ...Option)

Apply installs styled help and usage rendering on root and every subcommand reachable from it. It does not run the command.

Use Apply when the caller wants to own the Execute call; otherwise prefer Execute, which also routes errors through the configured handler.

func DefaultErrorHandler

func DefaultErrorHandler(w io.Writer, s *Styles, err error)

DefaultErrorHandler writes a styled "Error: <msg>" line to w. For errors that look like flag- or usage-parsing failures, it also emits a hint pointing users at --help.

func Execute

func Execute(ctx context.Context, root *cobra.Command, opts ...Option) error

Execute installs styled help, usage, and error rendering on root and every subcommand, then calls root.ExecuteContext(ctx). When cobra returns an error it is routed through the configured handler before being returned to the caller.

Types

type ErrorHandler

type ErrorHandler func(w io.Writer, s *Styles, err error)

ErrorHandler renders an error to w using styles s. Implementations should not return — any reporting must happen before returning.

type Option

type Option func(*config)

Option configures Execute or Apply.

func WithErrorHandler

func WithErrorHandler(fn ErrorHandler) Option

WithErrorHandler overrides the function used to render errors returned from cobra.Command.ExecuteContext.

func WithErrorOutput

func WithErrorOutput(w io.Writer) Option

WithErrorOutput overrides the writer used for error output. Defaults to os.Stderr.

func WithOutput

func WithOutput(w io.Writer) Option

WithOutput overrides the writer used for help and usage output. Defaults to os.Stdout.

func WithStyles

func WithStyles(s *Styles) Option

WithStyles overrides the default styles.

type Styles

type Styles struct {
	Title       lipgloss.Style // section headings: "Usage:", "Flags:", etc.
	Program     lipgloss.Style // program name in the usage line
	Command     lipgloss.Style // subcommand names in the commands list
	Flag        lipgloss.Style // flag tokens like --verbose / -v
	Argument    lipgloss.Style // <file>, [flags], placeholders
	Description lipgloss.Style // descriptions / Long text
	Default     lipgloss.Style // "(default 5)" trailing fragments
	Example     lipgloss.Style // example command lines
	Error       lipgloss.Style // "Error:" prefix
	Hint        lipgloss.Style // "Try --help for usage." footer
	Indent      string
	Width       int
}

Styles defines the styling configuration for cobra help, usage, and error output. Each field is applied to a specific token type when rendering.

func (*Styles) Clone

func (s *Styles) Clone() *Styles

Clone creates a copy of the Styles struct.

Jump to

Keyboard shortcuts

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